ifconfig.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /* ifconfig
  2. *
  3. * Similar to the standard Unix ifconfig, but with only the necessary
  4. * parts for AF_INET, and without any printing of if info (for now).
  5. *
  6. * Bjorn Wesen, Axis Communications AB
  7. *
  8. *
  9. * Authors of the original ifconfig was:
  10. * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  11. *
  12. * This program is free software; you can redistribute it
  13. * and/or modify it under the terms of the GNU General
  14. * Public License as published by the Free Software
  15. * Foundation; either version 2 of the License, or (at
  16. * your option) any later version.
  17. *
  18. * $Id: ifconfig.c,v 1.30 2004/03/31 11:30:08 andersen Exp $
  19. *
  20. */
  21. /*
  22. * Heavily modified by Manuel Novoa III Mar 6, 2001
  23. *
  24. * From initial port to busybox, removed most of the redundancy by
  25. * converting to a table-driven approach. Added several (optional)
  26. * args missing from initial port.
  27. *
  28. * Still missing: media, tunnel.
  29. *
  30. * 2002-04-20
  31. * IPV6 support added by Bart Visscher <magick@linux-fan.com>
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h> /* strcmp and friends */
  36. #include <ctype.h> /* isdigit and friends */
  37. #include <stddef.h> /* offsetof */
  38. #include <unistd.h>
  39. #include <netdb.h>
  40. #include <sys/ioctl.h>
  41. #include <net/if.h>
  42. #include <net/if_arp.h>
  43. #include <netinet/in.h>
  44. #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
  45. #include <netpacket/packet.h>
  46. #include <net/ethernet.h>
  47. #else
  48. #include <sys/types.h>
  49. #include <netinet/if_ether.h>
  50. #endif
  51. #include "inet_common.h"
  52. #include "busybox.h"
  53. #ifdef CONFIG_FEATURE_IFCONFIG_SLIP
  54. # include <net/if_slip.h>
  55. #endif
  56. /* I don't know if this is needed for busybox or not. Anyone? */
  57. #define QUESTIONABLE_ALIAS_CASE
  58. /* Defines for glibc2.0 users. */
  59. #ifndef SIOCSIFTXQLEN
  60. # define SIOCSIFTXQLEN 0x8943
  61. # define SIOCGIFTXQLEN 0x8942
  62. #endif
  63. /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
  64. #ifndef ifr_qlen
  65. # define ifr_qlen ifr_ifru.ifru_mtu
  66. #endif
  67. #ifndef IFF_DYNAMIC
  68. # define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
  69. #endif
  70. #ifdef CONFIG_FEATURE_IPV6
  71. struct in6_ifreq {
  72. struct in6_addr ifr6_addr;
  73. uint32_t ifr6_prefixlen;
  74. int ifr6_ifindex;
  75. };
  76. #endif
  77. /*
  78. * Here are the bit masks for the "flags" member of struct options below.
  79. * N_ signifies no arg prefix; M_ signifies arg prefixed by '-'.
  80. * CLR clears the flag; SET sets the flag; ARG signifies (optional) arg.
  81. */
  82. #define N_CLR 0x01
  83. #define M_CLR 0x02
  84. #define N_SET 0x04
  85. #define M_SET 0x08
  86. #define N_ARG 0x10
  87. #define M_ARG 0x20
  88. #define M_MASK (M_CLR | M_SET | M_ARG)
  89. #define N_MASK (N_CLR | N_SET | N_ARG)
  90. #define SET_MASK (N_SET | M_SET)
  91. #define CLR_MASK (N_CLR | M_CLR)
  92. #define SET_CLR_MASK (SET_MASK | CLR_MASK)
  93. #define ARG_MASK (M_ARG | N_ARG)
  94. /*
  95. * Here are the bit masks for the "arg_flags" member of struct options below.
  96. */
  97. /*
  98. * cast type:
  99. * 00 int
  100. * 01 char *
  101. * 02 HOST_COPY in_ether
  102. * 03 HOST_COPY INET_resolve
  103. */
  104. #define A_CAST_TYPE 0x03
  105. /*
  106. * map type:
  107. * 00 not a map type (mem_start, io_addr, irq)
  108. * 04 memstart (unsigned long)
  109. * 08 io_addr (unsigned short)
  110. * 0C irq (unsigned char)
  111. */
  112. #define A_MAP_TYPE 0x0C
  113. #define A_ARG_REQ 0x10 /* Set if an arg is required. */
  114. #define A_NETMASK 0x20 /* Set if netmask (check for multiple sets). */
  115. #define A_SET_AFTER 0x40 /* Set a flag at the end. */
  116. #define A_COLON_CHK 0x80 /* Is this needed? See below. */
  117. #ifdef CONFIG_FEATURE_IFCONFIG_BROADCAST_PLUS
  118. #define A_HOSTNAME 0x100 /* Set if it is ip addr. */
  119. #define A_BROADCAST 0x200 /* Set if it is broadcast addr. */
  120. #else
  121. #define A_HOSTNAME 0
  122. #define A_BROADCAST 0
  123. #endif
  124. /*
  125. * These defines are for dealing with the A_CAST_TYPE field.
  126. */
  127. #define A_CAST_CHAR_PTR 0x01
  128. #define A_CAST_RESOLVE 0x01
  129. #define A_CAST_HOST_COPY 0x02
  130. #define A_CAST_HOST_COPY_IN_ETHER A_CAST_HOST_COPY
  131. #define A_CAST_HOST_COPY_RESOLVE (A_CAST_HOST_COPY | A_CAST_RESOLVE)
  132. /*
  133. * These defines are for dealing with the A_MAP_TYPE field.
  134. */
  135. #define A_MAP_ULONG 0x04 /* memstart */
  136. #define A_MAP_USHORT 0x08 /* io_addr */
  137. #define A_MAP_UCHAR 0x0C /* irq */
  138. /*
  139. * Define the bit masks signifying which operations to perform for each arg.
  140. */
  141. #define ARG_METRIC (A_ARG_REQ /*| A_CAST_INT*/)
  142. #define ARG_MTU (A_ARG_REQ /*| A_CAST_INT*/)
  143. #define ARG_TXQUEUELEN (A_ARG_REQ /*| A_CAST_INT*/)
  144. #define ARG_MEM_START (A_ARG_REQ | A_MAP_ULONG)
  145. #define ARG_IO_ADDR (A_ARG_REQ | A_MAP_ULONG)
  146. #define ARG_IRQ (A_ARG_REQ | A_MAP_UCHAR)
  147. #define ARG_DSTADDR (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE)
  148. #define ARG_NETMASK (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK)
  149. #define ARG_BROADCAST (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_BROADCAST)
  150. #define ARG_HW (A_ARG_REQ | A_CAST_HOST_COPY_IN_ETHER)
  151. #define ARG_POINTOPOINT (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
  152. #define ARG_KEEPALIVE (A_ARG_REQ | A_CAST_CHAR_PTR)
  153. #define ARG_OUTFILL (A_ARG_REQ | A_CAST_CHAR_PTR)
  154. #define ARG_HOSTNAME (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK | A_HOSTNAME)
  155. #define ARG_ADD_DEL (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
  156. /*
  157. * Set up the tables. Warning! They must have corresponding order!
  158. */
  159. struct arg1opt {
  160. const char *name;
  161. int selector;
  162. unsigned short ifr_offset;
  163. };
  164. struct options {
  165. const char *name;
  166. #ifdef CONFIG_FEATURE_IFCONFIG_BROADCAST_PLUS
  167. const unsigned int flags:6;
  168. const unsigned int arg_flags:10;
  169. #else
  170. const unsigned char flags;
  171. const unsigned char arg_flags;
  172. #endif
  173. const unsigned short selector;
  174. };
  175. #define ifreq_offsetof(x) offsetof(struct ifreq, x)
  176. static const struct arg1opt Arg1Opt[] = {
  177. {"SIOCSIFMETRIC", SIOCSIFMETRIC, ifreq_offsetof(ifr_metric)},
  178. {"SIOCSIFMTU", SIOCSIFMTU, ifreq_offsetof(ifr_mtu)},
  179. {"SIOCSIFTXQLEN", SIOCSIFTXQLEN, ifreq_offsetof(ifr_qlen)},
  180. {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
  181. {"SIOCSIFNETMASK", SIOCSIFNETMASK, ifreq_offsetof(ifr_netmask)},
  182. {"SIOCSIFBRDADDR", SIOCSIFBRDADDR, ifreq_offsetof(ifr_broadaddr)},
  183. #ifdef CONFIG_FEATURE_IFCONFIG_HW
  184. {"SIOCSIFHWADDR", SIOCSIFHWADDR, ifreq_offsetof(ifr_hwaddr)},
  185. #endif
  186. {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
  187. #ifdef SIOCSKEEPALIVE
  188. {"SIOCSKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data)},
  189. #endif
  190. #ifdef SIOCSOUTFILL
  191. {"SIOCSOUTFILL", SIOCSOUTFILL, ifreq_offsetof(ifr_data)},
  192. #endif
  193. #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  194. {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.mem_start)},
  195. {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.base_addr)},
  196. {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.irq)},
  197. #endif
  198. /* Last entry if for unmatched (possibly hostname) arg. */
  199. #ifdef CONFIG_FEATURE_IPV6
  200. {"SIOCSIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr)}, /* IPv6 version ignores the offset */
  201. {"SIOCDIFADDR", SIOCDIFADDR, ifreq_offsetof(ifr_addr)}, /* IPv6 version ignores the offset */
  202. #endif
  203. {"SIOCSIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr)},
  204. };
  205. static const struct options OptArray[] = {
  206. {"metric", N_ARG, ARG_METRIC, 0},
  207. {"mtu", N_ARG, ARG_MTU, 0},
  208. {"txqueuelen", N_ARG, ARG_TXQUEUELEN, 0},
  209. {"dstaddr", N_ARG, ARG_DSTADDR, 0},
  210. {"netmask", N_ARG, ARG_NETMASK, 0},
  211. {"broadcast", N_ARG | M_CLR, ARG_BROADCAST, IFF_BROADCAST},
  212. #ifdef CONFIG_FEATURE_IFCONFIG_HW
  213. {"hw", N_ARG, ARG_HW, 0},
  214. #endif
  215. {"pointopoint", N_ARG | M_CLR, ARG_POINTOPOINT, IFF_POINTOPOINT},
  216. #ifdef SIOCSKEEPALIVE
  217. {"keepalive", N_ARG, ARG_KEEPALIVE, 0},
  218. #endif
  219. #ifdef SIOCSOUTFILL
  220. {"outfill", N_ARG, ARG_OUTFILL, 0},
  221. #endif
  222. #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  223. {"mem_start", N_ARG, ARG_MEM_START, 0},
  224. {"io_addr", N_ARG, ARG_IO_ADDR, 0},
  225. {"irq", N_ARG, ARG_IRQ, 0},
  226. #endif
  227. #ifdef CONFIG_FEATURE_IPV6
  228. {"add", N_ARG, ARG_ADD_DEL, 0},
  229. {"del", N_ARG, ARG_ADD_DEL, 0},
  230. #endif
  231. {"arp", N_CLR | M_SET, 0, IFF_NOARP},
  232. {"trailers", N_CLR | M_SET, 0, IFF_NOTRAILERS},
  233. {"promisc", N_SET | M_CLR, 0, IFF_PROMISC},
  234. {"multicast", N_SET | M_CLR, 0, IFF_MULTICAST},
  235. {"allmulti", N_SET | M_CLR, 0, IFF_ALLMULTI},
  236. {"dynamic", N_SET | M_CLR, 0, IFF_DYNAMIC},
  237. {"up", N_SET, 0, (IFF_UP | IFF_RUNNING)},
  238. {"down", N_CLR, 0, IFF_UP},
  239. {NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING)}
  240. };
  241. /*
  242. * A couple of prototypes.
  243. */
  244. #ifdef CONFIG_FEATURE_IFCONFIG_HW
  245. static int in_ether(char *bufp, struct sockaddr *sap);
  246. #endif
  247. #ifdef CONFIG_FEATURE_IFCONFIG_STATUS
  248. extern int interface_opt_a;
  249. extern int display_interfaces(char *ifname);
  250. #endif
  251. /*
  252. * Our main function.
  253. */
  254. int ifconfig_main(int argc, char **argv)
  255. {
  256. struct ifreq ifr;
  257. struct sockaddr_in sai;
  258. #ifdef CONFIG_FEATURE_IPV6
  259. struct sockaddr_in6 sai6;
  260. #endif
  261. #ifdef CONFIG_FEATURE_IFCONFIG_HW
  262. struct sockaddr sa;
  263. #endif
  264. const struct arg1opt *a1op;
  265. const struct options *op;
  266. int sockfd; /* socket fd we use to manipulate stuff with */
  267. int goterr;
  268. int selector;
  269. #ifdef CONFIG_FEATURE_IFCONFIG_BROADCAST_PLUS
  270. unsigned int mask;
  271. unsigned int did_flags;
  272. unsigned int sai_hostname, sai_netmask;
  273. #else
  274. unsigned char mask;
  275. unsigned char did_flags;
  276. #endif
  277. char *p;
  278. char host[128];
  279. goterr = 0;
  280. did_flags = 0;
  281. #ifdef CONFIG_FEATURE_IFCONFIG_BROADCAST_PLUS
  282. sai_hostname = 0;
  283. sai_netmask = 0;
  284. #endif
  285. /* skip argv[0] */
  286. ++argv;
  287. --argc;
  288. #ifdef CONFIG_FEATURE_IFCONFIG_STATUS
  289. if ((argc > 0) && (((*argv)[0] == '-') && ((*argv)[1] == 'a') && !(*argv)[2])) {
  290. interface_opt_a = 1;
  291. --argc;
  292. ++argv;
  293. }
  294. #endif
  295. if (argc <= 1) {
  296. #ifdef CONFIG_FEATURE_IFCONFIG_STATUS
  297. return display_interfaces(argc ? *argv : NULL);
  298. #else
  299. bb_error_msg_and_die
  300. ("ifconfig was not compiled with interface status display support.");
  301. #endif
  302. }
  303. /* Create a channel to the NET kernel. */
  304. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  305. bb_perror_msg_and_die("socket");
  306. }
  307. /* get interface name */
  308. safe_strncpy(ifr.ifr_name, *argv, IFNAMSIZ);
  309. /* Process the remaining arguments. */
  310. while (*++argv != (char *) NULL) {
  311. p = *argv;
  312. mask = N_MASK;
  313. if (*p == '-') { /* If the arg starts with '-'... */
  314. ++p; /* advance past it and */
  315. mask = M_MASK; /* set the appropriate mask. */
  316. }
  317. for (op = OptArray; op->name; op++) { /* Find table entry. */
  318. if (strcmp(p, op->name) == 0) { /* If name matches... */
  319. if ((mask &= op->flags)) { /* set the mask and go. */
  320. goto FOUND_ARG;
  321. }
  322. /* If we get here, there was a valid arg with an */
  323. /* invalid '-' prefix. */
  324. ++goterr;
  325. goto LOOP;
  326. }
  327. }
  328. /* We fell through, so treat as possible hostname. */
  329. a1op = Arg1Opt + (sizeof(Arg1Opt) / sizeof(Arg1Opt[0])) - 1;
  330. mask = op->arg_flags;
  331. goto HOSTNAME;
  332. FOUND_ARG:
  333. if (mask & ARG_MASK) {
  334. mask = op->arg_flags;
  335. a1op = Arg1Opt + (op - OptArray);
  336. if (mask & A_NETMASK & did_flags) {
  337. bb_show_usage();
  338. }
  339. if (*++argv == NULL) {
  340. if (mask & A_ARG_REQ) {
  341. bb_show_usage();
  342. } else {
  343. --argv;
  344. mask &= A_SET_AFTER; /* just for broadcast */
  345. }
  346. } else { /* got an arg so process it */
  347. HOSTNAME:
  348. did_flags |= (mask & (A_NETMASK|A_HOSTNAME));
  349. if (mask & A_CAST_HOST_COPY) {
  350. #ifdef CONFIG_FEATURE_IFCONFIG_HW
  351. if (mask & A_CAST_RESOLVE) {
  352. #endif
  353. #ifdef CONFIG_FEATURE_IPV6
  354. char *prefix;
  355. int prefix_len = 0;
  356. #endif
  357. safe_strncpy(host, *argv, (sizeof host));
  358. #ifdef CONFIG_FEATURE_IPV6
  359. if ((prefix = strchr(host, '/'))) {
  360. if (safe_strtoi(prefix + 1, &prefix_len) ||
  361. (prefix_len < 0) || (prefix_len > 128))
  362. {
  363. ++goterr;
  364. goto LOOP;
  365. }
  366. *prefix = 0;
  367. }
  368. #endif
  369. sai.sin_family = AF_INET;
  370. sai.sin_port = 0;
  371. if (!strcmp(host, bb_INET_default)) {
  372. /* Default is special, meaning 0.0.0.0. */
  373. sai.sin_addr.s_addr = INADDR_ANY;
  374. #ifdef CONFIG_FEATURE_IFCONFIG_BROADCAST_PLUS
  375. } else if (((host[0] == '+') && !host[1]) && (mask & A_BROADCAST) &&
  376. (did_flags & (A_NETMASK|A_HOSTNAME)) == (A_NETMASK|A_HOSTNAME)) {
  377. /* + is special, meaning broadcast is derived. */
  378. sai.sin_addr.s_addr = (~sai_netmask) | (sai_hostname & sai_netmask);
  379. #endif
  380. #ifdef CONFIG_FEATURE_IPV6
  381. } else if (inet_pton(AF_INET6, host, &sai6.sin6_addr) > 0) {
  382. int sockfd6;
  383. struct in6_ifreq ifr6;
  384. memcpy((char *) &ifr6.ifr6_addr,
  385. (char *) &sai6.sin6_addr,
  386. sizeof(struct in6_addr));
  387. /* Create a channel to the NET kernel. */
  388. if ((sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
  389. bb_perror_msg_and_die("socket6");
  390. }
  391. if (ioctl(sockfd6, SIOGIFINDEX, &ifr) < 0) {
  392. perror("SIOGIFINDEX");
  393. ++goterr;
  394. continue;
  395. }
  396. ifr6.ifr6_ifindex = ifr.ifr_ifindex;
  397. ifr6.ifr6_prefixlen = prefix_len;
  398. if (ioctl(sockfd6, a1op->selector, &ifr6) < 0) {
  399. perror(a1op->name);
  400. ++goterr;
  401. }
  402. continue;
  403. #endif
  404. } else if (inet_aton(host, &sai.sin_addr) == 0) {
  405. /* It's not a dotted quad. */
  406. struct hostent *hp;
  407. if ((hp = gethostbyname(host)) == (struct hostent *)NULL) {
  408. ++goterr;
  409. continue;
  410. }
  411. memcpy((char *) &sai.sin_addr, (char *) hp->h_addr_list[0],
  412. sizeof(struct in_addr));
  413. }
  414. #ifdef CONFIG_FEATURE_IFCONFIG_BROADCAST_PLUS
  415. if (mask & A_HOSTNAME) {
  416. sai_hostname = sai.sin_addr.s_addr;
  417. }
  418. if (mask & A_NETMASK) {
  419. sai_netmask = sai.sin_addr.s_addr;
  420. }
  421. #endif
  422. p = (char *) &sai;
  423. #ifdef CONFIG_FEATURE_IFCONFIG_HW
  424. } else { /* A_CAST_HOST_COPY_IN_ETHER */
  425. /* This is the "hw" arg case. */
  426. if (strcmp("ether", *argv) || (*++argv == NULL)) {
  427. bb_show_usage();
  428. }
  429. safe_strncpy(host, *argv, (sizeof host));
  430. if (in_ether(host, &sa)) {
  431. bb_error_msg("invalid hw-addr %s", host);
  432. ++goterr;
  433. continue;
  434. }
  435. p = (char *) &sa;
  436. }
  437. #endif
  438. memcpy((((char *) (&ifr)) + a1op->ifr_offset),
  439. p, sizeof(struct sockaddr));
  440. } else {
  441. unsigned long i = strtoul(*argv, NULL, 0);
  442. p = ((char *) (&ifr)) + a1op->ifr_offset;
  443. #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  444. if (mask & A_MAP_TYPE) {
  445. if (ioctl(sockfd, SIOCGIFMAP, &ifr) < 0) {
  446. ++goterr;
  447. continue;
  448. }
  449. if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR) {
  450. *((unsigned char *) p) = i;
  451. } else if (mask & A_MAP_USHORT) {
  452. *((unsigned short *) p) = i;
  453. } else {
  454. *((unsigned long *) p) = i;
  455. }
  456. } else
  457. #endif
  458. if (mask & A_CAST_CHAR_PTR) {
  459. *((caddr_t *) p) = (caddr_t) i;
  460. } else { /* A_CAST_INT */
  461. *((int *) p) = i;
  462. }
  463. }
  464. if (ioctl(sockfd, a1op->selector, &ifr) < 0) {
  465. perror(a1op->name);
  466. ++goterr;
  467. continue;
  468. }
  469. #ifdef QUESTIONABLE_ALIAS_CASE
  470. if (mask & A_COLON_CHK) {
  471. /*
  472. * Don't do the set_flag() if the address is an alias with
  473. * a - at the end, since it's deleted already! - Roman
  474. *
  475. * Should really use regex.h here, not sure though how well
  476. * it'll go with the cross-platform support etc.
  477. */
  478. char *ptr;
  479. short int found_colon = 0;
  480. for (ptr = ifr.ifr_name; *ptr; ptr++) {
  481. if (*ptr == ':') {
  482. found_colon++;
  483. }
  484. }
  485. if (found_colon && *(ptr - 1) == '-') {
  486. continue;
  487. }
  488. }
  489. #endif
  490. }
  491. if (!(mask & A_SET_AFTER)) {
  492. continue;
  493. }
  494. mask = N_SET;
  495. }
  496. if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
  497. perror("SIOCGIFFLAGS");
  498. ++goterr;
  499. } else {
  500. selector = op->selector;
  501. if (mask & SET_MASK) {
  502. ifr.ifr_flags |= selector;
  503. } else {
  504. ifr.ifr_flags &= ~selector;
  505. }
  506. if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
  507. perror("SIOCSIFFLAGS");
  508. ++goterr;
  509. }
  510. }
  511. LOOP:
  512. continue;
  513. } /* end of while-loop */
  514. if (ENABLE_FEATURE_CLEAN_UP) close(sockfd);
  515. return goterr;
  516. }
  517. #ifdef CONFIG_FEATURE_IFCONFIG_HW
  518. /* Input an Ethernet address and convert to binary. */
  519. static int in_ether(char *bufp, struct sockaddr *sap)
  520. {
  521. char *ptr;
  522. int i, j;
  523. unsigned char val;
  524. unsigned char c;
  525. sap->sa_family = ARPHRD_ETHER;
  526. ptr = sap->sa_data;
  527. i = 0;
  528. do {
  529. j = val = 0;
  530. /* We might get a semicolon here - not required. */
  531. if (i && (*bufp == ':')) {
  532. bufp++;
  533. }
  534. do {
  535. c = *bufp;
  536. if (((unsigned char)(c - '0')) <= 9) {
  537. c -= '0';
  538. } else if (((unsigned char)((c|0x20) - 'a')) <= 5) {
  539. c = (c|0x20) - ('a'-10);
  540. } else if (j && (c == ':' || c == 0)) {
  541. break;
  542. } else {
  543. return -1;
  544. }
  545. ++bufp;
  546. val <<= 4;
  547. val += c;
  548. } while (++j < 2);
  549. *ptr++ = val;
  550. } while (++i < ETH_ALEN);
  551. return (int) (*bufp); /* Error if we don't end at end of string. */
  552. }
  553. #endif