You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inet.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "includes.h"
  19. #include "common.h"
  20. #include "ipv6-gmp.h"
  21. #include "libnetlink.h"
  22. #include "ll_map.h"
  23. #include "inet.h"
  24. #include "endianness.h"
  25. /*
  26. * inet_ntohl: Converts each element of `data' from network to host order. If
  27. * `family' is equal to AF_INET6, the array is swapped too (on big endian
  28. * machine).
  29. */
  30. void
  31. inet_ntohl(u_int * data, int family)
  32. {
  33. #if BYTE_ORDER == LITTLE_ENDIAN
  34. if (family == AF_INET) {
  35. data[0] = ntohl(data[0]);
  36. } else {
  37. int i;
  38. swap_ints(MAX_IP_INT, data, data);
  39. for (i = 0; i < MAX_IP_INT; i++)
  40. data[i] = ntohl(data[i]);
  41. }
  42. #endif
  43. }
  44. /*
  45. * inet_htonl: Converts each element of `data' from host to network order. If
  46. * `family' is equal to AF_INET6, the array is swapped too (on big endian
  47. * machine).
  48. */
  49. void
  50. inet_htonl(u_int * data, int family)
  51. {
  52. #if BYTE_ORDER == LITTLE_ENDIAN
  53. if (family == AF_INET) {
  54. data[0] = htonl(data[0]);
  55. } else {
  56. int i;
  57. swap_ints(MAX_IP_INT, data, data);
  58. for (i = 0; i < MAX_IP_INT; i++)
  59. data[i] = htonl(data[i]);
  60. }
  61. #endif
  62. }
  63. /*
  64. * inet_setip_raw: fills the `ip' inet_prefix struct with `data' and `family'.
  65. */
  66. int
  67. inet_setip_raw(inet_prefix * ip, u_int * data, int family)
  68. {
  69. ip->family = family;
  70. setzero(ip->data, sizeof(ip->data));
  71. if (family == AF_INET) {
  72. ip->data[0] = data[0];
  73. ip->len = 4;
  74. } else if (family == AF_INET6) {
  75. memcpy(ip->data, data, sizeof(ip->data));
  76. ip->len = 16;
  77. } else
  78. fatal(ERROR_MSG "family not supported", ERROR_POS);
  79. ip->bits = ip->len << 3; /* bits=len*8 */
  80. return 0;
  81. }
  82. /*
  83. * inet_setip: fills the `ip' inet_prefix struct with `data' and `family'.
  84. * Note that it does a network to host order conversion on `data'.
  85. */
  86. int
  87. inet_setip(inet_prefix * ip, u_int * data, int family)
  88. {
  89. inet_setip_raw(ip, data, family);
  90. inet_ntohl(ip->data, ip->family);
  91. return 0;
  92. }
  93. int
  94. inet_setip_bcast(inet_prefix * ip, int family)
  95. {
  96. if (family == AF_INET) {
  97. u_int data[MAX_IP_INT] = { 0, 0, 0, 0 };
  98. data[0] = INADDR_BROADCAST;
  99. inet_setip(ip, data, family);
  100. } else if (family == AF_INET6) {
  101. u_int data[MAX_IP_INT] = IPV6_ADDR_BROADCAST;
  102. inet_setip(ip, data, family);
  103. } else
  104. fatal(ERROR_MSG "family not supported", ERROR_POS);
  105. return 0;
  106. }
  107. int
  108. inet_setip_anyaddr(inet_prefix * ip, int family)
  109. {
  110. if (family == AF_INET) {
  111. u_int data[MAX_IP_INT] = { 0, 0, 0, 0 };
  112. data[0] = INADDR_ANY;
  113. inet_setip(ip, data, family);
  114. } else if (family == AF_INET6) {
  115. struct in6_addr ipv6 = IN6ADDR_ANY_INIT;
  116. inet_setip(ip, (u_int *) (&ipv6), family);
  117. } else
  118. fatal(ERROR_MSG "family not supported", ERROR_POS);
  119. return 0;
  120. }
  121. int
  122. inet_setip_loopback(inet_prefix * ip, int family)
  123. {
  124. if (family == AF_INET) {
  125. u_int data[MAX_IP_INT] = { 0, 0, 0, 0 };
  126. data[0] = LOOPBACK_IP;
  127. inet_setip(ip, data, family);
  128. inet_htonl(ip->data, ip->family);
  129. } else if (family == AF_INET6) {
  130. u_int data[MAX_IP_INT] = LOOPBACK_IPV6;
  131. inet_setip(ip, data, family);
  132. } else
  133. fatal(ERROR_MSG "family not supported", ERROR_POS);
  134. return 0;
  135. }
  136. /*
  137. * inet_setip_localaddr: Restrict the `ip' to a local private class changing the
  138. * first byte of the `ip'. `class' specifies what restricted class is currently
  139. * being used (10.x.x.x or 172.16.x.x). In ipv6 the site local class is the
  140. * default.
  141. */
  142. int
  143. inet_setip_localaddr(inet_prefix * ip, int family, int class)
  144. {
  145. if (family == AF_INET) {
  146. if (class == RESTRICTED_10)
  147. ip->data[0] = NTK_RESTRICTED_10_MASK(ip->data[0]);
  148. else
  149. ip->data[0] = NTK_RESTRICTED_172_MASK(ip->data[0]);
  150. } else if (family == AF_INET6) {
  151. ip->data[0] = NTK_RESTRICTED_IPV6_MASK(ip->data[0]);
  152. } else
  153. fatal(ERROR_MSG "family not supported", ERROR_POS);
  154. return 0;
  155. }
  156. /*
  157. * inet_is_ip_local: verifies if `ip' is a local address. If it is, 1 is
  158. * returned. `class' specifies what restricted class is currently
  159. * being used (10.x.x.x or 172.16.x.x). In ipv6 the site local class is the
  160. * default.
  161. */
  162. int
  163. inet_is_ip_local(inet_prefix * ip, int class)
  164. {
  165. if (ip->family == AF_INET) {
  166. if (class == RESTRICTED_10)
  167. return ip->data[0] == NTK_RESTRICTED_10_MASK(ip->data[0]);
  168. else
  169. return ip->data[0] == NTK_RESTRICTED_172_MASK(ip->data[0]);
  170. } else if (ip->family == AF_INET6)
  171. return ip->data[0] == NTK_RESTRICTED_IPV6_MASK(ip->data[0]);
  172. else
  173. fatal(ERROR_MSG "family not supported", ERROR_POS);
  174. return 0;
  175. }
  176. void
  177. inet_copy(inet_prefix * dst, inet_prefix * src)
  178. {
  179. memcpy(dst, src, sizeof(inet_prefix));
  180. }
  181. /*
  182. * inet_copy_ipdata_raw: copies `ip'->data in `dst_data'.
  183. */
  184. void
  185. inet_copy_ipdata_raw(u_int * dst_data, inet_prefix * ip)
  186. {
  187. memcpy(dst_data, ip->data, MAX_IP_SZ);
  188. }
  189. /*
  190. * inet_copy_ipdata: copies `ip'->data in `dst_data' and converts it in network
  191. * order.
  192. */
  193. void
  194. inet_copy_ipdata(u_int * dst_data, inet_prefix * ip)
  195. {
  196. inet_prefix tmp_ip;
  197. inet_copy(&tmp_ip, ip);
  198. inet_htonl(tmp_ip.data, tmp_ip.family);
  199. memcpy(dst_data, tmp_ip.data, MAX_IP_SZ);
  200. }
  201. /*
  202. * pack_inet_prefix: packs the `ip' inet_prefix struct and stores it in
  203. * `pack', which must be INET_PREFIX_PACK_SZ bytes big. `pack' will be in
  204. * network order.
  205. */
  206. void
  207. pack_inet_prefix(inet_prefix * ip, char *pack)
  208. {
  209. char *buf;
  210. buf = pack;
  211. memcpy(buf, &ip->family, sizeof(u_char));
  212. buf += sizeof(u_char);
  213. memcpy(buf, &ip->len, sizeof(u_short));
  214. buf += sizeof(u_short);
  215. memcpy(buf, &ip->bits, sizeof(u_char));
  216. buf += sizeof(u_char);
  217. memcpy(buf, ip->data, MAX_IP_SZ);
  218. inet_htonl((u_int *) buf, ip->family);
  219. buf += MAX_IP_SZ;
  220. ints_host_to_network(pack, inet_prefix_iinfo);
  221. }
  222. /*
  223. * unpack_inet_prefix: restores in `ip' the inet_prefix struct contained in `pack'.
  224. * Note that `pack' will be modified during the restoration.
  225. */
  226. void
  227. unpack_inet_prefix(inet_prefix * ip, char *pack)
  228. {
  229. char *buf;
  230. buf = pack;
  231. ints_network_to_host(pack, inet_prefix_iinfo);
  232. memcpy(&ip->family, buf, sizeof(u_char));
  233. buf += sizeof(u_char);
  234. memcpy(&ip->len, buf, sizeof(u_short));
  235. buf += sizeof(u_short);
  236. memcpy(&ip->bits, buf, sizeof(u_char));
  237. buf += sizeof(u_char);
  238. memcpy(ip->data, buf, MAX_IP_SZ);
  239. inet_ntohl(ip->data, ip->family);
  240. buf += MAX_IP_SZ;
  241. }
  242. /*
  243. * inet_addr_match: without hesitating this function was robbed from iproute2.
  244. * It compares a->data wih b->data matching `bits'# bits.
  245. */
  246. int
  247. inet_addr_match(const inet_prefix * a, const inet_prefix * b, int bits)
  248. {
  249. const uint32_t *a1 = a->data;
  250. const uint32_t *a2 = b->data;
  251. int words = bits >> 0x05;
  252. bits &= 0x1f;
  253. if (words)
  254. if (memcmp(a1, a2, words << 2))
  255. return -1;
  256. if (bits) {
  257. uint32_t w1, w2;
  258. uint32_t mask;
  259. w1 = a1[words];
  260. w2 = a2[words];
  261. mask = htonl((0xffffffff) << (0x20 - bits));
  262. if ((w1 ^ w2) & mask)
  263. return 1;
  264. }
  265. return 0;
  266. }
  267. int
  268. ipv6_addr_type(inet_prefix addr)
  269. {
  270. int type;
  271. u_int st;
  272. st = htonl(addr.data[0]);
  273. if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
  274. type = IPV6_ADDR_MULTICAST;
  275. switch ((st & htonl(0x00FF0000))) {
  276. case __constant_htonl(0x00010000):
  277. type |= IPV6_ADDR_LOOPBACK;
  278. break;
  279. case __constant_htonl(0x00020000):
  280. type |= IPV6_ADDR_LINKLOCAL;
  281. break;
  282. case __constant_htonl(0x00050000):
  283. type |= IPV6_ADDR_SITELOCAL;
  284. break;
  285. };
  286. return type;
  287. }
  288. type = IPV6_ADDR_UNICAST;
  289. /* Consider all addresses with the first three bits different of
  290. 000 and 111 as finished.
  291. */
  292. if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
  293. (st & htonl(0xE0000000)) != htonl(0xE0000000))
  294. return type;
  295. if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
  296. return (IPV6_ADDR_LINKLOCAL | type);
  297. if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
  298. return (IPV6_ADDR_SITELOCAL | type);
  299. if ((addr.data[0] | addr.data[1]) == 0) {
  300. if (addr.data[2] == 0) {
  301. if (addr.data[3] == 0)
  302. return IPV6_ADDR_ANY;
  303. if (htonl(addr.data[3]) == htonl(0x00000001))
  304. return (IPV6_ADDR_LOOPBACK | type);
  305. return (IPV6_ADDR_COMPATv4 | type);
  306. }
  307. if (htonl(addr.data[2]) == htonl(0x0000ffff))
  308. return IPV6_ADDR_MAPPED;
  309. }
  310. st &= htonl(0xFF000000);
  311. if (st == 0)
  312. return IPV6_ADDR_RESERVED;
  313. st &= htonl(0xFE000000);
  314. if (st == htonl(0x02000000))
  315. return IPV6_ADDR_RESERVED; /* for NSAP */
  316. if (st == htonl(0x04000000))
  317. return IPV6_ADDR_RESERVED; /* for IPX */
  318. return type;
  319. }
  320. /*
  321. * inet_validate_ip: returns 0 is `ip' a valid IP which can be set by
  322. * Netsukuku to a network interface
  323. */
  324. int
  325. inet_validate_ip(inet_prefix ip)
  326. {
  327. int type, ipv4;
  328. if (ip.family == AF_INET) {
  329. ipv4 = htonl(ip.data[0]);
  330. if (MULTICAST(ipv4) || BADCLASS(ipv4) || ZERONET(ipv4)
  331. || LOOPBACK(ipv4) || NTK_PRIVATE_C(ipv4) ||
  332. (!restricted_mode && NTK_PRIVATE_B(ipv4)))
  333. return -EINVAL;
  334. } else if (ip.family == AF_INET6) {
  335. type = ipv6_addr_type(ip);
  336. if ((type & IPV6_ADDR_MULTICAST) || (type & IPV6_ADDR_RESERVED) ||
  337. (type & IPV6_ADDR_LOOPBACK))
  338. return -EINVAL;
  339. }
  340. if (is_bufzero((char *) ip.data, MAX_IP_SZ))
  341. return -EINVAL;
  342. return 0;
  343. }
  344. /*\
  345. *
  346. * * * Conversion functions... * *
  347. *
  348. \*/
  349. /*
  350. * ipraw_to_str: It returns the string which represents the given ip in host
  351. * order.
  352. */
  353. const char *
  354. ipraw_to_str(u_int ip[MAX_IP_INT], int family)
  355. {
  356. struct in_addr src;
  357. struct in6_addr src6;
  358. static char dst[INET_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN];
  359. if (family == AF_INET) {
  360. src.s_addr = htonl(ip[0]);
  361. inet_ntop(family, &src, dst, INET_ADDRSTRLEN);
  362. return dst;
  363. } else if (family == AF_INET6) {
  364. inet_htonl(ip, family);
  365. memcpy(&src6, ip, MAX_IP_SZ);
  366. inet_ntop(family, &src6, dst6, INET6_ADDRSTRLEN);
  367. return dst6;
  368. }
  369. return 0;
  370. }
  371. /*
  372. * inet_to_str: returns the string representation of `ip'
  373. */
  374. const char *
  375. inet_to_str(inet_prefix ip)
  376. {
  377. return ipraw_to_str(ip.data, ip.family);
  378. }
  379. /*
  380. * str_to_inet: it converts the IP address string contained in `src' and
  381. * terminated by a `\0' char to an inet_prefix struct. The result is stored in
  382. * `ip'. On error -1 is returned.
  383. */
  384. int
  385. str_to_inet(const char *src, inet_prefix * ip)
  386. {
  387. struct in_addr dst;
  388. struct in6_addr dst6;
  389. int family, res;
  390. u_int *data;
  391. setzero(ip, sizeof(inet_prefix));
  392. if (strstr(src, ":")) {
  393. family = AF_INET6;
  394. data = (u_int *) & dst6;
  395. } else {
  396. family = AF_INET;
  397. data = (u_int *) & dst;
  398. }
  399. if ((res = inet_pton(family, src, (void *) data)) < 0) {
  400. debug(DBG_NORMAL, ERROR_MSG "error -> %s.",
  401. ERROR_FUNC, strerror(errno));
  402. return -1;
  403. }
  404. if (!res) {
  405. debug(DBG_NORMAL, ERROR_MSG "impossible to convert \"%s\":"
  406. " invalid address.", ERROR_FUNC, src);
  407. return -1;
  408. }
  409. inet_setip(ip, data, family);
  410. return 0;
  411. }
  412. /*
  413. * inet_to_sockaddr: Converts a inet_prefix struct to a sockaddr struct
  414. */
  415. int
  416. inet_to_sockaddr(inet_prefix * ip, u_short port, struct sockaddr *dst,
  417. socklen_t * dstlen)
  418. {
  419. port = htons(port);
  420. if (ip->family == AF_INET) {
  421. struct sockaddr_in sin;
  422. setzero(&sin, sizeof(struct sockaddr_in));
  423. sin.sin_family = ip->family;
  424. sin.sin_port = port;
  425. sin.sin_addr.s_addr = htonl(ip->data[0]);
  426. memcpy(dst, &sin, sizeof(struct sockaddr_in));
  427. if (dstlen)
  428. *dstlen = sizeof(struct sockaddr_in);
  429. } else if (ip->family == AF_INET6) {
  430. struct sockaddr_in6 sin6;
  431. setzero(&sin6, sizeof(struct sockaddr_in6));
  432. sin6.sin6_family = ip->family;
  433. sin6.sin6_port = port;
  434. sin6.sin6_flowinfo = 0;
  435. memcpy(&sin6.sin6_addr, ip->data, MAX_IP_SZ);
  436. inet_htonl((u_int *) & sin6.sin6_addr, ip->family);
  437. memcpy(dst, &sin6, sizeof(struct sockaddr_in6));
  438. if (dstlen)
  439. *dstlen = sizeof(struct sockaddr_in6);
  440. } else
  441. fatal(ERROR_MSG "family not supported", ERROR_POS);
  442. return 0;
  443. }
  444. int
  445. sockaddr_to_inet(struct sockaddr *ip, inet_prefix * dst, u_short * port)
  446. {
  447. u_short po;
  448. char *p;
  449. setzero(dst, sizeof(inet_prefix));
  450. dst->family = ip->sa_family;
  451. memcpy(&po, &ip->sa_data, sizeof(u_short));
  452. if (port)
  453. *port = ntohs(po);
  454. if (ip->sa_family == AF_INET)
  455. p = (char *) ip->sa_data + sizeof(u_short);
  456. else if (ip->sa_family == AF_INET6)
  457. p = (char *) ip->sa_data + sizeof(u_short) + sizeof(int);
  458. else {
  459. error(ERROR_MSG "family not supported", ERROR_POS);
  460. return -1;
  461. }
  462. inet_setip(dst, (u_int *) p, ip->sa_family);
  463. return 0;
  464. }
  465. /*\
  466. *
  467. * * * Socket operations * *
  468. *
  469. \*/
  470. int
  471. new_socket(int sock_type)
  472. {
  473. int sockfd;
  474. if ((sockfd = socket(sock_type, SOCK_STREAM, 0)) == -1) {
  475. error("Socket SOCK_STREAM creation failed: %s", strerror(errno));
  476. return -1;
  477. }
  478. return sockfd;
  479. }
  480. int
  481. new_dgram_socket(int sock_type)
  482. {
  483. int sockfd;
  484. if ((sockfd = socket(sock_type, SOCK_DGRAM, 0)) == -1) {
  485. error("Socket SOCK_DGRAM creation failed: %s", strerror(errno));
  486. return -1;
  487. }
  488. return sockfd;
  489. }
  490. /*
  491. * inet_close
  492. *
  493. * It closes the `*sk' socket and sets it to zero.
  494. * It always returns 0;
  495. */
  496. int
  497. inet_close(int *sk)
  498. {
  499. close(*sk);
  500. return (*sk = 0);
  501. }
  502. int
  503. inet_getpeername(int sk, inet_prefix * ip, u_short *port)
  504. {
  505. struct sockaddr_storage saddr_sto;
  506. struct sockaddr *sa = (struct sockaddr *) &saddr_sto;
  507. socklen_t alen;
  508. alen = sizeof(saddr_sto);
  509. setzero(sa, alen);
  510. if (getpeername(sk, sa, &alen) == -1) {
  511. error("Cannot getpeername: %s", strerror(errno));
  512. return -1;
  513. }
  514. return sockaddr_to_inet(sa, ip, port);
  515. }
  516. /*
  517. * join_ipv6_multicast: It adds the membership to the IPV6_ADDR_BROADCAST
  518. * multicast group. The device with index `idx' will be used.
  519. */
  520. int
  521. join_ipv6_multicast(int socket, int idx)
  522. {
  523. struct ipv6_mreq mreq6;
  524. const int addr[MAX_IP_INT] = IPV6_ADDR_BROADCAST;
  525. setzero(&mreq6, sizeof(struct ipv6_mreq));
  526. memcpy(&mreq6.ipv6mr_multiaddr, addr, sizeof(struct in6_addr));
  527. mreq6.ipv6mr_interface = idx;
  528. if (setsockopt(socket, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
  529. sizeof(mreq6)) < 0) {
  530. error("Cannot set IPV6_JOIN_GROUP: %s", strerror(errno));
  531. close(socket);
  532. return -1;
  533. }
  534. return socket;
  535. }
  536. int
  537. set_multicast_if(int socket, int idx)
  538. {
  539. /* man ipv6 */
  540. if (setsockopt(socket, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  541. &idx, sizeof(int)) < 0) {
  542. error("set_multicast_if(): cannot set IPV6_MULTICAST_IF: %s",
  543. strerror(errno));
  544. close(socket);
  545. return -1;
  546. }
  547. return 0;
  548. }
  549. int
  550. set_nonblock_sk(int fd)
  551. {
  552. if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
  553. error("set_nonblock_sk(): cannot set O_NONBLOCK: %s",
  554. strerror(errno));
  555. close(fd);
  556. return -1;
  557. }
  558. return 0;
  559. }
  560. int
  561. unset_nonblock_sk(int fd)
  562. {
  563. if (fcntl(fd, F_SETFL, 0) < 0) {
  564. error("unset_nonblock_sk(): cannot unset O_NONBLOCK: %s",
  565. strerror(errno));
  566. close(fd);
  567. return -1;
  568. }
  569. return 0;
  570. }
  571. int
  572. set_reuseaddr_sk(int socket)
  573. {
  574. int reuseaddr = 1, ret;
  575. /*
  576. * SO_REUSEADDR: <<Go ahead and reuse that port even if it is in
  577. * TIME_WAIT state.>>
  578. */
  579. ret =
  580. setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
  581. sizeof(int));
  582. if (ret < 0)
  583. error("setsockopt SO_REUSEADDR: %s", strerror(errno));
  584. return ret;
  585. }
  586. int
  587. set_bindtodevice_sk(int socket, char *dev)
  588. {
  589. struct ifreq ifr;
  590. int ret = 0;
  591. setzero(&ifr, sizeof(ifr));
  592. strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1);
  593. ret =
  594. setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, dev,
  595. strlen(dev) + 1);
  596. if (ret < 0)
  597. error("setsockopt SO_BINDTODEVICE: %s", strerror(errno));
  598. return ret;
  599. }
  600. /*
  601. * `loop': 0 = disable, 1 = enable (default)
  602. */
  603. int
  604. set_multicast_loop_sk(int family, int socket, u_char loop)
  605. {
  606. int ret = 0;
  607. /*
  608. * <<The IPV6_MULTICAST_LOOP option gives the sender explicit control
  609. * over whether or not subsequent datagrams are looped bac.>>
  610. */
  611. if (family == AF_INET6)
  612. ret =
  613. setsockopt(socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop,
  614. sizeof(loop));
  615. if (ret < 0)
  616. error("setsockopt IP_MULTICAST_LOOP: %s", strerror(errno));
  617. return ret;
  618. }
  619. int
  620. set_broadcast_sk(int socket, int family, inet_prefix * host, short port,
  621. int dev_idx)
  622. {
  623. struct sockaddr_storage saddr_sto;
  624. struct sockaddr *sa = (struct sockaddr *) &saddr_sto;
  625. socklen_t alen;
  626. int broadcast = 1;
  627. if (family == AF_INET) {
  628. if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, &broadcast,
  629. sizeof(broadcast)) < 0) {
  630. error("Cannot set SO_BROADCAST to socket: %s",
  631. strerror(errno));
  632. close(socket);
  633. return -1;
  634. }
  635. } else if (family == AF_INET6) {
  636. if (join_ipv6_multicast(socket, dev_idx) < 0)
  637. return -1;
  638. if (set_multicast_loop_sk(family, socket, 0) < 0)
  639. return -1;
  640. set_multicast_if(socket, dev_idx);
  641. } else
  642. fatal(ERROR_MSG "family not supported", ERROR_POS);
  643. /* What's my name ? */
  644. alen = sizeof(saddr_sto);
  645. setzero(sa, alen);
  646. if (getsockname(socket, sa, &alen) == -1) {
  647. error("Cannot getsockname: %s", strerror(errno));
  648. close(socket);
  649. return -1;
  650. }
  651. /* Let's bind it! */
  652. if (bind(socket, sa, alen) < 0) {
  653. error("Cannot bind the broadcast socket: %s", strerror(errno));
  654. close(socket);
  655. return -1;
  656. }
  657. return socket;
  658. }
  659. int
  660. unset_broadcast_sk(int socket, int family)
  661. {
  662. int broadcast = 0;
  663. if (family == AF_INET) {
  664. if (setsockopt
  665. (socket, SOL_SOCKET, SO_BROADCAST, &broadcast,
  666. sizeof(broadcast)) < 0) {
  667. error("Cannot unset broadcasting: %s", strerror(errno));
  668. return -1;
  669. }
  670. }
  671. return 0;
  672. }
  673. int
  674. set_keepalive_sk(int socket)
  675. {
  676. int on = 1;
  677. if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (void *) &on,
  678. sizeof(on)) < 0) {
  679. error("Cannot set keepalive socket: %s", strerror(errno));
  680. return -1;
  681. }
  682. return 0;
  683. }
  684. int
  685. unset_keepalive_sk(int socket)
  686. {
  687. int off = 0;
  688. if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (void *) &off,
  689. sizeof(off)) < 0) {
  690. error("Cannot unset keepalive socket: %s", strerror(errno));
  691. return -1;
  692. }
  693. return 0;
  694. }
  695. int
  696. set_tos_sk(int socket, int lowdelay)
  697. {
  698. int tos = lowdelay ? IPTOS_LOWDELAY : IPTOS_THROUGHPUT;
  699. /* Only for Ipv4 */
  700. if (setsockopt(socket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) {
  701. error("setsockopt IP_TOS %d: %s", tos, strerror(errno));
  702. return -1;
  703. }
  704. return 0;
  705. }
  706. /*\
  707. *
  708. * * * Connection functions * *
  709. *
  710. \*/
  711. int
  712. new_tcp_conn(inet_prefix * host, short port, char *dev)
  713. {
  714. int sk;
  715. socklen_t sa_len;
  716. struct sockaddr_storage saddr_sto;
  717. struct sockaddr *sa = (struct sockaddr *) &saddr_sto;
  718. const char *ntop;
  719. ntop = inet_to_str(*host);
  720. if (inet_to_sockaddr(host, port, sa, &sa_len)) {
  721. error("Cannot new_tcp_connect(): %d Family not supported",
  722. host->family);
  723. ERROR_FINISH(sk, -1, finish);
  724. }
  725. if ((sk = new_socket(host->family)) == -1)
  726. ERROR_FINISH(sk, -1, finish);
  727. if (dev) /* if `dev' is not null bind the socket to it */
  728. if (set_bindtodevice_sk(sk, dev) < 0)
  729. ERROR_FINISH(sk, -1, finish);
  730. if (connect(sk, sa, sa_len) == -1) {
  731. error("Cannot tcp_connect() to %s: %s", ntop, strerror(errno));
  732. ERROR_FINISH(sk, -1, finish);
  733. }
  734. finish:
  735. return sk;
  736. }
  737. int
  738. new_udp_conn(inet_prefix * host, short port, char *dev)
  739. {
  740. int sk;
  741. socklen_t sa_len;
  742. struct sockaddr_storage saddr_sto;
  743. struct sockaddr *sa = (struct sockaddr *) &saddr_sto;
  744. const char *ntop;
  745. ntop = inet_to_str(*host);
  746. if (inet_to_sockaddr(host, port, sa, &sa_len)) {
  747. error("Cannot new_udp_connect(): %d Family not supported",
  748. host->family);
  749. ERROR_FINISH(sk, -1, finish);
  750. }
  751. if ((sk = new_dgram_socket(host->family)) == -1)
  752. ERROR_FINISH(sk, -1, finish);
  753. if (dev) /* if `dev' is not null bind the socket to it */
  754. if (set_bindtodevice_sk(sk, dev) < 0)
  755. ERROR_FINISH(sk, -1, finish);
  756. if (connect(sk, sa, sa_len) == -1) {
  757. error("Cannot connect to %s: %s", ntop, strerror(errno));
  758. ERROR_FINISH(sk, -1, finish);
  759. }
  760. finish:
  761. return sk;
  762. }
  763. int
  764. new_bcast_conn(inet_prefix * host, short port, int dev_idx)
  765. {
  766. struct sockaddr_storage saddr_sto;
  767. struct sockaddr *sa = (struct sockaddr *) &saddr_sto;
  768. socklen_t alen;
  769. int sk;
  770. const char *ntop;
  771. if ((sk = new_dgram_socket(host->family)) == -1)
  772. return -1;
  773. sk = set_broadcast_sk(sk, host->family, host, port, dev_idx);
  774. /*
  775. * Connect
  776. */
  777. if (inet_to_sockaddr(host, port, sa, &alen)) {
  778. error("set_broadcast_sk: %d Family not supported", host->family);
  779. return -1;
  780. }
  781. if (host->family == AF_INET6) {
  782. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
  783. sin6->sin6_scope_id = dev_idx;
  784. }
  785. if (set_bindtodevice_sk(sk, (char *) ll_index_to_name(dev_idx)) < 0)
  786. return -1;
  787. if (connect(sk, sa, alen) == -1) {
  788. ntop = inet_to_str(*host);
  789. error("Cannot connect to the broadcast (%s): %s", ntop,
  790. strerror(errno));
  791. return -1;
  792. }
  793. return sk;
  794. }
  795. /*\
  796. *
  797. * * * Recv/Send functions * *
  798. *
  799. \*/
  800. ssize_t
  801. inet_recv(int s, void *buf, size_t len, int flags)
  802. {
  803. ssize_t err;
  804. if ((err = recv(s, buf, len, flags)) == -1) {
  805. switch (errno) {
  806. default:
  807. /* Probably connection was closed */
  808. debug(DBG_NORMAL, "inet_recv: Cannot recv(): %s",
  809. strerror(errno));
  810. return err;
  811. break;
  812. }
  813. }
  814. return err;
  815. }
  816. /*
  817. * inet_recv_timeout
  818. *
  819. * is the same as inet_recv() but if no reply is received for `timeout'
  820. * seconds it returns -1.
  821. */
  822. ssize_t
  823. inet_recv_timeout(int s, void *buf, size_t len, int flags, u_int timeout)
  824. {
  825. struct timeval timeout_t;
  826. fd_set fdset;
  827. int ret;
  828. MILLISEC_TO_TV(timeout * 1000, timeout_t);
  829. FD_ZERO(&fdset);
  830. FD_SET(s, &fdset);
  831. ret = select(s + 1, &fdset, NULL, NULL, &timeout_t);
  832. if (ret == -1) {
  833. error(ERROR_MSG "select error: %s", ERROR_FUNC, strerror(errno));
  834. return ret;
  835. }
  836. return FD_ISSET(s, &fdset) ? inet_recv(s, buf, len, flags) : -1;
  837. }
  838. ssize_t
  839. inet_recvfrom(int s, void *buf, size_t len, int flags,
  840. struct sockaddr * from, socklen_t * fromlen)
  841. {
  842. ssize_t err;
  843. if ((err = recvfrom(s, buf, len, flags, from, fromlen)) < 0) {
  844. switch (errno) {
  845. default:
  846. error("inet_recvfrom: Cannot recv(): %s", strerror(errno));
  847. return err;
  848. break;
  849. }
  850. }
  851. return err;
  852. }
  853. /*
  854. * inet_recvfrom_timeout: is the same as inet_recvfrom() but if no reply is
  855. * received for `timeout' seconds it returns -1.
  856. */
  857. ssize_t
  858. inet_recvfrom_timeout(int s, void *buf, size_t len, int flags,
  859. struct sockaddr * from, socklen_t * fromlen,
  860. u_int timeout)
  861. {
  862. struct timeval timeout_t;
  863. fd_set fdset;
  864. int ret;
  865. MILLISEC_TO_TV(timeout * 1000, timeout_t);
  866. FD_ZERO(&fdset);
  867. FD_SET(s, &fdset);
  868. ret = select(s + 1, &fdset, NULL, NULL, &timeout_t);
  869. if (ret == -1) {
  870. error(ERROR_MSG "select error: %s", ERROR_FUNC, strerror(errno));
  871. return ret;
  872. }
  873. if (FD_ISSET(s, &fdset))
  874. return inet_recvfrom(s, buf, len, flags, from, fromlen);
  875. return -1;
  876. }
  877. ssize_t
  878. inet_send(int s, const void *msg, size_t len, int flags)
  879. {
  880. ssize_t err;
  881. if((err=send(s, msg, len, flags)) < 0) {
  882. switch(errno)
  883. {
  884. /* This divides the length of a packet if it is too large to send,
  885. * it then sends the first half, And then the second half.
  886. * If it is too large to send again, When it tries to send the first half
  887. * it will just come back here to repeat the process as needed. */
  888. case EMSGSIZE:
  889. inet_send(s, msg, len/2, flags);
  890. err = inet_send(s, msg+(len/2), (len+1)/2, flags);
  891. printf("%lu", err);
  892. break;
  893. default:
  894. error("inet_send: Cannot send(): %s", strerror(errno));
  895. return err;
  896. break;
  897. }
  898. }
  899. return err;
  900. }
  901. /*
  902. * inet_send_timeout: is the same as inet_send() but if the packet isn't sent
  903. * in `timeout' seconds it timeouts and returns -1.
  904. */
  905. ssize_t
  906. inet_send_timeout(int s, const void *msg, size_t len, int flags,
  907. u_int timeout)
  908. {
  909. struct timeval timeout_t;
  910. fd_set fdset;
  911. int ret;
  912. MILLISEC_TO_TV(timeout * 1000, timeout_t);
  913. FD_ZERO(&fdset);
  914. FD_SET(s, &fdset);
  915. ret = select(s + 1, NULL, &fdset, NULL, &timeout_t);
  916. if (ret == -1) {
  917. error(ERROR_MSG "select error: %s", ERROR_FUNC, strerror(errno));
  918. return ret;
  919. }
  920. if (FD_ISSET(s, &fdset))
  921. return inet_send(s, msg, len, flags);
  922. return -1;
  923. }
  924. ssize_t
  925. inet_sendto(int s, const void *msg, size_t len, int flags,
  926. const struct sockaddr * to, socklen_t tolen)
  927. {
  928. ssize_t err;
  929. if ((err = sendto(s, msg, len, flags, to, tolen)) == -1) {
  930. switch (errno) {
  931. case EMSGSIZE:
  932. inet_sendto(s, msg, len/2, flags, to, tolen);
  933. err = inet_sendto(s, msg+(len/2), (len+1)/2, flags, to, tolen);
  934. printf("%lu", err);
  935. break;
  936. case EFAULT:
  937. /* Must be modified to accept IPv6 addresses
  938. * when IPv6 support is activated. */
  939. error("Bad Address\n"
  940. "To Family is: %hu "
  941. "To Data is: %s",
  942. to->sa_family,
  943. inet_ntoa( ( ((struct sockaddr_in*)to)->sin_addr ) ));
  944. default:
  945. error("inet_sendto: Cannot send(): %s", strerror(errno));
  946. return err;
  947. break;
  948. }
  949. }
  950. return err;
  951. }
  952. /*
  953. * inet_sendto_timeout: is the same as inet_sendto() but if the packet isn't sent
  954. * in `timeout' seconds it timeouts and returns -1.
  955. */
  956. ssize_t
  957. inet_sendto_timeout(int s, const void *msg, size_t len, int flags,
  958. const struct sockaddr * to, socklen_t tolen,
  959. u_int timeout)
  960. {
  961. struct timeval timeout_t;
  962. fd_set fdset;
  963. int ret;
  964. MILLISEC_TO_TV(timeout * 1000, timeout_t);
  965. FD_ZERO(&fdset);
  966. FD_SET(s, &fdset);
  967. ret = select(s + 1, NULL, &fdset, NULL, &timeout_t);
  968. if (ret == -1) {
  969. error(ERROR_MSG "select error: %s", ERROR_FUNC, strerror(errno));
  970. return ret;
  971. }
  972. if (FD_ISSET(s, &fdset))
  973. return inet_sendto(s, msg, len, flags, to, tolen);
  974. return -1;
  975. }
  976. ssize_t
  977. inet_sendfile(int out_fd, int in_fd, off_t * offset, size_t count)
  978. {
  979. ssize_t err;
  980. if ((err = sendfile(out_fd, in_fd, offset, count)) == -1)
  981. error("inet_sendfile: Cannot sendfile(): %s", strerror(errno));
  982. if (err < count) {
  983. count = count - err;
  984. err = inet_sendfile(out_fd, in_fd, offset, count);
  985. }
  986. return err;
  987. }
  988. /* For use in 32 bit systems requiring 64 bit operations
  989. * Currently non-functional. */
  990. /*
  991. #if UINTPTR_MAX == 0xffffffff
  992. #ifndef _LARGEFILE64_SOURCE
  993. #define _LARGEFILE64_SOURCE
  994. #endif
  995. ssize_t
  996. inet_sendfile64(int out_fd, int in_fd, off64_t * offset, size_t count)
  997. {
  998. ssize_t err;
  999. if ((err = sendfile64(out_fd, in_fd, offset, count)) == -1)
  1000. error("inet_sendfile: Cannot sendfile(): %s", strerror(errno));
  1001. if (err < count) {
  1002. count = count - err;
  1003. err = inet_sendfile64(out_fd, in_fd, offset, count);
  1004. }
  1005. return err;
  1006. }
  1007. #endif*/