udpgso.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <stddef.h>
  4. #include <arpa/inet.h>
  5. #include <error.h>
  6. #include <errno.h>
  7. #include <net/if.h>
  8. #include <linux/in.h>
  9. #include <linux/netlink.h>
  10. #include <linux/rtnetlink.h>
  11. #include <netinet/if_ether.h>
  12. #include <netinet/ip.h>
  13. #include <netinet/ip6.h>
  14. #include <netinet/udp.h>
  15. #include <stdbool.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/socket.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #ifndef ETH_MAX_MTU
  27. #define ETH_MAX_MTU 0xFFFFU
  28. #endif
  29. #ifndef UDP_SEGMENT
  30. #define UDP_SEGMENT 103
  31. #endif
  32. #ifndef UDP_MAX_SEGMENTS
  33. #define UDP_MAX_SEGMENTS (1 << 6UL)
  34. #endif
  35. #define CONST_MTU_TEST 1500
  36. #define CONST_HDRLEN_V4 (sizeof(struct iphdr) + sizeof(struct udphdr))
  37. #define CONST_HDRLEN_V6 (sizeof(struct ip6_hdr) + sizeof(struct udphdr))
  38. #define CONST_MSS_V4 (CONST_MTU_TEST - CONST_HDRLEN_V4)
  39. #define CONST_MSS_V6 (CONST_MTU_TEST - CONST_HDRLEN_V6)
  40. #define CONST_MAX_SEGS_V4 (ETH_MAX_MTU / CONST_MSS_V4)
  41. #define CONST_MAX_SEGS_V6 (ETH_MAX_MTU / CONST_MSS_V6)
  42. static bool cfg_do_ipv4;
  43. static bool cfg_do_ipv6;
  44. static bool cfg_do_connected;
  45. static bool cfg_do_connectionless;
  46. static bool cfg_do_msgmore;
  47. static bool cfg_do_setsockopt;
  48. static int cfg_specific_test_id = -1;
  49. static const char cfg_ifname[] = "lo";
  50. static unsigned short cfg_port = 9000;
  51. static char buf[ETH_MAX_MTU];
  52. struct testcase {
  53. int tlen; /* send() buffer size, may exceed mss */
  54. bool tfail; /* send() call is expected to fail */
  55. int gso_len; /* mss after applying gso */
  56. int r_num_mss; /* recv(): number of calls of full mss */
  57. int r_len_last; /* recv(): size of last non-mss dgram, if any */
  58. };
  59. const struct in6_addr addr6 = IN6ADDR_LOOPBACK_INIT;
  60. const struct in_addr addr4 = { .s_addr = __constant_htonl(INADDR_LOOPBACK + 2) };
  61. struct testcase testcases_v4[] = {
  62. {
  63. /* no GSO: send a single byte */
  64. .tlen = 1,
  65. .r_len_last = 1,
  66. },
  67. {
  68. /* no GSO: send a single MSS */
  69. .tlen = CONST_MSS_V4,
  70. .r_num_mss = 1,
  71. },
  72. {
  73. /* no GSO: send a single MSS + 1B: fail */
  74. .tlen = CONST_MSS_V4 + 1,
  75. .tfail = true,
  76. },
  77. {
  78. /* send a single MSS: will fall back to no GSO */
  79. .tlen = CONST_MSS_V4,
  80. .gso_len = CONST_MSS_V4,
  81. .r_num_mss = 1,
  82. },
  83. {
  84. /* send a single MSS + 1B */
  85. .tlen = CONST_MSS_V4 + 1,
  86. .gso_len = CONST_MSS_V4,
  87. .r_num_mss = 1,
  88. .r_len_last = 1,
  89. },
  90. {
  91. /* send exactly 2 MSS */
  92. .tlen = CONST_MSS_V4 * 2,
  93. .gso_len = CONST_MSS_V4,
  94. .r_num_mss = 2,
  95. },
  96. {
  97. /* send 2 MSS + 1B */
  98. .tlen = (CONST_MSS_V4 * 2) + 1,
  99. .gso_len = CONST_MSS_V4,
  100. .r_num_mss = 2,
  101. .r_len_last = 1,
  102. },
  103. {
  104. /* send MAX segs */
  105. .tlen = (ETH_MAX_MTU / CONST_MSS_V4) * CONST_MSS_V4,
  106. .gso_len = CONST_MSS_V4,
  107. .r_num_mss = (ETH_MAX_MTU / CONST_MSS_V4),
  108. },
  109. {
  110. /* send MAX bytes */
  111. .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4,
  112. .gso_len = CONST_MSS_V4,
  113. .r_num_mss = CONST_MAX_SEGS_V4,
  114. .r_len_last = ETH_MAX_MTU - CONST_HDRLEN_V4 -
  115. (CONST_MAX_SEGS_V4 * CONST_MSS_V4),
  116. },
  117. {
  118. /* send MAX + 1: fail */
  119. .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4 + 1,
  120. .gso_len = CONST_MSS_V4,
  121. .tfail = true,
  122. },
  123. {
  124. /* send a single 1B MSS: will fall back to no GSO */
  125. .tlen = 1,
  126. .gso_len = 1,
  127. .r_num_mss = 1,
  128. },
  129. {
  130. /* send 2 1B segments */
  131. .tlen = 2,
  132. .gso_len = 1,
  133. .r_num_mss = 2,
  134. },
  135. {
  136. /* send 2B + 2B + 1B segments */
  137. .tlen = 5,
  138. .gso_len = 2,
  139. .r_num_mss = 2,
  140. .r_len_last = 1,
  141. },
  142. {
  143. /* send max number of min sized segments */
  144. .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V4,
  145. .gso_len = 1,
  146. .r_num_mss = UDP_MAX_SEGMENTS - CONST_HDRLEN_V4,
  147. },
  148. {
  149. /* send max number + 1 of min sized segments: fail */
  150. .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V4 + 1,
  151. .gso_len = 1,
  152. .tfail = true,
  153. },
  154. {
  155. /* EOL */
  156. }
  157. };
  158. #ifndef IP6_MAX_MTU
  159. #define IP6_MAX_MTU (ETH_MAX_MTU + sizeof(struct ip6_hdr))
  160. #endif
  161. struct testcase testcases_v6[] = {
  162. {
  163. /* no GSO: send a single byte */
  164. .tlen = 1,
  165. .r_len_last = 1,
  166. },
  167. {
  168. /* no GSO: send a single MSS */
  169. .tlen = CONST_MSS_V6,
  170. .r_num_mss = 1,
  171. },
  172. {
  173. /* no GSO: send a single MSS + 1B: fail */
  174. .tlen = CONST_MSS_V6 + 1,
  175. .tfail = true,
  176. },
  177. {
  178. /* send a single MSS: will fall back to no GSO */
  179. .tlen = CONST_MSS_V6,
  180. .gso_len = CONST_MSS_V6,
  181. .r_num_mss = 1,
  182. },
  183. {
  184. /* send a single MSS + 1B */
  185. .tlen = CONST_MSS_V6 + 1,
  186. .gso_len = CONST_MSS_V6,
  187. .r_num_mss = 1,
  188. .r_len_last = 1,
  189. },
  190. {
  191. /* send exactly 2 MSS */
  192. .tlen = CONST_MSS_V6 * 2,
  193. .gso_len = CONST_MSS_V6,
  194. .r_num_mss = 2,
  195. },
  196. {
  197. /* send 2 MSS + 1B */
  198. .tlen = (CONST_MSS_V6 * 2) + 1,
  199. .gso_len = CONST_MSS_V6,
  200. .r_num_mss = 2,
  201. .r_len_last = 1,
  202. },
  203. {
  204. /* send MAX segs */
  205. .tlen = (IP6_MAX_MTU / CONST_MSS_V6) * CONST_MSS_V6,
  206. .gso_len = CONST_MSS_V6,
  207. .r_num_mss = (IP6_MAX_MTU / CONST_MSS_V6),
  208. },
  209. {
  210. /* send MAX bytes */
  211. .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6,
  212. .gso_len = CONST_MSS_V6,
  213. .r_num_mss = CONST_MAX_SEGS_V6,
  214. .r_len_last = IP6_MAX_MTU - CONST_HDRLEN_V6 -
  215. (CONST_MAX_SEGS_V6 * CONST_MSS_V6),
  216. },
  217. {
  218. /* send MAX + 1: fail */
  219. .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6 + 1,
  220. .gso_len = CONST_MSS_V6,
  221. .tfail = true,
  222. },
  223. {
  224. /* send a single 1B MSS: will fall back to no GSO */
  225. .tlen = 1,
  226. .gso_len = 1,
  227. .r_num_mss = 1,
  228. },
  229. {
  230. /* send 2 1B segments */
  231. .tlen = 2,
  232. .gso_len = 1,
  233. .r_num_mss = 2,
  234. },
  235. {
  236. /* send 2B + 2B + 1B segments */
  237. .tlen = 5,
  238. .gso_len = 2,
  239. .r_num_mss = 2,
  240. .r_len_last = 1,
  241. },
  242. {
  243. /* send max number of min sized segments */
  244. .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V6,
  245. .gso_len = 1,
  246. .r_num_mss = UDP_MAX_SEGMENTS - CONST_HDRLEN_V6,
  247. },
  248. {
  249. /* send max number + 1 of min sized segments: fail */
  250. .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V6 + 1,
  251. .gso_len = 1,
  252. .tfail = true,
  253. },
  254. {
  255. /* EOL */
  256. }
  257. };
  258. static unsigned int get_device_mtu(int fd, const char *ifname)
  259. {
  260. struct ifreq ifr;
  261. memset(&ifr, 0, sizeof(ifr));
  262. strcpy(ifr.ifr_name, ifname);
  263. if (ioctl(fd, SIOCGIFMTU, &ifr))
  264. error(1, errno, "ioctl get mtu");
  265. return ifr.ifr_mtu;
  266. }
  267. static void __set_device_mtu(int fd, const char *ifname, unsigned int mtu)
  268. {
  269. struct ifreq ifr;
  270. memset(&ifr, 0, sizeof(ifr));
  271. ifr.ifr_mtu = mtu;
  272. strcpy(ifr.ifr_name, ifname);
  273. if (ioctl(fd, SIOCSIFMTU, &ifr))
  274. error(1, errno, "ioctl set mtu");
  275. }
  276. static void set_device_mtu(int fd, int mtu)
  277. {
  278. int val;
  279. val = get_device_mtu(fd, cfg_ifname);
  280. fprintf(stderr, "device mtu (orig): %u\n", val);
  281. __set_device_mtu(fd, cfg_ifname, mtu);
  282. val = get_device_mtu(fd, cfg_ifname);
  283. if (val != mtu)
  284. error(1, 0, "unable to set device mtu to %u\n", val);
  285. fprintf(stderr, "device mtu (test): %u\n", val);
  286. }
  287. static void set_pmtu_discover(int fd, bool is_ipv4)
  288. {
  289. int level, name, val;
  290. if (is_ipv4) {
  291. level = SOL_IP;
  292. name = IP_MTU_DISCOVER;
  293. val = IP_PMTUDISC_DO;
  294. } else {
  295. level = SOL_IPV6;
  296. name = IPV6_MTU_DISCOVER;
  297. val = IPV6_PMTUDISC_DO;
  298. }
  299. if (setsockopt(fd, level, name, &val, sizeof(val)))
  300. error(1, errno, "setsockopt path mtu");
  301. }
  302. static unsigned int get_path_mtu(int fd, bool is_ipv4)
  303. {
  304. socklen_t vallen;
  305. unsigned int mtu;
  306. int ret;
  307. vallen = sizeof(mtu);
  308. if (is_ipv4)
  309. ret = getsockopt(fd, SOL_IP, IP_MTU, &mtu, &vallen);
  310. else
  311. ret = getsockopt(fd, SOL_IPV6, IPV6_MTU, &mtu, &vallen);
  312. if (ret)
  313. error(1, errno, "getsockopt mtu");
  314. fprintf(stderr, "path mtu (read): %u\n", mtu);
  315. return mtu;
  316. }
  317. /* very wordy version of system("ip route add dev lo mtu 1500 127.0.0.3/32") */
  318. static void set_route_mtu(int mtu, bool is_ipv4)
  319. {
  320. struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
  321. struct nlmsghdr *nh;
  322. struct rtattr *rta;
  323. struct rtmsg *rt;
  324. char data[NLMSG_ALIGN(sizeof(*nh)) +
  325. NLMSG_ALIGN(sizeof(*rt)) +
  326. NLMSG_ALIGN(RTA_LENGTH(sizeof(addr6))) +
  327. NLMSG_ALIGN(RTA_LENGTH(sizeof(int))) +
  328. NLMSG_ALIGN(RTA_LENGTH(0) + RTA_LENGTH(sizeof(int)))];
  329. int fd, ret, alen, off = 0;
  330. alen = is_ipv4 ? sizeof(addr4) : sizeof(addr6);
  331. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  332. if (fd == -1)
  333. error(1, errno, "socket netlink");
  334. memset(data, 0, sizeof(data));
  335. nh = (void *)data;
  336. nh->nlmsg_type = RTM_NEWROUTE;
  337. nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
  338. off += NLMSG_ALIGN(sizeof(*nh));
  339. rt = (void *)(data + off);
  340. rt->rtm_family = is_ipv4 ? AF_INET : AF_INET6;
  341. rt->rtm_table = RT_TABLE_MAIN;
  342. rt->rtm_dst_len = alen << 3;
  343. rt->rtm_protocol = RTPROT_BOOT;
  344. rt->rtm_scope = RT_SCOPE_UNIVERSE;
  345. rt->rtm_type = RTN_UNICAST;
  346. off += NLMSG_ALIGN(sizeof(*rt));
  347. rta = (void *)(data + off);
  348. rta->rta_type = RTA_DST;
  349. rta->rta_len = RTA_LENGTH(alen);
  350. if (is_ipv4)
  351. memcpy(RTA_DATA(rta), &addr4, alen);
  352. else
  353. memcpy(RTA_DATA(rta), &addr6, alen);
  354. off += NLMSG_ALIGN(rta->rta_len);
  355. rta = (void *)(data + off);
  356. rta->rta_type = RTA_OIF;
  357. rta->rta_len = RTA_LENGTH(sizeof(int));
  358. *((int *)(RTA_DATA(rta))) = 1; //if_nametoindex("lo");
  359. off += NLMSG_ALIGN(rta->rta_len);
  360. /* MTU is a subtype in a metrics type */
  361. rta = (void *)(data + off);
  362. rta->rta_type = RTA_METRICS;
  363. rta->rta_len = RTA_LENGTH(0) + RTA_LENGTH(sizeof(int));
  364. off += NLMSG_ALIGN(rta->rta_len);
  365. /* now fill MTU subtype. Note that it fits within above rta_len */
  366. rta = (void *)(((char *) rta) + RTA_LENGTH(0));
  367. rta->rta_type = RTAX_MTU;
  368. rta->rta_len = RTA_LENGTH(sizeof(int));
  369. *((int *)(RTA_DATA(rta))) = mtu;
  370. nh->nlmsg_len = off;
  371. ret = sendto(fd, data, off, 0, (void *)&nladdr, sizeof(nladdr));
  372. if (ret != off)
  373. error(1, errno, "send netlink: %uB != %uB\n", ret, off);
  374. if (close(fd))
  375. error(1, errno, "close netlink");
  376. fprintf(stderr, "route mtu (test): %u\n", mtu);
  377. }
  378. static bool __send_one(int fd, struct msghdr *msg, int flags)
  379. {
  380. int ret;
  381. ret = sendmsg(fd, msg, flags);
  382. if (ret == -1 &&
  383. (errno == EMSGSIZE || errno == ENOMEM || errno == EINVAL))
  384. return false;
  385. if (ret == -1)
  386. error(1, errno, "sendmsg");
  387. if (ret != msg->msg_iov->iov_len)
  388. error(1, 0, "sendto: %d != %lu", ret, msg->msg_iov->iov_len);
  389. if (msg->msg_flags)
  390. error(1, 0, "sendmsg: return flags 0x%x\n", msg->msg_flags);
  391. return true;
  392. }
  393. static bool send_one(int fd, int len, int gso_len,
  394. struct sockaddr *addr, socklen_t alen)
  395. {
  396. char control[CMSG_SPACE(sizeof(uint16_t))] = {0};
  397. struct msghdr msg = {0};
  398. struct iovec iov = {0};
  399. struct cmsghdr *cm;
  400. iov.iov_base = buf;
  401. iov.iov_len = len;
  402. msg.msg_iov = &iov;
  403. msg.msg_iovlen = 1;
  404. msg.msg_name = addr;
  405. msg.msg_namelen = alen;
  406. if (gso_len && !cfg_do_setsockopt) {
  407. msg.msg_control = control;
  408. msg.msg_controllen = sizeof(control);
  409. cm = CMSG_FIRSTHDR(&msg);
  410. cm->cmsg_level = SOL_UDP;
  411. cm->cmsg_type = UDP_SEGMENT;
  412. cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
  413. *((uint16_t *) CMSG_DATA(cm)) = gso_len;
  414. }
  415. /* If MSG_MORE, send 1 byte followed by remainder */
  416. if (cfg_do_msgmore && len > 1) {
  417. iov.iov_len = 1;
  418. if (!__send_one(fd, &msg, MSG_MORE))
  419. error(1, 0, "send 1B failed");
  420. iov.iov_base++;
  421. iov.iov_len = len - 1;
  422. }
  423. return __send_one(fd, &msg, 0);
  424. }
  425. static int recv_one(int fd, int flags)
  426. {
  427. int ret;
  428. ret = recv(fd, buf, sizeof(buf), flags);
  429. if (ret == -1 && errno == EAGAIN && (flags & MSG_DONTWAIT))
  430. return 0;
  431. if (ret == -1)
  432. error(1, errno, "recv");
  433. return ret;
  434. }
  435. static void run_one(struct testcase *test, int fdt, int fdr,
  436. struct sockaddr *addr, socklen_t alen)
  437. {
  438. int i, ret, val, mss;
  439. bool sent;
  440. fprintf(stderr, "ipv%d tx:%d gso:%d %s\n",
  441. addr->sa_family == AF_INET ? 4 : 6,
  442. test->tlen, test->gso_len,
  443. test->tfail ? "(fail)" : "");
  444. val = test->gso_len;
  445. if (cfg_do_setsockopt) {
  446. if (setsockopt(fdt, SOL_UDP, UDP_SEGMENT, &val, sizeof(val)))
  447. error(1, errno, "setsockopt udp segment");
  448. }
  449. sent = send_one(fdt, test->tlen, test->gso_len, addr, alen);
  450. if (sent && test->tfail)
  451. error(1, 0, "send succeeded while expecting failure");
  452. if (!sent && !test->tfail)
  453. error(1, 0, "send failed while expecting success");
  454. if (!sent)
  455. return;
  456. if (test->gso_len)
  457. mss = test->gso_len;
  458. else
  459. mss = addr->sa_family == AF_INET ? CONST_MSS_V4 : CONST_MSS_V6;
  460. /* Recv all full MSS datagrams */
  461. for (i = 0; i < test->r_num_mss; i++) {
  462. ret = recv_one(fdr, 0);
  463. if (ret != mss)
  464. error(1, 0, "recv.%d: %d != %d", i, ret, mss);
  465. }
  466. /* Recv the non-full last datagram, if tlen was not a multiple of mss */
  467. if (test->r_len_last) {
  468. ret = recv_one(fdr, 0);
  469. if (ret != test->r_len_last)
  470. error(1, 0, "recv.%d: %d != %d (last)",
  471. i, ret, test->r_len_last);
  472. }
  473. /* Verify received all data */
  474. ret = recv_one(fdr, MSG_DONTWAIT);
  475. if (ret)
  476. error(1, 0, "recv: unexpected datagram");
  477. }
  478. static void run_all(int fdt, int fdr, struct sockaddr *addr, socklen_t alen)
  479. {
  480. struct testcase *tests, *test;
  481. tests = addr->sa_family == AF_INET ? testcases_v4 : testcases_v6;
  482. for (test = tests; test->tlen; test++) {
  483. /* if a specific test is given, then skip all others */
  484. if (cfg_specific_test_id == -1 ||
  485. cfg_specific_test_id == test - tests)
  486. run_one(test, fdt, fdr, addr, alen);
  487. }
  488. }
  489. static void run_test(struct sockaddr *addr, socklen_t alen)
  490. {
  491. struct timeval tv = { .tv_usec = 100 * 1000 };
  492. int fdr, fdt, val;
  493. fdr = socket(addr->sa_family, SOCK_DGRAM, 0);
  494. if (fdr == -1)
  495. error(1, errno, "socket r");
  496. if (bind(fdr, addr, alen))
  497. error(1, errno, "bind");
  498. /* Have tests fail quickly instead of hang */
  499. if (setsockopt(fdr, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
  500. error(1, errno, "setsockopt rcv timeout");
  501. fdt = socket(addr->sa_family, SOCK_DGRAM, 0);
  502. if (fdt == -1)
  503. error(1, errno, "socket t");
  504. /* Do not fragment these datagrams: only succeed if GSO works */
  505. set_pmtu_discover(fdt, addr->sa_family == AF_INET);
  506. if (cfg_do_connectionless) {
  507. set_device_mtu(fdt, CONST_MTU_TEST);
  508. run_all(fdt, fdr, addr, alen);
  509. }
  510. if (cfg_do_connected) {
  511. set_device_mtu(fdt, CONST_MTU_TEST + 100);
  512. set_route_mtu(CONST_MTU_TEST, addr->sa_family == AF_INET);
  513. if (connect(fdt, addr, alen))
  514. error(1, errno, "connect");
  515. val = get_path_mtu(fdt, addr->sa_family == AF_INET);
  516. if (val != CONST_MTU_TEST)
  517. error(1, 0, "bad path mtu %u\n", val);
  518. run_all(fdt, fdr, addr, 0 /* use connected addr */);
  519. }
  520. if (close(fdt))
  521. error(1, errno, "close t");
  522. if (close(fdr))
  523. error(1, errno, "close r");
  524. }
  525. static void run_test_v4(void)
  526. {
  527. struct sockaddr_in addr = {0};
  528. addr.sin_family = AF_INET;
  529. addr.sin_port = htons(cfg_port);
  530. addr.sin_addr = addr4;
  531. run_test((void *)&addr, sizeof(addr));
  532. }
  533. static void run_test_v6(void)
  534. {
  535. struct sockaddr_in6 addr = {0};
  536. addr.sin6_family = AF_INET6;
  537. addr.sin6_port = htons(cfg_port);
  538. addr.sin6_addr = addr6;
  539. run_test((void *)&addr, sizeof(addr));
  540. }
  541. static void parse_opts(int argc, char **argv)
  542. {
  543. int c;
  544. while ((c = getopt(argc, argv, "46cCmst:")) != -1) {
  545. switch (c) {
  546. case '4':
  547. cfg_do_ipv4 = true;
  548. break;
  549. case '6':
  550. cfg_do_ipv6 = true;
  551. break;
  552. case 'c':
  553. cfg_do_connected = true;
  554. break;
  555. case 'C':
  556. cfg_do_connectionless = true;
  557. break;
  558. case 'm':
  559. cfg_do_msgmore = true;
  560. break;
  561. case 's':
  562. cfg_do_setsockopt = true;
  563. break;
  564. case 't':
  565. cfg_specific_test_id = strtoul(optarg, NULL, 0);
  566. break;
  567. default:
  568. error(1, 0, "%s: parse error", argv[0]);
  569. }
  570. }
  571. }
  572. int main(int argc, char **argv)
  573. {
  574. parse_opts(argc, argv);
  575. if (cfg_do_ipv4)
  576. run_test_v4();
  577. if (cfg_do_ipv6)
  578. run_test_v6();
  579. fprintf(stderr, "OK\n");
  580. return 0;
  581. }