tsctp_upcall.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. * Copyright (C) 2005-2013 Michael Tuexen
  3. * Copyright (C) 2011-2013 Irene Ruengeler
  4. * Copyright (C) 2014-2019 Felix Weinrank
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the project nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #include <sys/types.h>
  33. #ifdef _WIN32
  34. #include <winsock2.h>
  35. #include <ws2tcpip.h>
  36. #include <stdlib.h>
  37. #include <crtdbg.h>
  38. #include <sys/timeb.h>
  39. #else
  40. #include <sys/socket.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include <sys/time.h>
  44. #include <unistd.h>
  45. #include <pthread.h>
  46. #endif
  47. #include <stdarg.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <signal.h>
  52. #include <errno.h>
  53. #include <inttypes.h>
  54. #ifdef LINUX
  55. #include <getopt.h>
  56. #endif
  57. #include <usrsctp.h>
  58. #include "programs_helper.h"
  59. #define TSCTP_CLIENT 1
  60. #define TSCTP_SERVER 2
  61. #define DEFAULT_LENGTH 1024
  62. #define DEFAULT_NUMBER_OF_MESSAGES 1024
  63. #define DEFAULT_PORT 5001
  64. #define BUFFERSIZE (1<<16)
  65. static int par_verbose = 0;
  66. static int par_very_verbose = 0;
  67. static unsigned int done = 0;
  68. struct tsctp_meta {
  69. uint8_t par_role;
  70. uint8_t par_stats_human;
  71. uint8_t par_ordered;
  72. uint64_t par_messages;
  73. uint64_t par_message_length;
  74. uint64_t par_runtime;
  75. uint64_t stat_messages;
  76. uint64_t stat_message_length;
  77. uint64_t stat_notifications;
  78. uint64_t stat_recv_calls;
  79. struct timeval stat_start;
  80. uint64_t stat_fragment_sum;
  81. char *buffer;
  82. };
  83. #ifndef timersub
  84. #define timersub(tvp, uvp, vvp) \
  85. do { \
  86. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  87. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  88. if ((vvp)->tv_usec < 0) { \
  89. (vvp)->tv_sec--; \
  90. (vvp)->tv_usec += 1000000; \
  91. } \
  92. } while (0)
  93. #endif
  94. #ifdef _WIN32
  95. static void
  96. gettimeofday(struct timeval *tv, void *ignore)
  97. {
  98. FILETIME filetime;
  99. ULARGE_INTEGER ularge;
  100. GetSystemTimeAsFileTime(&filetime);
  101. ularge.LowPart = filetime.dwLowDateTime;
  102. ularge.HighPart = filetime.dwHighDateTime;
  103. /* Change base from Jan 1 1601 00:00:00 to Jan 1 1970 00:00:00 */
  104. #if defined(__MINGW32__)
  105. ularge.QuadPart -= 116444736000000000ULL;
  106. #else
  107. ularge.QuadPart -= 116444736000000000UI64;
  108. #endif
  109. /*
  110. * ularge.QuadPart is now the number of 100-nanosecond intervals
  111. * since Jan 1 1970 00:00:00.
  112. */
  113. #if defined(__MINGW32__)
  114. tv->tv_sec = (long)(ularge.QuadPart / 10000000ULL);
  115. tv->tv_usec = (long)((ularge.QuadPart % 10000000ULL) / 10ULL);
  116. #else
  117. tv->tv_sec = (long)(ularge.QuadPart / 10000000UI64);
  118. tv->tv_usec = (long)((ularge.QuadPart % 10000000UI64) / 10UI64);
  119. #endif
  120. }
  121. #endif
  122. char Usage[] =
  123. "Usage: tsctp [options] [address]\n"
  124. "Options:\n"
  125. " -a set adaptation layer indication\n"
  126. " -E local UDP encapsulation port (default 9899)\n"
  127. " -f fragmentation point\n"
  128. " -H human readable statistics"
  129. " -l message length\n"
  130. " -L bind to local IP (default INADDR_ANY)\n"
  131. " -n number of messages sent (0 means infinite)/received\n"
  132. " -D turns Nagle off\n"
  133. " -R socket recv buffer\n"
  134. " -S socket send buffer\n"
  135. " -T time to send messages\n"
  136. " -u use unordered user messages\n"
  137. " -U remote UDP encapsulation port\n"
  138. " -v verbose\n"
  139. " -V very verbose\n"
  140. ;
  141. static void handle_upcall(struct socket *upcall_socket, void *upcall_data, int upcall_flags);
  142. static const char *bytes2human(uint64_t bytes)
  143. {
  144. char *suffix[] = {"", "K", "M", "G", "T"};
  145. char suffix_length = sizeof(suffix) / sizeof(suffix[0]);
  146. int i = 0;
  147. double human_size = bytes;
  148. static char output[200];
  149. if (bytes > 1024) {
  150. for (i = 0; (bytes / 1024) > 0 && i < suffix_length - 1; i++) {
  151. human_size = bytes / 1024.0;
  152. bytes /= 1024;
  153. }
  154. }
  155. if (snprintf(output, sizeof(output), "%.02lf %s", human_size, suffix[i]) < 0) {
  156. output[0] = '\0';
  157. }
  158. return output;
  159. }
  160. static void
  161. handle_accept(struct socket *upcall_socket, void *upcall_data, int upcall_flags)
  162. {
  163. struct socket *conn_sock;
  164. struct sockaddr_in remote_addr;
  165. socklen_t addr_len = sizeof(struct sockaddr_in);
  166. struct tsctp_meta *meta_listening, *meta_accepted;
  167. char addrbuf[INET_ADDRSTRLEN];
  168. meta_listening = (struct tsctp_meta *) upcall_data;
  169. memset(&remote_addr, 0, sizeof(struct sockaddr_in));
  170. if (((conn_sock = usrsctp_accept(upcall_socket, (struct sockaddr *) &remote_addr, &addr_len)) == NULL) && (errno != EINPROGRESS)) {
  171. perror("usrsctp_accept");
  172. exit(EXIT_FAILURE);
  173. }
  174. if (par_verbose) {
  175. printf("Connection accepted from %s:%d\n", inet_ntop(AF_INET, &(remote_addr.sin_addr), addrbuf, INET_ADDRSTRLEN), ntohs(remote_addr.sin_port));
  176. }
  177. meta_accepted = malloc(sizeof(struct tsctp_meta));
  178. if (!meta_accepted) {
  179. printf("malloc() failed!\n");
  180. exit(EXIT_FAILURE);
  181. }
  182. memset(meta_accepted, 0, sizeof(struct tsctp_meta));
  183. meta_accepted->par_role = meta_listening->par_role;
  184. meta_accepted->par_stats_human = meta_listening->par_stats_human;
  185. meta_accepted->buffer = malloc(BUFFERSIZE);
  186. if (!meta_accepted->buffer) {
  187. printf("malloc() failed!\n");
  188. exit(EXIT_FAILURE);
  189. }
  190. usrsctp_set_upcall(conn_sock, handle_upcall, meta_accepted);
  191. }
  192. static void
  193. handle_upcall(struct socket *upcall_socket, void *upcall_data, int upcall_flags)
  194. {
  195. int events = usrsctp_get_events(upcall_socket);
  196. struct tsctp_meta* tsctp_meta = (struct tsctp_meta*) upcall_data;
  197. struct sctp_recvv_rn rn;
  198. ssize_t n;
  199. struct sockaddr_storage addr;
  200. int recv_flags = 0;
  201. socklen_t len = (socklen_t)sizeof(struct sockaddr_storage);
  202. unsigned int infotype = 0;
  203. socklen_t infolen = sizeof(struct sctp_recvv_rn);
  204. struct sctp_rcvinfo *rcvinfo = (struct sctp_rcvinfo *) &rn;
  205. memset(&rn, 0, sizeof(struct sctp_recvv_rn));
  206. struct timeval note_time;
  207. union sctp_notification *snp;
  208. struct sctp_paddr_change *spc;
  209. struct timeval time_now;
  210. struct timeval time_diff;
  211. float seconds;
  212. struct sctp_sndinfo snd_info;
  213. if (events & SCTP_EVENT_READ) {
  214. while ((n = usrsctp_recvv(upcall_socket, tsctp_meta->buffer, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn, &infolen, &infotype, &recv_flags)) > 0) {
  215. if (!tsctp_meta->stat_recv_calls) {
  216. gettimeofday(&tsctp_meta->stat_start, NULL);
  217. }
  218. tsctp_meta->stat_recv_calls++;
  219. if (recv_flags & MSG_NOTIFICATION) {
  220. tsctp_meta->stat_notifications++;
  221. gettimeofday(&note_time, NULL);
  222. if (par_verbose) {
  223. printf("notification arrived at %f\n", note_time.tv_sec + (double)note_time.tv_usec / 1000000.0);
  224. snp = (union sctp_notification *)tsctp_meta->buffer;
  225. if (snp->sn_header.sn_type == SCTP_PEER_ADDR_CHANGE) {
  226. spc = &snp->sn_paddr_change;
  227. printf("SCTP_PEER_ADDR_CHANGE: state=%u, error=%u\n",spc->spc_state, spc->spc_error);
  228. }
  229. }
  230. } else {
  231. if (par_very_verbose) {
  232. if (infotype == SCTP_RECVV_RCVINFO) {
  233. printf("Message received - %zd bytes - %s - sid %u - tsn %u %s\n",
  234. n,
  235. (rcvinfo->rcv_flags & SCTP_UNORDERED) ? "unordered" : "ordered",
  236. rcvinfo->rcv_sid,
  237. rcvinfo->rcv_tsn,
  238. (recv_flags & MSG_EOR) ? "- EOR" : ""
  239. );
  240. } else {
  241. printf("Message received - %zd bytes %s\n", n, (recv_flags & MSG_EOR) ? "- EOR" : "");
  242. }
  243. }
  244. tsctp_meta->stat_fragment_sum += n;
  245. if (recv_flags & MSG_EOR) {
  246. tsctp_meta->stat_messages++;
  247. if (tsctp_meta->stat_message_length == 0) {
  248. tsctp_meta->stat_message_length = tsctp_meta->stat_fragment_sum;
  249. }
  250. }
  251. }
  252. }
  253. if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
  254. perror("usrsctp_recvv");
  255. exit(EXIT_FAILURE);
  256. }
  257. if (n == 0) {
  258. done = 1;
  259. gettimeofday(&time_now, NULL);
  260. timersub(&time_now, &tsctp_meta->stat_start, &time_diff);
  261. seconds = time_diff.tv_sec + (double)time_diff.tv_usec / 1000000.0;
  262. if (tsctp_meta->par_stats_human) {
  263. printf("Connection closed - statistics\n");
  264. printf("\tmessage size : %" PRIu64 "\n", tsctp_meta->stat_message_length);
  265. printf("\tmessages : %" PRIu64 "\n", tsctp_meta->stat_messages);
  266. printf("\trecv() calls : %" PRIu64 "\n", tsctp_meta->stat_recv_calls);
  267. printf("\tnotifications : %" PRIu64 "\n", tsctp_meta->stat_notifications);
  268. printf("\ttransferred : %sByte\n", bytes2human(tsctp_meta->stat_message_length * tsctp_meta->stat_messages));
  269. printf("\truntime : %.2f s\n", seconds);
  270. printf("\tgoodput : %sBit/s\n", bytes2human((double) tsctp_meta->stat_message_length * (double) tsctp_meta->stat_messages / seconds * 8));
  271. } else {
  272. printf("%" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %f, %f, %" PRIu64 "\n",
  273. tsctp_meta->stat_message_length,
  274. tsctp_meta->stat_messages,
  275. tsctp_meta->stat_recv_calls,
  276. tsctp_meta->stat_message_length * tsctp_meta->stat_messages,
  277. seconds,
  278. (double) tsctp_meta->stat_message_length * (double) tsctp_meta->stat_messages / seconds,
  279. tsctp_meta->stat_notifications);
  280. }
  281. fflush(stdout);
  282. usrsctp_close(upcall_socket);
  283. free(tsctp_meta->buffer);
  284. free(tsctp_meta);
  285. return;
  286. }
  287. }
  288. if ((events & SCTP_EVENT_WRITE) && tsctp_meta->par_role == TSCTP_CLIENT && !done) {
  289. memset(&snd_info, 0, sizeof(struct sctp_sndinfo));
  290. if (tsctp_meta->par_ordered == 0) {
  291. snd_info.snd_flags |= SCTP_UNORDERED;
  292. }
  293. while (usrsctp_sendv(upcall_socket, tsctp_meta->buffer, tsctp_meta->par_message_length, NULL, 0, &snd_info, (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) > 0) {
  294. if (tsctp_meta->stat_messages == 0) {
  295. gettimeofday(&tsctp_meta->stat_start, NULL);
  296. }
  297. tsctp_meta->stat_messages++;
  298. if (par_very_verbose) {
  299. printf("Message #%" PRIu64 " sent\n", tsctp_meta->stat_messages);
  300. }
  301. if (tsctp_meta->par_messages && tsctp_meta->par_messages == tsctp_meta->stat_messages) {
  302. break;
  303. }
  304. }
  305. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  306. done = 1;
  307. usrsctp_close(upcall_socket);
  308. printf("client socket %p closed\n", (void *)upcall_socket);
  309. free(tsctp_meta->buffer);
  310. free(tsctp_meta);
  311. return;
  312. }
  313. gettimeofday(&time_now, NULL);
  314. timersub(&time_now, &tsctp_meta->stat_start, &time_diff);
  315. seconds = time_diff.tv_sec + (double)time_diff.tv_usec / 1000000.0;
  316. if ((tsctp_meta->par_messages && tsctp_meta->par_messages == tsctp_meta->stat_messages) ||
  317. (tsctp_meta->par_runtime && tsctp_meta->par_runtime <= seconds)) {
  318. if (par_verbose) {
  319. printf("Runtime or max messages reached - finishing...\n");
  320. }
  321. done = 1;
  322. usrsctp_close(upcall_socket);
  323. free(tsctp_meta->buffer);
  324. free(tsctp_meta);
  325. return;
  326. }
  327. }
  328. return;
  329. }
  330. int main(int argc, char **argv)
  331. {
  332. #ifndef _WIN32
  333. int c;
  334. #endif
  335. struct socket *psock = NULL;
  336. struct sockaddr_in local_addr;
  337. struct sockaddr_in remote_addr;
  338. int optval;
  339. uint16_t local_port;
  340. uint16_t remote_port;
  341. uint16_t local_udp_port;
  342. uint16_t remote_udp_port;
  343. int rcvbufsize = 0;
  344. int sndbufsize = 0;
  345. socklen_t intlen;
  346. int nodelay = 0;
  347. struct sctp_assoc_value av;
  348. struct sctp_udpencaps encaps;
  349. struct tsctp_meta *meta;
  350. uint16_t par_port = DEFAULT_PORT;
  351. uint8_t par_stats_human = 0;
  352. int par_ordered = 1;
  353. int par_message_length = DEFAULT_LENGTH;
  354. int par_messages = DEFAULT_NUMBER_OF_MESSAGES;
  355. int par_runtime = 0;
  356. #ifdef _WIN32
  357. unsigned long src_addr;
  358. #else
  359. in_addr_t src_addr;
  360. #endif
  361. int fragpoint = 0;
  362. struct sctp_setadaptation ind = {0};
  363. #ifdef _WIN32
  364. char *opt;
  365. int optind;
  366. #endif
  367. remote_udp_port = 0;
  368. local_udp_port = 9899;
  369. src_addr = htonl(INADDR_ANY);
  370. memset((void *) &remote_addr, 0, sizeof(struct sockaddr_in));
  371. memset((void *) &local_addr, 0, sizeof(struct sockaddr_in));
  372. #ifndef _WIN32
  373. while ((c = getopt(argc, argv, "a:DE:f:Hl:L:n:p:R:S:T:uU:vV")) != -1)
  374. switch(c) {
  375. case 'a':
  376. ind.ssb_adaptation_ind = atoi(optarg);
  377. break;
  378. case 'D':
  379. nodelay = 1;
  380. break;
  381. case 'E':
  382. local_udp_port = atoi(optarg);
  383. break;
  384. case 'f':
  385. fragpoint = atoi(optarg);
  386. break;
  387. case 'H':
  388. par_stats_human = 1;
  389. break;
  390. case 'l':
  391. par_message_length = atoi(optarg);
  392. break;
  393. case 'L':
  394. if (inet_pton(AF_INET, optarg, &src_addr) != 1) {
  395. printf("Can't parse %s\n", optarg);
  396. exit(EXIT_FAILURE);
  397. }
  398. break;
  399. case 'n':
  400. par_messages = atoi(optarg);
  401. break;
  402. case 'p':
  403. par_port = atoi(optarg);
  404. break;
  405. case 'R':
  406. rcvbufsize = atoi(optarg);
  407. break;
  408. case 'S':
  409. sndbufsize = atoi(optarg);
  410. break;
  411. case 'T':
  412. par_runtime = atoi(optarg);
  413. par_messages = 0;
  414. break;
  415. case 'u':
  416. par_ordered = 0;
  417. break;
  418. case 'U':
  419. remote_udp_port = atoi(optarg);
  420. break;
  421. case 'v':
  422. par_verbose = 1;
  423. break;
  424. case 'V':
  425. par_verbose = 1;
  426. par_very_verbose = 1;
  427. break;
  428. default:
  429. fprintf(stderr, "%s", Usage);
  430. exit(1);
  431. }
  432. #else
  433. for (optind = 1; optind < argc; optind++) {
  434. if (argv[optind][0] == '-') {
  435. switch (argv[optind][1]) {
  436. case 'a':
  437. if (++optind >= argc) {
  438. printf("%s", Usage);
  439. exit(1);
  440. }
  441. opt = argv[optind];
  442. ind.ssb_adaptation_ind = atoi(opt);
  443. break;
  444. case 'D':
  445. nodelay = 1;
  446. break;
  447. case 'E':
  448. if (++optind >= argc) {
  449. printf("%s", Usage);
  450. exit(1);
  451. }
  452. opt = argv[optind];
  453. local_udp_port = atoi(opt);
  454. break;
  455. case 'f':
  456. if (++optind >= argc) {
  457. printf("%s", Usage);
  458. exit(1);
  459. }
  460. opt = argv[optind];
  461. fragpoint = atoi(opt);
  462. break;
  463. case 'H':
  464. par_stats_human = 1;
  465. break;
  466. case 'l':
  467. if (++optind >= argc) {
  468. printf("%s", Usage);
  469. exit(1);
  470. }
  471. opt = argv[optind];
  472. par_message_length = atoi(opt);
  473. break;
  474. case 'L':
  475. if (++optind >= argc) {
  476. printf("%s", Usage);
  477. exit(1);
  478. }
  479. opt = argv[optind];
  480. inet_pton(AF_INET, opt, &src_addr);
  481. break;
  482. case 'n':
  483. if (++optind >= argc) {
  484. printf("%s", Usage);
  485. exit(1);
  486. }
  487. opt = argv[optind];
  488. par_messages = atoi(opt);
  489. break;
  490. case 'p':
  491. if (++optind >= argc) {
  492. printf("%s", Usage);
  493. exit(1);
  494. }
  495. opt = argv[optind];
  496. par_port = atoi(opt);
  497. break;
  498. case 'R':
  499. if (++optind >= argc) {
  500. printf("%s", Usage);
  501. exit(1);
  502. }
  503. opt = argv[optind];
  504. rcvbufsize = atoi(opt);
  505. break;
  506. case 'S':
  507. if (++optind >= argc) {
  508. printf("%s", Usage);
  509. exit(1);
  510. }
  511. opt = argv[optind];
  512. sndbufsize = atoi(opt);
  513. break;
  514. case 'T':
  515. if (++optind >= argc) {
  516. printf("%s", Usage);
  517. exit(1);
  518. }
  519. opt = argv[optind];
  520. par_runtime = atoi(opt);
  521. par_messages = 0;
  522. break;
  523. case 'u':
  524. par_ordered = 0;
  525. break;
  526. case 'U':
  527. if (++optind >= argc) {
  528. printf("%s", Usage);
  529. exit(1);
  530. }
  531. opt = argv[optind];
  532. remote_udp_port = atoi(opt);
  533. break;
  534. case 'v':
  535. par_verbose = 1;
  536. break;
  537. case 'V':
  538. par_verbose = 1;
  539. par_very_verbose = 1;
  540. break;
  541. default:
  542. printf("%s", Usage);
  543. exit(1);
  544. }
  545. } else {
  546. break;
  547. }
  548. }
  549. #endif
  550. meta = malloc(sizeof(struct tsctp_meta));
  551. if (!meta) {
  552. printf("malloc() failed!\n");
  553. exit(EXIT_FAILURE);
  554. }
  555. memset(meta, 0, sizeof(struct tsctp_meta));
  556. meta->buffer = malloc(BUFFERSIZE);
  557. if (!meta->buffer) {
  558. printf("malloc() failed!\n");
  559. exit(EXIT_FAILURE);
  560. }
  561. meta->par_stats_human = par_stats_human;
  562. meta->par_message_length = par_message_length;
  563. meta->par_messages = par_messages;
  564. meta->par_ordered = par_ordered;
  565. meta->par_runtime = par_runtime;
  566. if (optind == argc) {
  567. meta->par_role = TSCTP_SERVER;
  568. local_port = par_port;
  569. remote_port = 0;
  570. } else {
  571. meta->par_role = TSCTP_CLIENT;
  572. local_port = 0;
  573. remote_port = par_port;
  574. }
  575. local_addr.sin_family = AF_INET;
  576. #ifdef HAVE_SIN_LEN
  577. local_addr.sin_len = sizeof(struct sockaddr_in);
  578. #endif
  579. local_addr.sin_port = htons(local_port);
  580. local_addr.sin_addr.s_addr = src_addr;
  581. usrsctp_init(local_udp_port, NULL, debug_printf_stack);
  582. #ifdef SCTP_DEBUG
  583. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
  584. #endif
  585. usrsctp_sysctl_set_sctp_blackhole(2);
  586. usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
  587. usrsctp_sysctl_set_sctp_enable_sack_immediately(1);
  588. if (!(psock = usrsctp_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL))) {
  589. perror("user_socket");
  590. exit(EXIT_FAILURE);
  591. }
  592. optval = 1;
  593. if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_RECVRCVINFO, &optval, sizeof(optval)) < 0) {
  594. perror("usrsctp_setsockopt SCTP_RECVRCVINFO");
  595. }
  596. usrsctp_set_non_blocking(psock, 1);
  597. if (usrsctp_bind(psock, (struct sockaddr *) &local_addr, sizeof(struct sockaddr_in)) == -1) {
  598. perror("usrsctp_bind");
  599. exit(1);
  600. }
  601. if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_ADAPTATION_LAYER, (const void*)&ind, (socklen_t)sizeof(struct sctp_setadaptation)) < 0) {
  602. perror("setsockopt");
  603. }
  604. if (meta->par_role == TSCTP_SERVER) {
  605. if (rcvbufsize) {
  606. if (usrsctp_setsockopt(psock, SOL_SOCKET, SO_RCVBUF, &rcvbufsize, sizeof(int)) < 0) {
  607. perror("setsockopt: rcvbuf");
  608. }
  609. }
  610. if (par_verbose) {
  611. intlen = sizeof(int);
  612. if (usrsctp_getsockopt(psock, SOL_SOCKET, SO_RCVBUF, &rcvbufsize, (socklen_t *)&intlen) < 0) {
  613. perror("getsockopt: rcvbuf");
  614. } else {
  615. fprintf(stdout, "Receive buffer size: %d.\n", rcvbufsize);
  616. }
  617. }
  618. if (usrsctp_listen(psock, 1) < 0) {
  619. perror("usrsctp_listen");
  620. exit(EXIT_FAILURE);
  621. }
  622. usrsctp_set_upcall(psock, handle_accept, meta);
  623. while (1) {
  624. #ifdef _WIN32
  625. Sleep(1000);
  626. #else
  627. sleep(1);
  628. #endif
  629. }
  630. } else {
  631. memset(&encaps, 0, sizeof(struct sctp_udpencaps));
  632. encaps.sue_address.ss_family = AF_INET;
  633. encaps.sue_port = htons(remote_udp_port);
  634. if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
  635. perror("setsockopt");
  636. }
  637. remote_addr.sin_family = AF_INET;
  638. #ifdef HAVE_SIN_LEN
  639. remote_addr.sin_len = sizeof(struct sockaddr_in);
  640. #endif
  641. if (!inet_pton(AF_INET, argv[optind], &remote_addr.sin_addr.s_addr)){
  642. printf("error: invalid destination address\n");
  643. exit(EXIT_FAILURE);
  644. }
  645. remote_addr.sin_port = htons(remote_port);
  646. memset(meta->buffer, 'X', BUFFERSIZE);
  647. usrsctp_set_upcall(psock, handle_upcall, meta);
  648. usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_NODELAY, &nodelay, sizeof(nodelay));
  649. if (fragpoint) {
  650. av.assoc_id = 0;
  651. av.assoc_value = fragpoint;
  652. if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_MAXSEG, &av, sizeof(struct sctp_assoc_value)) < 0) {
  653. perror("setsockopt: SCTP_MAXSEG");
  654. }
  655. }
  656. if (sndbufsize) {
  657. if (usrsctp_setsockopt(psock, SOL_SOCKET, SO_SNDBUF, &sndbufsize, sizeof(int)) < 0) {
  658. perror("setsockopt: sndbuf");
  659. }
  660. }
  661. if (par_verbose) {
  662. intlen = sizeof(int);
  663. if (usrsctp_getsockopt(psock, SOL_SOCKET, SO_SNDBUF, &sndbufsize, (socklen_t *)&intlen) < 0) {
  664. perror("setsockopt: SO_SNDBUF");
  665. } else {
  666. fprintf(stdout,"Send buffer size: %d.\n", sndbufsize);
  667. }
  668. }
  669. if (usrsctp_connect(psock, (struct sockaddr *) &remote_addr, sizeof(struct sockaddr_in)) == -1 ) {
  670. if (errno != EINPROGRESS) {
  671. perror("usrsctp_connect");
  672. exit(EXIT_FAILURE);
  673. }
  674. }
  675. while (!done) {
  676. #ifdef _WIN32
  677. Sleep(1000);
  678. #else
  679. sleep(1);
  680. #endif
  681. }
  682. if (par_verbose) {
  683. printf("Finished... \n");
  684. }
  685. }
  686. while (usrsctp_finish() != 0) {
  687. #ifdef _WIN32
  688. Sleep(1000);
  689. #else
  690. sleep(1);
  691. #endif
  692. }
  693. return 0;
  694. }