Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/networking/libiproute/iprule.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/networking/libiproute/iprule.c
r1765 r2725 1 1 /* vi: set sw=4 ts=4: */ 2 2 /* 3 * iprule.c "ip rule". 3 * This program is free software; you can redistribute it and/or 4 * modify it under the terms of the GNU General Public License 5 * as published by the Free Software Foundation; either version 6 * 2 of the License, or (at your option) any later version. 4 7 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * as published by the Free Software Foundation; either version 8 * 2 of the License, or (at your option) any later version. 9 * 10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 11 * 8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 12 9 * 13 10 * Changes: 14 11 * 15 * Rani Assaf <rani@magic.metawire.com> 980929: 16 * initially integrated into busybox by Bernhard Fischer12 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses 13 * initially integrated into busybox by Bernhard Reutner-Fischer 17 14 */ 18 15 19 #include <syslog.h>20 //#include <sys/socket.h>21 16 #include <netinet/in.h> 22 17 #include <netinet/ip.h> … … 43 38 */ 44 39 45 static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED, 46 struct nlmsghdr *n, void *arg) 47 { 48 FILE *fp = (FILE*)arg; 40 static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM, 41 struct nlmsghdr *n, void *arg UNUSED_PARAM) 42 { 49 43 struct rtmsg *r = NLMSG_DATA(n); 50 44 int len = n->nlmsg_len; … … 73 67 host_len = 80; 74 68 */ 75 if (tb[RTA_PRIORITY]) 76 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])); 77 else 78 fprintf(fp, "0:\t"); 79 80 fprintf(fp, "from "); 69 printf("%u:\t", tb[RTA_PRIORITY] ? 70 *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]) 71 : 0); 72 printf("from "); 81 73 if (tb[RTA_SRC]) { 82 74 if (r->rtm_src_len != host_len) { 83 fprintf(fp, "%s/%u", rt_addr_n2a(r->rtm_family, 84 RTA_PAYLOAD(tb[RTA_SRC]), 75 printf("%s/%u", rt_addr_n2a(r->rtm_family, 85 76 RTA_DATA(tb[RTA_SRC]), 86 77 abuf, sizeof(abuf)), … … 88 79 ); 89 80 } else { 90 fp rintf(fp, "%s",format_host(r->rtm_family,81 fputs(format_host(r->rtm_family, 91 82 RTA_PAYLOAD(tb[RTA_SRC]), 92 83 RTA_DATA(tb[RTA_SRC]), 93 abuf, sizeof(abuf)) 94 ); 84 abuf, sizeof(abuf)), stdout); 95 85 } 96 86 } else if (r->rtm_src_len) { 97 fprintf(fp,"0/%d", r->rtm_src_len);87 printf("0/%d", r->rtm_src_len); 98 88 } else { 99 fprintf(fp,"all");100 } 101 fprintf(fp, " ");89 printf("all"); 90 } 91 bb_putchar(' '); 102 92 103 93 if (tb[RTA_DST]) { 104 94 if (r->rtm_dst_len != host_len) { 105 fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family, 106 RTA_PAYLOAD(tb[RTA_DST]), 95 printf("to %s/%u ", rt_addr_n2a(r->rtm_family, 107 96 RTA_DATA(tb[RTA_DST]), 108 97 abuf, sizeof(abuf)), … … 110 99 ); 111 100 } else { 112 fprintf(fp,"to %s ", format_host(r->rtm_family,101 printf("to %s ", format_host(r->rtm_family, 113 102 RTA_PAYLOAD(tb[RTA_DST]), 114 103 RTA_DATA(tb[RTA_DST]), … … 116 105 } 117 106 } else if (r->rtm_dst_len) { 118 fprintf(fp,"to 0/%d ", r->rtm_dst_len);107 printf("to 0/%d ", r->rtm_dst_len); 119 108 } 120 109 121 110 if (r->rtm_tos) { 122 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));111 printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1)); 123 112 } 124 113 if (tb[RTA_PROTOINFO]) { 125 fprintf(fp,"fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));114 printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO])); 126 115 } 127 116 128 117 if (tb[RTA_IIF]) { 129 fprintf(fp,"iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));118 printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF])); 130 119 } 131 120 132 121 if (r->rtm_table) 133 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));122 printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1)); 134 123 135 124 if (tb[RTA_FLOW]) { … … 138 127 to &= 0xFFFF; 139 128 if (from) { 140 fprintf(fp,"realms %s/",141 rtnl_rtrealm_n2a(from, b1 , sizeof(b1)));129 printf("realms %s/", 130 rtnl_rtrealm_n2a(from, b1)); 142 131 } 143 fprintf(fp,"%s ",144 rtnl_rtrealm_n2a(to, b1 , sizeof(b1)));132 printf("%s ", 133 rtnl_rtrealm_n2a(to, b1)); 145 134 } 146 135 147 136 if (r->rtm_type == RTN_NAT) { 148 137 if (tb[RTA_GATEWAY]) { 149 fprintf(fp,"map-to %s ",138 printf("map-to %s ", 150 139 format_host(r->rtm_family, 151 140 RTA_PAYLOAD(tb[RTA_GATEWAY]), … … 153 142 abuf, sizeof(abuf))); 154 143 } else 155 fprintf(fp,"masquerade");144 printf("masquerade"); 156 145 } else if (r->rtm_type != RTN_UNICAST) 157 fp rintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));158 159 fputc('\n', fp);160 fflush(fp);146 fputs(rtnl_rtntype_n2a(r->rtm_type, b1), stdout); 147 148 bb_putchar('\n'); 149 /*fflush_all();*/ 161 150 return 0; 162 151 } 163 152 164 153 /* Return value becomes exitcode. It's okay to not return at all */ 165 static int iprule_list( int argc,char **argv)154 static int iprule_list(char **argv) 166 155 { 167 156 struct rtnl_handle rth; … … 171 160 af = AF_INET; 172 161 173 if ( argc > 0) {162 if (*argv) { 174 163 //bb_error_msg("\"rule show\" needs no arguments"); 175 bb_warn_ignoring_args( argc);164 bb_warn_ignoring_args(*argv); 176 165 return -1; 177 166 } … … 180 169 181 170 xrtnl_wilddump_request(&rth, af, RTM_GETRULE); 182 xrtnl_dump_filter(&rth, print_rule, stdout);171 xrtnl_dump_filter(&rth, print_rule, NULL); 183 172 184 173 return 0; … … 186 175 187 176 /* Return value becomes exitcode. It's okay to not return at all */ 188 static int iprule_modify(int cmd, int argc,char **argv)177 static int iprule_modify(int cmd, char **argv) 189 178 { 190 179 static const char keywords[] ALIGN1 = … … 200 189 struct rtnl_handle rth; 201 190 struct { 202 struct nlmsghdr 203 struct rtmsg 204 char 191 struct nlmsghdr n; 192 struct rtmsg r; 193 char buf[1024]; 205 194 } req; 206 195 smalluint key; … … 222 211 } 223 212 224 while ( argc > 0) {213 while (*argv) { 225 214 key = index_in_substrings(keywords, *argv) + 1; 226 215 if (key == 0) /* no match found in keywords array, bail out. */ … … 240 229 } else if (key == ARG_preference || 241 230 key == ARG_order || 242 key == ARG_priority) { 231 key == ARG_priority 232 ) { 243 233 uint32_t pref; 244 234 NEXT_ARG(); 245 if (get_u32(&pref, *argv, 0)) 246 invarg(*argv, "preference"); 235 pref = get_u32(*argv, "preference"); 247 236 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref); 248 237 } else if (key == ARG_tos) { … … 255 244 uint32_t fwmark; 256 245 NEXT_ARG(); 257 if (get_u32(&fwmark, *argv, 0)) 258 invarg(*argv, "fwmark"); 246 fwmark = get_u32(*argv, "fwmark"); 259 247 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark); 260 248 } else if (key == ARG_realms) { … … 265 253 addattr32(&req.n, sizeof(req), RTA_FLOW, realm); 266 254 } else if (key == ARG_table || 267 key == ARG_lookup) { 255 key == ARG_lookup 256 ) { 268 257 uint32_t tid; 269 258 NEXT_ARG(); … … 273 262 table_ok = 1; 274 263 } else if (key == ARG_dev || 275 key == ARG_iif) { 264 key == ARG_iif 265 ) { 276 266 NEXT_ARG(); 277 267 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1); 278 268 } else if (key == ARG_nat || 279 key == ARG_map_to) { 269 key == ARG_map_to 270 ) { 280 271 NEXT_ARG(); 281 272 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv)); … … 293 284 req.r.rtm_type = type; 294 285 } 295 argc--;296 286 argv++; 297 287 } … … 312 302 313 303 /* Return value becomes exitcode. It's okay to not return at all */ 314 int do_iprule(int argc,char **argv)304 int FAST_FUNC do_iprule(char **argv) 315 305 { 316 306 static const char ip_rule_commands[] ALIGN1 = 317 307 "add\0""delete\0""list\0""show\0"; 318 int cmd = 2; /* list */ 319 320 if (argc < 1) 321 return iprule_list(0, NULL); 322 if (*argv) 323 cmd = index_in_substrings(ip_rule_commands, *argv); 324 325 switch (cmd) { 326 case 0: /* add */ 327 cmd = RTM_NEWRULE; 328 break; 329 case 1: /* delete */ 330 cmd = RTM_DELRULE; 331 break; 332 case 2: /* list */ 333 case 3: /* show */ 334 return iprule_list(argc-1, argv+1); 335 break; 336 default: 337 bb_error_msg_and_die("unknown command %s", *argv); 338 } 339 return iprule_modify(cmd, argc-1, argv+1); 340 } 308 if (*argv) { 309 smalluint cmd = index_in_substrings(ip_rule_commands, *argv); 310 if (cmd > 3) 311 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); 312 argv++; 313 if (cmd < 2) 314 return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv); 315 } 316 return iprule_list(argv); 317 }
Note:
See TracChangeset
for help on using the changeset viewer.