Changes in uspace/app/inet/inet.c [b8b1adb1:bf9e6fc] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/inet/inet.c
rb8b1adb1 rbf9e6fc 1 1 /* 2 * Copyright (c) 201 3Jiri Svoboda2 * Copyright (c) 2012 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 36 36 37 37 #include <errno.h> 38 #include <inet/addr.h>39 38 #include <inet/inetcfg.h> 40 39 #include <loc.h> … … 44 43 #include <sys/types.h> 45 44 46 #define NAME 45 #define NAME "inet" 47 46 48 47 static void print_syntax(void) … … 55 54 } 56 55 56 static int naddr_parse(const char *text, inet_naddr_t *naddr) 57 { 58 unsigned long a[4], bits; 59 char *cp = (char *)text; 60 int i; 61 62 for (i = 0; i < 3; i++) { 63 a[i] = strtoul(cp, &cp, 10); 64 if (*cp != '.') 65 return EINVAL; 66 ++cp; 67 } 68 69 a[3] = strtoul(cp, &cp, 10); 70 if (*cp != '/') 71 return EINVAL; 72 ++cp; 73 74 bits = strtoul(cp, &cp, 10); 75 if (*cp != '\0') 76 return EINVAL; 77 78 naddr->ipv4 = 0; 79 for (i = 0; i < 4; i++) { 80 if (a[i] > 255) 81 return EINVAL; 82 naddr->ipv4 = (naddr->ipv4 << 8) | a[i]; 83 } 84 85 if (bits > 31) 86 return EINVAL; 87 88 naddr->bits = bits; 89 return EOK; 90 } 91 92 static int addr_parse(const char *text, inet_addr_t *addr) 93 { 94 unsigned long a[4]; 95 char *cp = (char *)text; 96 int i; 97 98 for (i = 0; i < 3; i++) { 99 a[i] = strtoul(cp, &cp, 10); 100 if (*cp != '.') 101 return EINVAL; 102 ++cp; 103 } 104 105 a[3] = strtoul(cp, &cp, 10); 106 if (*cp != '\0') 107 return EINVAL; 108 109 addr->ipv4 = 0; 110 for (i = 0; i < 4; i++) { 111 if (a[i] > 255) 112 return EINVAL; 113 addr->ipv4 = (addr->ipv4 << 8) | a[i]; 114 } 115 116 return EOK; 117 } 118 119 static int naddr_format(inet_naddr_t *naddr, char **bufp) 120 { 121 int rc; 122 123 rc = asprintf(bufp, "%d.%d.%d.%d/%d", naddr->ipv4 >> 24, 124 (naddr->ipv4 >> 16) & 0xff, (naddr->ipv4 >> 8) & 0xff, 125 naddr->ipv4 & 0xff, naddr->bits); 126 127 if (rc < 0) 128 return ENOMEM; 129 130 return EOK; 131 } 132 133 static int addr_format(inet_addr_t *addr, char **bufp) 134 { 135 int rc; 136 137 rc = asprintf(bufp, "%d.%d.%d.%d", addr->ipv4 >> 24, 138 (addr->ipv4 >> 16) & 0xff, (addr->ipv4 >> 8) & 0xff, 139 addr->ipv4 & 0xff); 140 141 if (rc < 0) 142 return ENOMEM; 143 144 return EOK; 145 } 146 57 147 static int addr_create_static(int argc, char *argv[]) 58 148 { … … 88 178 } 89 179 90 rc = inet_naddr_parse(addr_spec, &naddr);180 rc = naddr_parse(addr_spec, &naddr); 91 181 if (rc != EOK) { 92 182 printf(NAME ": Invalid network address format '%s'.\n", … … 177 267 route_name = argv[2]; 178 268 179 rc = inet_naddr_parse(dest_str, &dest);269 rc = naddr_parse(dest_str, &dest); 180 270 if (rc != EOK) { 181 271 printf(NAME ": Invalid network address format '%s'.\n", … … 184 274 } 185 275 186 rc = inet_addr_parse(router_str, &router);276 rc = addr_parse(router_str, &router); 187 277 if (rc != EOK) { 188 278 printf(NAME ": Invalid address format '%s'.\n", router_str); … … 257 347 if (count > 0) 258 348 printf(" [Addr/Width] [Link-Name] [Addr-Name] [Def-MTU]\n"); 259 ainfo.name = NULL; 260 linfo.name = NULL; 261 astr = NULL; 349 ainfo.name = linfo.name = astr = NULL; 262 350 263 351 for (i = 0; i < count; i++) { … … 278 366 } 279 367 280 rc = inet_naddr_format(&ainfo.naddr, &astr);368 rc = naddr_format(&ainfo.naddr, &astr); 281 369 if (rc != EOK) { 282 370 printf("Memory allocation failed.\n"); … … 292 380 free(astr); 293 381 294 ainfo.name = NULL; 295 linfo.name = NULL; 296 astr = NULL; 382 ainfo.name = linfo.name = astr = NULL; 297 383 } 298 384 … … 312 398 } 313 399 314 static int link_list(void)315 {316 sysarg_t *link_list;317 inet_link_info_t linfo;318 319 size_t count;320 size_t i;321 int rc;322 323 rc = inetcfg_get_link_list(&link_list, &count);324 if (rc != EOK) {325 printf(NAME ": Failed getting link list.\n");326 return rc;327 }328 329 printf("IP links:\n");330 if (count > 0)331 printf(" [Link-layer Address] [Link-Name] [Def-MTU]\n");332 333 for (i = 0; i < count; i++) {334 rc = inetcfg_link_get(link_list[i], &linfo);335 if (rc != EOK) {336 printf("Failed getting properties of link %zu.\n",337 (size_t)link_list[i]);338 continue;339 }340 341 printf(" %02x:%02x:%02x:%02x:%02x:%02x %s %zu\n",342 linfo.mac_addr[0], linfo.mac_addr[1],343 linfo.mac_addr[2], linfo.mac_addr[3],344 linfo.mac_addr[4], linfo.mac_addr[5],345 linfo.name, linfo.def_mtu);346 347 free(linfo.name);348 349 linfo.name = NULL;350 }351 352 if (count == 0)353 printf(" None\n");354 355 free(link_list);356 357 return EOK;358 }359 360 400 static int sroute_list(void) 361 401 { … … 379 419 printf(" [Dest/Width] [Router-Addr] [Route-Name]\n"); 380 420 381 srinfo.name = NULL; 382 dest_str = NULL; 383 router_str = NULL; 421 srinfo.name = dest_str = router_str = NULL; 384 422 385 423 for (i = 0; i < count; i++) { … … 392 430 } 393 431 394 rc = inet_naddr_format(&srinfo.dest, &dest_str);432 rc = naddr_format(&srinfo.dest, &dest_str); 395 433 if (rc != EOK) { 396 434 printf("Memory allocation failed.\n"); … … 399 437 } 400 438 401 rc = inet_addr_format(&srinfo.router, &router_str);439 rc = addr_format(&srinfo.router, &router_str); 402 440 if (rc != EOK) { 403 441 printf("Memory allocation failed.\n"); … … 412 450 free(router_str); 413 451 414 router_str = NULL; 415 srinfo.name = NULL; 416 dest_str = NULL; 452 router_str = srinfo.name = dest_str = NULL; 417 453 } 418 454 … … 448 484 return 1; 449 485 rc = sroute_list(); 450 if (rc != EOK)451 return 1;452 rc = link_list();453 486 if (rc != EOK) 454 487 return 1;
Note:
See TracChangeset
for help on using the changeset viewer.