Changes in uspace/app/inet/inet.c [bf9e6fc:b8b1adb1] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/inet/inet.c
rbf9e6fc rb8b1adb1 1 1 /* 2 * Copyright (c) 201 2Jiri Svoboda2 * Copyright (c) 2013 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 36 36 37 37 #include <errno.h> 38 #include <inet/addr.h> 38 39 #include <inet/inetcfg.h> 39 40 #include <loc.h> … … 43 44 #include <sys/types.h> 44 45 45 #define NAME "inet"46 #define NAME "inet" 46 47 47 48 static void print_syntax(void) … … 54 55 } 55 56 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 147 57 static int addr_create_static(int argc, char *argv[]) 148 58 { … … 178 88 } 179 89 180 rc = naddr_parse(addr_spec, &naddr);90 rc = inet_naddr_parse(addr_spec, &naddr); 181 91 if (rc != EOK) { 182 92 printf(NAME ": Invalid network address format '%s'.\n", … … 267 177 route_name = argv[2]; 268 178 269 rc = naddr_parse(dest_str, &dest);179 rc = inet_naddr_parse(dest_str, &dest); 270 180 if (rc != EOK) { 271 181 printf(NAME ": Invalid network address format '%s'.\n", … … 274 184 } 275 185 276 rc = addr_parse(router_str, &router);186 rc = inet_addr_parse(router_str, &router); 277 187 if (rc != EOK) { 278 188 printf(NAME ": Invalid address format '%s'.\n", router_str); … … 347 257 if (count > 0) 348 258 printf(" [Addr/Width] [Link-Name] [Addr-Name] [Def-MTU]\n"); 349 ainfo.name = linfo.name = astr = NULL; 259 ainfo.name = NULL; 260 linfo.name = NULL; 261 astr = NULL; 350 262 351 263 for (i = 0; i < count; i++) { … … 366 278 } 367 279 368 rc = naddr_format(&ainfo.naddr, &astr);280 rc = inet_naddr_format(&ainfo.naddr, &astr); 369 281 if (rc != EOK) { 370 282 printf("Memory allocation failed.\n"); … … 380 292 free(astr); 381 293 382 ainfo.name = linfo.name = astr = NULL; 294 ainfo.name = NULL; 295 linfo.name = NULL; 296 astr = NULL; 383 297 } 384 298 … … 398 312 } 399 313 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 400 360 static int sroute_list(void) 401 361 { … … 419 379 printf(" [Dest/Width] [Router-Addr] [Route-Name]\n"); 420 380 421 srinfo.name = dest_str = router_str = NULL; 381 srinfo.name = NULL; 382 dest_str = NULL; 383 router_str = NULL; 422 384 423 385 for (i = 0; i < count; i++) { … … 430 392 } 431 393 432 rc = naddr_format(&srinfo.dest, &dest_str);394 rc = inet_naddr_format(&srinfo.dest, &dest_str); 433 395 if (rc != EOK) { 434 396 printf("Memory allocation failed.\n"); … … 437 399 } 438 400 439 rc = addr_format(&srinfo.router, &router_str);401 rc = inet_addr_format(&srinfo.router, &router_str); 440 402 if (rc != EOK) { 441 403 printf("Memory allocation failed.\n"); … … 450 412 free(router_str); 451 413 452 router_str = srinfo.name = dest_str = NULL; 414 router_str = NULL; 415 srinfo.name = NULL; 416 dest_str = NULL; 453 417 } 454 418 … … 486 450 if (rc != EOK) 487 451 return 1; 452 rc = link_list(); 453 if (rc != EOK) 454 return 1; 488 455 return 0; 489 456 }
Note:
See TracChangeset
for help on using the changeset viewer.