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 <string.h>
  19. #include <sys/ioctl.h>
  20. #include <sys/socket.h>
  21. #include <sys/stat.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <unistd.h>
  25. #ifndef ETH_MAX_MTU
  26. #define ETH_MAX_MTU 0xFFFFU
  27. #endif
  28. #ifndef UDP_SEGMENT
  29. #define UDP_SEGMENT 103
  30. #endif
  31. #ifndef UDP_MAX_SEGMENTS
  32. #define UDP_MAX_SEGMENTS (1 << 6UL)
  33. #endif
  34. #define CONST_MTU_TEST 1500
  35. #define CONST_HDRLEN_V4 (sizeof(struct iphdr) + sizeof(struct udphdr))
  36. #define CONST_HDRLEN_V6 (sizeof(struct ip6_hdr) + sizeof(struct udphdr))
  37. #define CONST_MSS_V4 (CONST_MTU_TEST - CONST_HDRLEN_V4)
  38. #define CONST_MSS_V6 (CONST_MTU_TEST - CONST_HDRLEN_V6)
  39. #define CONST_MAX_SEGS_V4 (ETH_MAX_MTU / CONST_MSS_V4)
  40. #define CONST_MAX_SEGS_V6 (ETH_MAX_MTU / CONST_MSS_V6)
  41. static bool cfg_do_ipv4;
  42. static bool cfg_do_ipv6;
  43. static bool cfg_do_connected;
  44. static bool cfg_do_connectionless;
  45. static bool cfg_do_msgmore;
  46. static bool cfg_do_setsockopt;
  47. static int cfg_specific_test_id = -1;
  48. static const char cfg_ifname[] = "lo";
  49. static unsigned short cfg_port = 9000;
  50. static char buf[ETH_MAX_MTU];
  51. struct testcase {
  52. int tlen; /* send() buffer size, may exceed mss */
  53. bool tfail; /* send() call is expected to fail */
  54. int gso_len; /* mss after applying gso */
  55. int r_num_mss; /* recv(): number of calls of full mss */
  56. int r_len_last; /* recv(): size of last non-mss dgram, if any */
  57. };
  58. const struct in6_addr addr6 = IN6ADDR_LOOPBACK_INIT;
  59. const struct in_addr addr4 = { .s_addr = __constant_htonl(INADDR_LOOPBACK + 2) };
  60. struct testcase testcases_v4[] = {
  61. {
  62. /* no GSO: send a single byte */
  63. .tlen = 1,
  64. .r_len_last = 1,
  65. },
  66. {
  67. /* no GSO: send a single MSS */
  68. .tlen = CONST_MSS_V4,
  69. .r_num_mss = 1,
  70. },
  71. {
  72. /* no GSO: send a single MSS + 1B: fail */
  73. .tlen = CONST_MSS_V4 + 1,
  74. .tfail = true,
  75. },
  76. {
  77. /* send a single MSS: will fall back to no GSO */
  78. .tlen = CONST_MSS_V4,
  79. .gso_len = CONST_MSS_V4,
  80. .r_num_mss = 1,
  81. },
  82. {
  83. /* send a single MSS + 1B */
  84. .tlen = CONST_MSS_V4 + 1,
  85. .gso_len = CONST_MSS_V4,
  86. .r_num_mss = 1,
  87. .r_len_last = 1,
  88. },
  89. {
  90. /* send exactly 2 MSS */
  91. .tlen = CONST_MSS_V4 * 2,
  92. .gso_len = CONST_MSS_V4,
  93. .r_num_mss = 2,
  94. },
  95. {
  96. /* send 2 MSS + 1B */
  97. .tlen = (CONST_MSS_V4 * 2) + 1,
  98. .gso_len = CONST_MSS_V4,
  99. .r_num_mss = 2,
  100. .r_len_last = 1,
  101. },
  102. {
  103. /* send MAX segs */
  104. .tlen = (ETH_MAX_MTU / CONST_MSS_V4) * CONST_MSS_V4,
  105. .gso_len = CONST_MSS_V4,
  106. .r_num_mss = (ETH_MAX_MTU / CONST_MSS_V4),
  107. },
  108. {
  109. /* send MAX bytes */
  110. .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4,
  111. .gso_len = CONST_MSS_V4,
  112. .r_num_mss = CONST_MAX_SEGS_V4,
  113. .r_len_last = ETH_MAX_MTU - CONST_HDRLEN_V4 -
  114. (CONST_MAX_SEGS_V4 * CONST_MSS_V4),
  115. },
  116. {
  117. /* send MAX + 1: fail */
  118. .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4 + 1,
  119. .gso_len = CONST_MSS_V4,
  120. .tfail = true,
  121. },
  122. {
  123. /* send a single 1B MSS: will fall back to no GSO */
  124. .tlen = 1,
  125. .gso_len = 1,
  126. .r_num_mss = 1,
  127. },
  128. {
  129. /* send 2 1B segments */
  130. .tlen = 2,
  131. .gso_len = 1,
  132. .r_num_mss = 2,
  133. },
  134. {
  135. /* send 2B + 2B + 1B segments */
  136. .tlen = 5,
  137. .gso_len = 2,
  138. .r_num_mss = 2,
  139. .r_len_last = 1,
  140. },
  141. {
  142. /* send max number of min sized segments */
  143. .tlen = UDP_MAX_SEGMENTS,
  144. .gso_len = 1,
  145. .r_num_mss = UDP_MAX_SEGMENTS,
  146. },
  147. {
  148. /* send max number + 1 of min sized segments: fail */
  149. .tlen = UDP_MAX_SEGMENTS + 1,
  150. .gso_len = 1,
  151. .tfail = true,
  152. },
  153. {
  154. /* EOL */
  155. }
  156. };
  157. #ifndef IP6_MAX_MTU
  158. #define IP6_MAX_MTU (ETH_MAX_MTU + sizeof(struct ip6_hdr))
  159. #endif
  160. struct testcase testcases_v6[] = {
  161. {
  162. /* no GSO: send a single byte */
  163. .tlen = 1,
  164. .r_len_last = 1,
  165. },
  166. {
  167. /* no GSO: send a single MSS */
  168. .tlen = CONST_MSS_V6,
  169. .r_num_mss = 1,
  170. },
  171. {
  172. /* no GSO: send a single MSS + 1B: fail */
  173. .tlen = CONST_MSS_V6 + 1,
  174. .tfail = true,
  175. },
  176. {
  177. /* send a single MSS: will fall back to no GSO */
  178. .tlen = CONST_MSS_V6,
  179. .gso_len = CONST_MSS_V6,
  180. .r_num_mss = 1,
  181. },
  182. {
  183. /* send a single MSS + 1B */
  184. .tlen = CONST_MSS_V6 + 1,
  185. .gso_len = CONST_MSS_V6,
  186. .r_num_mss = 1,
  187. .r_len_last = 1,
  188. },
  189. {
  190. /* send exactly 2 MSS */
  191. .tlen = CONST_MSS_V6 * 2,
  192. .gso_len = CONST_MSS_V6,
  193. .r_num_mss = 2,
  194. },
  195. {
  196. /* send 2 MSS + 1B */
  197. .tlen = (CONST_MSS_V6 * 2) + 1,
  198. .gso_len = CONST_MSS_V6,
  199. .r_num_mss = 2,
  200. .r_len_last = 1,
  201. },
  202. {
  203. /* send MAX segs */
  204. .tlen = (IP6_MAX_MTU / CONST_MSS_V6) * CONST_MSS_V6,
  205. .gso_len = CONST_MSS_V6,
  206. .r_num_mss = (IP6_MAX_MTU / CONST_MSS_V6),
  207. },
  208. {
  209. /* send MAX bytes */
  210. .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6,
  211. .gso_len = CONST_MSS_V6,
  212. .r_num_mss = CONST_MAX_SEGS_V6,
  213. .r_len_last = IP6_MAX_MTU - CONST_HDRLEN_V6 -
  214. (CONST_MAX_SEGS_V6 * CONST_MSS_V6),
  215. },
  216. {
  217. /* send MAX + 1: fail */
  218. .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6 + 1,
  219. .gso_len = CONST_MSS_V6,
  220. .tfail = true,
  221. },
  222. {
  223. /* send a single 1B MSS: will fall back to no GSO */
  224. .tlen = 1,
  225. .gso_len = 1,
  226. .r_num_mss = 1,
  227. },
  228. {
  229. /* send 2 1B segments */
  230. .tlen = 2,
  231. .gso_len = 1,
  232. .r_num_mss = 2,
  233. },
  234. {
  235. /* send 2B + 2B + 1B segments */
  236. .tlen = 5,
  237. .gso_len = 2,
  238. .r_num_mss = 2,
  239. .r_len_last = 1,
  240. },
  241. {
  242. /* send max number of min sized segments */
  243. .tlen = UDP_MAX_SEGMENTS,
  244. .gso_len = 1,
  245. .r_num_mss = UDP_MAX_SEGMENTS,
  246. },
  247. {
  248. /* send max number + 1 of min sized segments: fail */
  249. .tlen = UDP_MAX_SEGMENTS + 1,
  250. .gso_len = 1,
  251. .tfail = true,
  252. },
  253. {
  254. /* EOL */
  255. }
  256. };
  257. static unsigned int get_device_mtu(int fd, const char *ifname)
  258. {
  259. struct ifreq ifr;
  260. memset(&ifr, 0, sizeof(ifr));
  261. strcpy(ifr.ifr_name, ifname);
  262. if (ioctl(fd, SIOCGIFMTU, &ifr))
  263. error(1, errno, "ioctl get mtu");
  264. return ifr.ifr_mtu;
  265. }
  266. static void __set_device_mtu(int fd, const char *ifname, unsigned int mtu)
  267. {
  268. struct ifreq ifr;
  269. memset(&ifr, 0, sizeof(ifr));
  270. ifr.ifr_mtu = mtu;
  271. strcpy(ifr.ifr_name, ifname);
  272. if (ioctl(fd, SIOCSIFMTU, &ifr))
  273. error(1, errno, "ioctl set mtu");
  274. }
  275. static void set_device_mtu(int fd, int mtu)
  276. {
  277. int val;
  278. val = get_device_mtu(fd, cfg_ifname);
  279. fprintf(stderr, "device mtu (orig): %u\n", val);
  280. __set_device_mtu(fd, cfg_ifname, mtu);
  281. val = get_device_mtu(fd, cfg_ifname);
  282. if (val != mtu)
  283. error(1, 0, "unable to set device mtu to %u\n", val);
  284. fprintf(stderr, "device mtu (test): %u\n", val);
  285. }
  286. static void set_pmtu_discover(int fd, bool is_ipv4)
  287. {
  288. int level, name, val;
  289. if (is_ipv4) {
  290. level = SOL_IP;
  291. name = IP_MTU_DISCOVER;
  292. val = IP_PMTUDISC_DO;
  293. } else {
  294. level = SOL_IPV6;
  295. name = IPV6_MTU_DISCOVER;
  296. val = IPV6_PMTUDISC_DO;
  297. }
  298. if (setsockopt(fd, level, name, &val, sizeof(val)))
  299. error(1, errno, "setsockopt path mtu");
  300. }
  301. static unsigned int get_path_mtu(int fd, bool is_ipv4)
  302. {
  303. socklen_t vallen;
  304. unsigned int mtu;
  305. int ret;
  306. vallen = sizeof(mtu);
  307. if (is_ipv4)
  308. ret = getsockopt(fd, SOL_IP, IP_MTU, &mtu, &vallen);
  309. else
  310. ret = getsockopt(fd, SOL_IPV6, IPV6_MTU, &mtu, &vallen);
  311. if (ret)
  312. error(1, errno, "getsockopt mtu");
  313. fprintf(stderr, "path mtu (read): %u\n", mtu);
  314. return mtu;
  315. }
  316. /* very wordy version of system("ip route add dev lo mtu 1500 127.0.0.3/32") */
  317. static void set_route_mtu(int mtu, bool is_ipv4)
  318. {
  319. struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
  320. struct nlmsghdr *nh;
  321. struct rtattr *rta;
  322. struct rtmsg *rt;
  323. char data[NLMSG_ALIGN(sizeof(*nh)) +
  324. NLMSG_ALIGN(sizeof(*rt)) +
  325. NLMSG_ALIGN(RTA_LENGTH(sizeof(addr6))) +
  326. NLMSG_ALIGN(RTA_LENGTH(sizeof(int))) +
  327. NLMSG_ALIGN(RTA_LENGTH(0) + RTA_LENGTH(sizeof(int)))];
  328. int fd, ret, alen, off = 0;
  329. alen = is_ipv4 ? sizeof(addr4) : sizeof(addr6);
  330. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  331. if (fd == -1)
  332. error(1, errno, "socket netlink");
  333. memset(data, 0, sizeof(data));
  334. nh = (void *)data;
  335. nh->nlmsg_type = RTM_NEWROUTE;
  336. nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
  337. off += NLMSG_ALIGN(sizeof(*nh));
  338. rt = (void *)(data + off);
  339. rt->rtm_family = is_ipv4 ? AF_INET : AF_INET6;
  340. rt->rtm_table = RT_TABLE_MAIN;
  341. rt->rtm_dst_len = alen << 3;
  342. rt->rtm_protocol = RTPROT_BOOT;
  343. rt->rtm_scope = RT_SCOPE_UNIVERSE;
  344. rt->rtm_type = RTN_UNICAST;
  345. off += NLMSG_ALIGN(sizeof(*rt));
  346. rta = (void *)(data + off);
  347. rta->rta_type = RTA_DST;
  348. rta->rta_len = RTA_LENGTH(alen);
  349. if (is_ipv4)
  350. memcpy(RTA_DATA(rta), &addr4, alen);
  351. else
  352. memcpy(RTA_DATA(rta), &addr6, alen);
  353. off += NLMSG_ALIGN(rta->rta_len);
  354. rta = (void *)(data + off);
  355. rta->rta_type = RTA_OIF;
  356. rta->rta_len = RTA_LENGTH(sizeof(int));
  357. *((int *)(RTA_DATA(rta))) = 1; //if_nametoindex("lo");
  358. off += NLMSG_ALIGN(rta->rta_len);
  359. /* MTU is a subtype in a metrics type */
  360. rta = (void *)(data + off);
  361. rta->rta_type = RTA_METRICS;
  362. rta->rta_len = RTA_LENGTH(0) + RTA_LENGTH(sizeof(int));
  363. off += NLMSG_ALIGN(rta->rta_len);
  364. /* now fill MTU subtype. Note that it fits within above rta_len */
  365. rta = (void *)(((char *) rta) + RTA_LENGTH(0));
  366. rta->rta_type = RTAX_MTU;
  367. rta->rta_len = RTA_LENGTH(sizeof(int));
  368. *((int *)(RTA_DATA(rta))) = mtu;
  369. nh->nlmsg_len = off;
  370. ret = sendto(fd, data, off, 0, (void *)&nladdr, sizeof(nladdr));
  371. if (ret != off)
  372. error(1, errno, "send netlink: %uB != %uB\n", ret, off);
  373. if (close(fd))
  374. error(1, errno, "close netlink");
  375. fprintf(stderr, "route mtu (test): %u\n", mtu);
  376. }
  377. static bool __send_one(int fd, struct msghdr *msg, int flags)
  378. {
  379. int ret;
  380. ret = sendmsg(fd, msg, flags);
  381. if (ret == -1 &&
  382. (errno == EMSGSIZE || errno == ENOMEM || errno == EINVAL))
  383. return false;
  384. if (ret == -1)
  385. error(1, errno, "sendmsg");
  386. if (ret != msg->msg_iov->iov_len)
  387. error(1, 0, "sendto: %d != %llu", ret,
  388. (unsigned long long)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. }