Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/networking/libiproute/ll_map.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/networking/libiproute/ll_map.c
r821 r1770 1 /* vi: set sw=4 ts=4: */ 1 2 /* 2 3 * ll_map.c … … 11 12 */ 12 13 14 #include <net/if.h> /* struct ifreq and co. */ 15 13 16 #include "libbb.h" 14 #include <string.h>15 16 17 #include "libnetlink.h" 17 18 #include "ll_map.h" 18 19 19 struct idxmap 20 { 21 struct idxmap * next; 22 int index; 23 int type; 24 int alen; 25 unsigned flags; 26 unsigned char addr[8]; 27 char name[16]; 20 struct idxmap { 21 struct idxmap *next; 22 int index; 23 int type; 24 int alen; 25 unsigned flags; 26 unsigned char addr[8]; 27 char name[16]; 28 28 }; 29 29 30 30 static struct idxmap *idxmap[16]; 31 32 static struct idxmap *find_by_index(int idx) 33 { 34 struct idxmap *im; 35 36 for (im = idxmap[idx & 0xF]; im; im = im->next) 37 if (im->index == idx) 38 return im; 39 return NULL; 40 } 31 41 32 42 int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) … … 43 53 return -1; 44 54 45 46 55 memset(tb, 0, sizeof(tb)); 47 56 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n)); … … 49 58 return 0; 50 59 51 h = ifi->ifi_index &0xF;60 h = ifi->ifi_index & 0xF; 52 61 53 for (imp =&idxmap[h]; (im=*imp)!=NULL; imp = &im->next)62 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next) 54 63 if (im->index == ifi->ifi_index) 55 break;64 goto found; 56 65 57 if (im == NULL) { 58 im = xmalloc(sizeof(*im)); 59 im->next = *imp; 60 im->index = ifi->ifi_index; 61 *imp = im; 62 } 63 66 im = xmalloc(sizeof(*im)); 67 im->next = *imp; 68 im->index = ifi->ifi_index; 69 *imp = im; 70 found: 64 71 im->type = ifi->ifi_type; 65 72 im->flags = ifi->ifi_flags; … … 84 91 if (idx == 0) 85 92 return "*"; 86 for (im = idxmap[idx&0xF]; im; im = im->next)87 if (im->index == idx)88 93 im = find_by_index(idx); 94 if (im) 95 return im->name; 89 96 snprintf(buf, 16, "if%d", idx); 90 97 return buf; … … 99 106 } 100 107 108 #ifdef UNUSED 101 109 int ll_index_to_type(int idx) 102 110 { … … 105 113 if (idx == 0) 106 114 return -1; 107 for (im = idxmap[idx&0xF]; im; im = im->next)108 if (im->index == idx)109 115 im = find_by_index(idx); 116 if (im) 117 return im->type; 110 118 return -1; 111 119 } 120 #endif 112 121 113 122 unsigned ll_index_to_flags(int idx) … … 117 126 if (idx == 0) 118 127 return 0; 119 120 for (im = idxmap[idx&0xF]; im; im = im->next) 121 if (im->index == idx) 122 return im->flags; 128 im = find_by_index(idx); 129 if (im) 130 return im->flags; 123 131 return 0; 124 132 } 125 133 126 int ll_name_to_index(char *name)134 int xll_name_to_index(const char *const name) 127 135 { 136 int ret = 0; 137 int sock_fd; 138 139 /* caching is not warranted - no users which repeatedly call it */ 140 #ifdef UNUSED 128 141 static char ncache[16]; 129 142 static int icache; 143 130 144 struct idxmap *im; 131 145 int i; 132 146 133 147 if (name == NULL) 134 return 0; 135 if (icache && strcmp(name, ncache) == 0) 136 return icache; 137 for (i=0; i<16; i++) { 148 goto out; 149 if (icache && strcmp(name, ncache) == 0) { 150 ret = icache; 151 goto out; 152 } 153 for (i = 0; i < 16; i++) { 138 154 for (im = idxmap[i]; im; im = im->next) { 139 155 if (strcmp(im->name, name) == 0) { 140 156 icache = im->index; 141 157 strcpy(ncache, name); 142 return im->index; 158 ret = im->index; 159 goto out; 143 160 } 144 161 } 145 162 } 146 return 0; 163 /* We have not found the interface in our cache, but the kernel 164 * may still know about it. One reason is that we may be using 165 * module on-demand loading, which means that the kernel will 166 * load the module and make the interface exist only when 167 * we explicitely request it (check for dev_load() in net/core/dev.c). 168 * I can think of other similar scenario, but they are less common... 169 * Jean II */ 170 #endif 171 172 sock_fd = socket(AF_INET, SOCK_DGRAM, 0); 173 if (sock_fd) { 174 struct ifreq ifr; 175 int tmp; 176 177 strncpy(ifr.ifr_name, name, IFNAMSIZ); 178 ifr.ifr_ifindex = -1; 179 tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr); 180 close(sock_fd); 181 if (tmp >= 0) 182 /* In theory, we should redump the interface list 183 * to update our cache, this is left as an exercise 184 * to the reader... Jean II */ 185 ret = ifr.ifr_ifindex; 186 } 187 /* out:*/ 188 if (ret <= 0) 189 bb_error_msg_and_die("cannot find device \"%s\"", name); 190 return ret; 147 191 } 148 192 149 193 int ll_init_map(struct rtnl_handle *rth) 150 194 { 151 if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) { 152 perror("Cannot send dump request"); 153 exit(1); 154 } 155 156 if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) { 157 fprintf(stderr, "Dump terminated\n"); 158 exit(1); 159 } 195 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK); 196 xrtnl_dump_filter(rth, ll_remember_index, &idxmap); 160 197 return 0; 161 198 }
Note:
See TracChangeset
for help on using the changeset viewer.