owipcalc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * owipcalc - OpenWrt IP Calculator
  3. *
  4. * Copyright (C) 2012 Jo-Philipp Wich <[email protected]>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <arpa/inet.h>
  25. struct cidr {
  26. uint8_t family;
  27. uint32_t prefix;
  28. union {
  29. struct in_addr v4;
  30. struct in6_addr v6;
  31. } addr;
  32. union {
  33. char v4[sizeof("255.255.255.255/255.255.255.255 ")];
  34. char v6[sizeof("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255/128 ")];
  35. } buf;
  36. struct cidr *next;
  37. };
  38. struct op {
  39. const char *name;
  40. const char *desc;
  41. struct {
  42. bool (*a1)(struct cidr *a);
  43. bool (*a2)(struct cidr *a, struct cidr *b);
  44. } f4;
  45. struct {
  46. bool (*a1)(struct cidr *a);
  47. bool (*a2)(struct cidr *a, struct cidr *b);
  48. } f6;
  49. };
  50. static bool quiet = false;
  51. static bool printed = false;
  52. static struct cidr *stack = NULL;
  53. #define qprintf(...) \
  54. do { \
  55. if (!quiet) printf(__VA_ARGS__); \
  56. printed = true; \
  57. } while(0)
  58. static void cidr_push(struct cidr *a)
  59. {
  60. if (a)
  61. {
  62. a->next = stack;
  63. stack = a;
  64. }
  65. }
  66. static bool cidr_pop(struct cidr *a)
  67. {
  68. struct cidr *old = stack;
  69. if (old)
  70. {
  71. stack = stack->next;
  72. free(old);
  73. return true;
  74. }
  75. return false;
  76. }
  77. static struct cidr * cidr_clone(struct cidr *a)
  78. {
  79. struct cidr *b = malloc(sizeof(*b));
  80. if (!b)
  81. {
  82. fprintf(stderr, "out of memory\n");
  83. exit(255);
  84. }
  85. memcpy(b, a, sizeof(*b));
  86. cidr_push(b);
  87. return b;
  88. }
  89. static struct cidr * cidr_parse4(const char *s)
  90. {
  91. char *p = NULL, *r;
  92. struct in_addr mask;
  93. struct cidr *addr = malloc(sizeof(struct cidr));
  94. if (!addr || (strlen(s) >= sizeof(addr->buf.v4)))
  95. goto err;
  96. snprintf(addr->buf.v4, sizeof(addr->buf.v4), "%s", s);
  97. addr->family = AF_INET;
  98. if ((p = strchr(addr->buf.v4, '/')) != NULL)
  99. {
  100. *p++ = 0;
  101. if (strchr(p, '.') != NULL)
  102. {
  103. if (inet_pton(AF_INET, p, &mask) != 1)
  104. goto err;
  105. for (addr->prefix = 0; mask.s_addr; mask.s_addr >>= 1)
  106. addr->prefix += (mask.s_addr & 1);
  107. }
  108. else
  109. {
  110. addr->prefix = strtoul(p, &r, 10);
  111. if ((p == r) || (*r != 0) || (addr->prefix > 32))
  112. goto err;
  113. }
  114. }
  115. else
  116. {
  117. addr->prefix = 32;
  118. }
  119. if (p == addr->buf.v4+1)
  120. memset(&addr->addr.v4, 0, sizeof(addr->addr.v4));
  121. else if (inet_pton(AF_INET, addr->buf.v4, &addr->addr.v4) != 1)
  122. goto err;
  123. return addr;
  124. err:
  125. if (addr)
  126. free(addr);
  127. return NULL;
  128. }
  129. static bool cidr_add4(struct cidr *a, struct cidr *b)
  130. {
  131. uint32_t x = ntohl(a->addr.v4.s_addr);
  132. uint32_t y = ntohl(b->addr.v4.s_addr);
  133. struct cidr *n = cidr_clone(a);
  134. if ((n->family != AF_INET) || (b->family != AF_INET))
  135. return false;
  136. if ((uint32_t)(x + y) < x)
  137. {
  138. fprintf(stderr, "overflow during 'add'\n");
  139. return false;
  140. }
  141. n->addr.v4.s_addr = htonl(x + y);
  142. return true;
  143. }
  144. static bool cidr_sub4(struct cidr *a, struct cidr *b)
  145. {
  146. uint32_t x = ntohl(a->addr.v4.s_addr);
  147. uint32_t y = ntohl(b->addr.v4.s_addr);
  148. struct cidr *n = cidr_clone(a);
  149. if ((n->family != AF_INET) || (b->family != AF_INET))
  150. return false;
  151. if ((uint32_t)(x - y) > x)
  152. {
  153. fprintf(stderr, "underflow during 'sub'\n");
  154. return false;
  155. }
  156. n->addr.v4.s_addr = htonl(x - y);
  157. return true;
  158. }
  159. static bool cidr_network4(struct cidr *a)
  160. {
  161. struct cidr *n = cidr_clone(a);
  162. n->addr.v4.s_addr &= htonl(~((1 << (32 - n->prefix)) - 1));
  163. n->prefix = 32;
  164. return true;
  165. }
  166. static bool cidr_broadcast4(struct cidr *a)
  167. {
  168. struct cidr *n = cidr_clone(a);
  169. n->addr.v4.s_addr |= htonl(((1 << (32 - n->prefix)) - 1));
  170. n->prefix = 32;
  171. return true;
  172. }
  173. static bool cidr_contains4(struct cidr *a, struct cidr *b)
  174. {
  175. uint32_t net1 = a->addr.v4.s_addr & htonl(~((1 << (32 - a->prefix)) - 1));
  176. uint32_t net2 = b->addr.v4.s_addr & htonl(~((1 << (32 - a->prefix)) - 1));
  177. if (printed)
  178. qprintf(" ");
  179. if ((b->prefix >= a->prefix) && (net1 == net2))
  180. {
  181. qprintf("1");
  182. return true;
  183. }
  184. else
  185. {
  186. qprintf("0");
  187. return false;
  188. }
  189. }
  190. static bool cidr_netmask4(struct cidr *a)
  191. {
  192. struct cidr *n = cidr_clone(a);
  193. n->addr.v4.s_addr = htonl(~((1 << (32 - n->prefix)) - 1));
  194. n->prefix = 32;
  195. return true;
  196. }
  197. static bool cidr_private4(struct cidr *a)
  198. {
  199. uint32_t x = ntohl(a->addr.v4.s_addr);
  200. if (printed)
  201. qprintf(" ");
  202. if (((x >= 0x0A000000) && (x <= 0x0AFFFFFF)) ||
  203. ((x >= 0xAC100000) && (x <= 0xAC1FFFFF)) ||
  204. ((x >= 0xC0A80000) && (x <= 0xC0A8FFFF)))
  205. {
  206. qprintf("1");
  207. return true;
  208. }
  209. else
  210. {
  211. qprintf("0");
  212. return false;
  213. }
  214. }
  215. static bool cidr_linklocal4(struct cidr *a)
  216. {
  217. uint32_t x = ntohl(a->addr.v4.s_addr);
  218. if (printed)
  219. qprintf(" ");
  220. if ((x >= 0xA9FE0000) && (x <= 0xA9FEFFFF))
  221. {
  222. qprintf("1");
  223. return true;
  224. }
  225. else
  226. {
  227. qprintf("0");
  228. return false;
  229. }
  230. }
  231. static bool cidr_prev4(struct cidr *a, struct cidr *b)
  232. {
  233. struct cidr *n = cidr_clone(a);
  234. n->prefix = b->prefix;
  235. n->addr.v4.s_addr -= htonl(1 << (32 - b->prefix));
  236. return true;
  237. }
  238. static bool cidr_next4(struct cidr *a, struct cidr *b)
  239. {
  240. struct cidr *n = cidr_clone(a);
  241. n->prefix = b->prefix;
  242. n->addr.v4.s_addr += htonl(1 << (32 - b->prefix));
  243. return true;
  244. }
  245. static bool cidr_6to4(struct cidr *a)
  246. {
  247. struct cidr *n = cidr_clone(a);
  248. uint32_t x = a->addr.v4.s_addr;
  249. memset(&n->addr.v6.s6_addr, 0, sizeof(n->addr.v6.s6_addr));
  250. n->family = AF_INET6;
  251. n->prefix = 48;
  252. n->addr.v6.s6_addr[0] = 0x20;
  253. n->addr.v6.s6_addr[1] = 0x02;
  254. n->addr.v6.s6_addr[2] = (x >> 24);
  255. n->addr.v6.s6_addr[3] = (x >> 16) & 0xFF;
  256. n->addr.v6.s6_addr[4] = (x >> 8) & 0xFF;
  257. n->addr.v6.s6_addr[5] = x & 0xFF;
  258. return true;
  259. }
  260. static bool cidr_print4(struct cidr *a)
  261. {
  262. char *p;
  263. if (!a || (a->family != AF_INET))
  264. return false;
  265. if (!(p = (char *)inet_ntop(AF_INET, &a->addr.v4, a->buf.v4, sizeof(a->buf.v4))))
  266. return false;
  267. if (printed)
  268. qprintf(" ");
  269. qprintf("%s", p);
  270. if (a->prefix < 32)
  271. qprintf("/%u", a->prefix);
  272. cidr_pop(a);
  273. return true;
  274. }
  275. static struct cidr * cidr_parse6(const char *s)
  276. {
  277. char *p = NULL, *r;
  278. struct cidr *addr = malloc(sizeof(struct cidr));
  279. if (!addr || (strlen(s) >= sizeof(addr->buf.v6)))
  280. goto err;
  281. snprintf(addr->buf.v4, sizeof(addr->buf.v6), "%s", s);
  282. addr->family = AF_INET6;
  283. if ((p = strchr(addr->buf.v4, '/')) != NULL)
  284. {
  285. *p++ = 0;
  286. addr->prefix = strtoul(p, &r, 10);
  287. if ((p == r) || (*r != 0) || (addr->prefix > 128))
  288. goto err;
  289. }
  290. else
  291. {
  292. addr->prefix = 128;
  293. }
  294. if (p == addr->buf.v4+1)
  295. memset(&addr->addr.v6, 0, sizeof(addr->addr.v6));
  296. else if (inet_pton(AF_INET6, addr->buf.v4, &addr->addr.v6) != 1)
  297. goto err;
  298. return addr;
  299. err:
  300. if (addr)
  301. free(addr);
  302. return NULL;
  303. }
  304. static bool cidr_add6(struct cidr *a, struct cidr *b)
  305. {
  306. uint8_t idx = 15, carry = 0, overflow = 0;
  307. struct cidr *n = cidr_clone(a);
  308. struct in6_addr *x = &n->addr.v6;
  309. struct in6_addr *y = &b->addr.v6;
  310. if ((a->family != AF_INET6) || (b->family != AF_INET6))
  311. return false;
  312. do {
  313. overflow = !!((x->s6_addr[idx] + y->s6_addr[idx] + carry) >= 256);
  314. x->s6_addr[idx] += y->s6_addr[idx] + carry;
  315. carry = overflow;
  316. }
  317. while (idx-- > 0);
  318. if (carry)
  319. {
  320. fprintf(stderr, "overflow during 'add'\n");
  321. return false;
  322. }
  323. return true;
  324. }
  325. static bool cidr_sub6(struct cidr *a, struct cidr *b)
  326. {
  327. uint8_t idx = 15, carry = 0, underflow = 0;
  328. struct cidr *n = cidr_clone(a);
  329. struct in6_addr *x = &n->addr.v6;
  330. struct in6_addr *y = &b->addr.v6;
  331. if ((n->family != AF_INET6) || (b->family != AF_INET6))
  332. return false;
  333. do {
  334. underflow = !!((x->s6_addr[idx] - y->s6_addr[idx] - carry) < 0);
  335. x->s6_addr[idx] -= y->s6_addr[idx] + carry;
  336. carry = underflow;
  337. }
  338. while (idx-- > 0);
  339. if (carry)
  340. {
  341. fprintf(stderr, "underflow during 'sub'\n");
  342. return false;
  343. }
  344. return true;
  345. }
  346. static bool cidr_prev6(struct cidr *a, struct cidr *b)
  347. {
  348. uint8_t idx, carry = 1, underflow = 0;
  349. struct cidr *n = cidr_clone(a);
  350. struct in6_addr *x = &n->addr.v6;
  351. if (b->prefix == 0)
  352. {
  353. fprintf(stderr, "underflow during 'prev'\n");
  354. return false;
  355. }
  356. idx = (b->prefix - 1) / 8;
  357. do {
  358. underflow = !!((x->s6_addr[idx] - carry) < 0);
  359. x->s6_addr[idx] -= carry;
  360. carry = underflow;
  361. }
  362. while (idx-- > 0);
  363. if (carry)
  364. {
  365. fprintf(stderr, "underflow during 'prev'\n");
  366. return false;
  367. }
  368. n->prefix = b->prefix;
  369. return true;
  370. }
  371. static bool cidr_next6(struct cidr *a, struct cidr *b)
  372. {
  373. uint8_t idx, carry = 1, overflow = 0;
  374. struct cidr *n = cidr_clone(a);
  375. struct in6_addr *x = &n->addr.v6;
  376. if (b->prefix == 0)
  377. {
  378. fprintf(stderr, "overflow during 'next'\n");
  379. return false;
  380. }
  381. idx = (b->prefix - 1) / 8;
  382. do {
  383. overflow = !!((x->s6_addr[idx] + carry) >= 256);
  384. x->s6_addr[idx] += carry;
  385. carry = overflow;
  386. }
  387. while (idx-- > 0);
  388. if (carry)
  389. {
  390. fprintf(stderr, "overflow during 'next'\n");
  391. return false;
  392. }
  393. n->prefix = b->prefix;
  394. return true;
  395. }
  396. static bool cidr_network6(struct cidr *a)
  397. {
  398. uint8_t i;
  399. struct cidr *n = cidr_clone(a);
  400. for (i = 0; i < (128 - n->prefix) / 8; i++)
  401. n->addr.v6.s6_addr[15-i] = 0;
  402. if ((128 - n->prefix) % 8)
  403. n->addr.v6.s6_addr[15-i] &= ~((1 << ((128 - n->prefix) % 8)) - 1);
  404. return true;
  405. }
  406. static bool cidr_contains6(struct cidr *a, struct cidr *b)
  407. {
  408. struct cidr *n = cidr_clone(a);
  409. struct in6_addr *x = &n->addr.v6;
  410. struct in6_addr *y = &b->addr.v6;
  411. uint8_t i = (128 - n->prefix) / 8;
  412. uint8_t m = ~((1 << ((128 - n->prefix) % 8)) - 1);
  413. uint8_t net1 = x->s6_addr[15-i] & m;
  414. uint8_t net2 = y->s6_addr[15-i] & m;
  415. if (printed)
  416. qprintf(" ");
  417. if ((b->prefix >= n->prefix) && (net1 == net2) &&
  418. ((i == 15) || !memcmp(&x->s6_addr, &y->s6_addr, 15-i)))
  419. {
  420. qprintf("1");
  421. return true;
  422. }
  423. else
  424. {
  425. qprintf("0");
  426. return false;
  427. }
  428. }
  429. static bool cidr_linklocal6(struct cidr *a)
  430. {
  431. if (printed)
  432. qprintf(" ");
  433. if ((a->addr.v6.s6_addr[0] == 0xFE) &&
  434. (a->addr.v6.s6_addr[1] >= 0x80) &&
  435. (a->addr.v6.s6_addr[1] <= 0xBF))
  436. {
  437. qprintf("1");
  438. return true;
  439. }
  440. else
  441. {
  442. qprintf("0");
  443. return false;
  444. }
  445. }
  446. static bool cidr_ula6(struct cidr *a)
  447. {
  448. if (printed)
  449. qprintf(" ");
  450. if ((a->addr.v6.s6_addr[0] >= 0xFC) &&
  451. (a->addr.v6.s6_addr[0] <= 0xFD))
  452. {
  453. qprintf("1");
  454. return true;
  455. }
  456. else
  457. {
  458. qprintf("0");
  459. return false;
  460. }
  461. }
  462. static bool cidr_print6(struct cidr *a)
  463. {
  464. char *p;
  465. if (!a || (a->family != AF_INET6))
  466. return NULL;
  467. if (!(p = (char *)inet_ntop(AF_INET6, &a->addr.v6, a->buf.v6, sizeof(a->buf.v6))))
  468. return false;
  469. if (printed)
  470. qprintf(" ");
  471. qprintf("%s", p);
  472. if (a->prefix < 128)
  473. qprintf("/%u", a->prefix);
  474. cidr_pop(a);
  475. return true;
  476. }
  477. static struct cidr * cidr_parse(const char *op, const char *s, int af_hint)
  478. {
  479. char *r;
  480. struct cidr *a;
  481. uint8_t i;
  482. uint32_t sum = strtoul(s, &r, 0);
  483. if ((r > s) && (*r == 0))
  484. {
  485. a = malloc(sizeof(struct cidr));
  486. if (!a)
  487. return NULL;
  488. if (af_hint == AF_INET)
  489. {
  490. a->family = AF_INET;
  491. a->prefix = sum;
  492. a->addr.v4.s_addr = htonl(sum);
  493. }
  494. else
  495. {
  496. a->family = AF_INET6;
  497. a->prefix = sum;
  498. for (i = 0; i <= 15; i++)
  499. {
  500. a->addr.v6.s6_addr[15-i] = sum % 256;
  501. sum >>= 8;
  502. }
  503. }
  504. return a;
  505. }
  506. if (strchr(s, ':'))
  507. a = cidr_parse6(s);
  508. else
  509. a = cidr_parse4(s);
  510. if (!a)
  511. return NULL;
  512. if (a->family != af_hint)
  513. {
  514. fprintf(stderr, "attempt to '%s' %s with %s address\n",
  515. op,
  516. (af_hint == AF_INET) ? "ipv4" : "ipv6",
  517. (af_hint != AF_INET) ? "ipv4" : "ipv6");
  518. exit(4);
  519. }
  520. return a;
  521. }
  522. static bool cidr_howmany(struct cidr *a, struct cidr *b)
  523. {
  524. if (printed)
  525. qprintf(" ");
  526. if (b->prefix < a->prefix)
  527. qprintf("0");
  528. else
  529. qprintf("%u", 1 << (b->prefix - a->prefix));
  530. return true;
  531. }
  532. static bool cidr_prefix(struct cidr *a, struct cidr *b)
  533. {
  534. a->prefix = b->prefix;
  535. return true;
  536. }
  537. static bool cidr_quiet(struct cidr *a)
  538. {
  539. quiet = true;
  540. return true;
  541. }
  542. struct op ops[] = {
  543. { .name = "add",
  544. .desc = "Add argument to base address",
  545. .f4.a2 = cidr_add4,
  546. .f6.a2 = cidr_add6 },
  547. { .name = "sub",
  548. .desc = "Substract argument from base address",
  549. .f4.a2 = cidr_sub4,
  550. .f6.a2 = cidr_sub6 },
  551. { .name = "next",
  552. .desc = "Advance base address to next prefix of given size",
  553. .f4.a2 = cidr_next4,
  554. .f6.a2 = cidr_next6 },
  555. { .name = "prev",
  556. .desc = "Lower base address to previous prefix of give size",
  557. .f4.a2 = cidr_prev4,
  558. .f6.a2 = cidr_prev6 },
  559. { .name = "network",
  560. .desc = "Turn base address into network address",
  561. .f4.a1 = cidr_network4,
  562. .f6.a1 = cidr_network6 },
  563. { .name = "broadcast",
  564. .desc = "Turn base address into broadcast address",
  565. .f4.a1 = cidr_broadcast4 },
  566. { .name = "prefix",
  567. .desc = "Set the prefix of base address to argument",
  568. .f4.a2 = cidr_prefix,
  569. .f6.a2 = cidr_prefix },
  570. { .name = "netmask",
  571. .desc = "Calculate netmask of base address",
  572. .f4.a1 = cidr_netmask4 },
  573. { .name = "6to4",
  574. .desc = "Calculate 6to4 prefix of given ipv4-address",
  575. .f4.a1 = cidr_6to4 },
  576. { .name = "howmany",
  577. .desc = "Print amount of righ-hand prefixes that fit into base address",
  578. .f4.a2 = cidr_howmany,
  579. .f6.a2 = cidr_howmany },
  580. { .name = "contains",
  581. .desc = "Print '1' if argument fits into base address or '0' if not",
  582. .f4.a2 = cidr_contains4,
  583. .f6.a2 = cidr_contains6 },
  584. { .name = "private",
  585. .desc = "Print '1' if base address is in RFC1918 private space or '0' "
  586. "if not",
  587. .f4.a1 = cidr_private4 },
  588. { .name = "linklocal",
  589. .desc = "Print '1' if base address is in 169.254.0.0/16 or FE80::/10 "
  590. "link local space or '0' if not",
  591. .f4.a1 = cidr_linklocal4,
  592. .f6.a1 = cidr_linklocal6 },
  593. { .name = "ula",
  594. .desc = "Print '1' if base address is in FC00::/7 unique local address "
  595. "(ULA) space or '0' if not",
  596. .f6.a1 = cidr_ula6 },
  597. { .name = "quiet",
  598. .desc = "Suppress output, useful for test operation where the result can "
  599. "be inferred from the exit code",
  600. .f4.a1 = cidr_quiet,
  601. .f6.a1 = cidr_quiet },
  602. { .name = "pop",
  603. .desc = "Pop intermediate result from stack",
  604. .f4.a1 = cidr_pop,
  605. .f6.a1 = cidr_pop },
  606. { .name = "print",
  607. .desc = "Print intermediate result and pop it from stack, invoked "
  608. "implicitely at the end of calculation if no intermediate prints "
  609. "happened",
  610. .f4.a1 = cidr_print4,
  611. .f6.a1 = cidr_print6 },
  612. };
  613. static void usage(const char *prog)
  614. {
  615. int i;
  616. fprintf(stderr,
  617. "\n"
  618. "Usage:\n\n"
  619. " %s {base address} operation [argument] "
  620. "[operation [argument] ...]\n\n"
  621. "Operations:\n\n",
  622. prog);
  623. for (i = 0; i < sizeof(ops) / sizeof(ops[0]); i++)
  624. {
  625. if (ops[i].f4.a2 || ops[i].f6.a2)
  626. {
  627. fprintf(stderr, " %s %s\n",
  628. ops[i].name,
  629. (ops[i].f4.a2 && ops[i].f6.a2) ? "{ipv4/ipv6/amount}" :
  630. (ops[i].f6.a2 ? "{ipv6/amount}" : "{ipv4/amount}"));
  631. }
  632. else
  633. {
  634. fprintf(stderr, " %s\n", ops[i].name);
  635. }
  636. fprintf(stderr, " %s.\n", ops[i].desc);
  637. if ((ops[i].f4.a1 && ops[i].f6.a1) || (ops[i].f4.a2 && ops[i].f6.a2))
  638. fprintf(stderr, " Applicable to ipv4- and ipv6-addresses.\n\n");
  639. else if (ops[i].f6.a2 || ops[i].f6.a1)
  640. fprintf(stderr, " Only applicable to ipv6-addresses.\n\n");
  641. else
  642. fprintf(stderr, " Only applicable to ipv4-addresses.\n\n");
  643. }
  644. fprintf(stderr,
  645. "Examples:\n\n"
  646. " Calculate a DHCP range:\n\n"
  647. " $ %s 192.168.1.1/255.255.255.0 network add 100 print add 150 print\n"
  648. " 192.168.1.100\n"
  649. " 192.168.1.250\n\n"
  650. " Count number of prefixes:\n\n"
  651. " $ %s 2001:0DB8:FDEF::/48 howmany ::/64\n"
  652. " 65536\n\n",
  653. prog, prog);
  654. exit(1);
  655. }
  656. static bool runop(char ***arg, int *status)
  657. {
  658. int i;
  659. char *arg1 = **arg;
  660. char *arg2 = *(*arg+1);
  661. struct cidr *a = stack;
  662. struct cidr *b = NULL;
  663. if (!arg1)
  664. return false;
  665. for (i = 0; i < sizeof(ops) / sizeof(ops[0]); i++)
  666. {
  667. if (!strcmp(ops[i].name, arg1))
  668. {
  669. if (ops[i].f4.a2 || ops[i].f6.a2)
  670. {
  671. if (!arg2)
  672. {
  673. fprintf(stderr, "'%s' requires an argument\n",
  674. ops[i].name);
  675. *status = 2;
  676. return false;
  677. }
  678. b = cidr_parse(ops[i].name, arg2, a->family);
  679. if (!b)
  680. {
  681. fprintf(stderr, "invalid address argument for '%s'\n",
  682. ops[i].name);
  683. *status = 3;
  684. return false;
  685. }
  686. *arg += 2;
  687. if (((a->family == AF_INET) && !ops[i].f4.a2) ||
  688. ((a->family == AF_INET6) && !ops[i].f6.a2))
  689. {
  690. fprintf(stderr, "'%s' not supported for %s addresses\n",
  691. ops[i].name,
  692. (a->family == AF_INET) ? "ipv4" : "ipv6");
  693. *status = 5;
  694. return false;
  695. }
  696. *status = !((a->family == AF_INET) ? ops[i].f4.a2(a, b)
  697. : ops[i].f6.a2(a, b));
  698. return true;
  699. }
  700. else
  701. {
  702. *arg += 1;
  703. if (((a->family == AF_INET) && !ops[i].f4.a1) ||
  704. ((a->family == AF_INET6) && !ops[i].f6.a1))
  705. {
  706. fprintf(stderr, "'%s' not supported for %s addresses\n",
  707. ops[i].name,
  708. (a->family == AF_INET) ? "ipv4" : "ipv6");
  709. *status = 5;
  710. return false;
  711. }
  712. *status = !((a->family == AF_INET) ? ops[i].f4.a1(a)
  713. : ops[i].f6.a1(a));
  714. return true;
  715. }
  716. }
  717. }
  718. return false;
  719. }
  720. int main(int argc, char **argv)
  721. {
  722. int status = 0;
  723. char **arg = argv+2;
  724. struct cidr *a;
  725. if (argc < 3)
  726. usage(argv[0]);
  727. a = strchr(argv[1], ':') ? cidr_parse6(argv[1]) : cidr_parse4(argv[1]);
  728. if (!a)
  729. usage(argv[0]);
  730. cidr_push(a);
  731. while (runop(&arg, &status));
  732. if (*arg)
  733. {
  734. fprintf(stderr, "unknown operation '%s'\n", *arg);
  735. exit(6);
  736. }
  737. if (!printed && (status < 2))
  738. {
  739. if (stack->family == AF_INET)
  740. cidr_print4(stack);
  741. else
  742. cidr_print6(stack);
  743. }
  744. qprintf("\n");
  745. exit(status);
  746. }