tsctp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * Copyright (C) 2005-2013 Michael Tuexen
  3. * Copyright (C) 2011-2013 Irene Ruengeler
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the project nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <sys/types.h>
  32. #ifdef _WIN32
  33. #include <winsock2.h>
  34. #include <ws2tcpip.h>
  35. #include <stdlib.h>
  36. #include <crtdbg.h>
  37. #include <sys/timeb.h>
  38. #else
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <sys/time.h>
  43. #include <unistd.h>
  44. #include <pthread.h>
  45. #endif
  46. #include <stdarg.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <signal.h>
  51. #include <errno.h>
  52. #include <time.h>
  53. #ifdef LINUX
  54. #include <getopt.h>
  55. #endif
  56. #include <usrsctp.h>
  57. #include "programs_helper.h"
  58. /* global for the send callback, but used in kernel version as well */
  59. static unsigned long number_of_messages;
  60. static char *buffer;
  61. static int length;
  62. static struct sockaddr_in remote_addr;
  63. static int unordered;
  64. uint32_t optval = 1;
  65. struct socket *psock = NULL;
  66. static struct timeval start_time;
  67. unsigned int runtime = 0;
  68. static int policy = 0;
  69. static unsigned int timetolive = 0;
  70. static unsigned long cb_messages = 0;
  71. static unsigned long long cb_first_length = 0;
  72. static unsigned long long cb_sum = 0;
  73. static unsigned int use_cb = 0;
  74. #ifdef _WIN32
  75. static void
  76. gettimeofday(struct timeval *tv, void *ignore)
  77. {
  78. FILETIME filetime;
  79. ULARGE_INTEGER ularge;
  80. GetSystemTimeAsFileTime(&filetime);
  81. ularge.LowPart = filetime.dwLowDateTime;
  82. ularge.HighPart = filetime.dwHighDateTime;
  83. /* Change base from Jan 1 1601 00:00:00 to Jan 1 1970 00:00:00 */
  84. #if defined(__MINGW32__)
  85. ularge.QuadPart -= 116444736000000000ULL;
  86. #else
  87. ularge.QuadPart -= 116444736000000000UI64;
  88. #endif
  89. /*
  90. * ularge.QuadPart is now the number of 100-nanosecond intervals
  91. * since Jan 1 1970 00:00:00.
  92. */
  93. #if defined(__MINGW32__)
  94. tv->tv_sec = (long)(ularge.QuadPart / 10000000ULL);
  95. tv->tv_usec = (long)((ularge.QuadPart % 10000000ULL) / 10ULL);
  96. #else
  97. tv->tv_sec = (long)(ularge.QuadPart / 10000000UI64);
  98. tv->tv_usec = (long)((ularge.QuadPart % 10000000UI64) / 10UI64);
  99. #endif
  100. }
  101. #endif
  102. char Usage[] =
  103. "Usage: tsctp [options] [address]\n"
  104. "Options:\n"
  105. " -a set adaptation layer indication\n"
  106. " -c use callback API\n"
  107. " -d time in seconds after which a status update is printed\n"
  108. " -D turns Nagle off\n"
  109. " -E local UDP encapsulation port (default 9899)\n"
  110. " -f fragmentation point\n"
  111. " -l message length\n"
  112. " -L bind to local IP (default INADDR_ANY)\n"
  113. " -n number of messages sent (0 means infinite)/received\n"
  114. " -p port number\n"
  115. " -P partial reliability policy to use (0=none (default), 1=ttl, 2=rtx, 3=buf)\n"
  116. " -R socket recv buffer\n"
  117. " -S socket send buffer\n"
  118. " -t based on -P the time to live, number of retransmissions, or priority for messages\n"
  119. " -T time to send messages\n"
  120. " -u use unordered user messages\n"
  121. " -U remote UDP encapsulation port\n"
  122. " -v verbose\n"
  123. " -V very verbose\n"
  124. ;
  125. #define DEFAULT_LENGTH 1024
  126. #define DEFAULT_NUMBER_OF_MESSAGES 1024
  127. #define DEFAULT_PORT 5001
  128. #define BUFFERSIZE (1<<16)
  129. static int verbose, very_verbose;
  130. static unsigned int done;
  131. static unsigned int round_duration;
  132. void stop_sender(int sig)
  133. {
  134. done = 1;
  135. }
  136. static time_t calc_round_timeout(struct timeval round_start)
  137. {
  138. time_t round_timeout = round_start.tv_sec + round_duration;
  139. if (round_start.tv_usec >= 500000) {
  140. round_timeout++;
  141. }
  142. return round_timeout;
  143. }
  144. #ifdef _WIN32
  145. static DWORD WINAPI
  146. #else
  147. static void *
  148. #endif
  149. handle_connection(void *arg)
  150. {
  151. ssize_t n;
  152. char *buf;
  153. #if !defined(_WIN32)
  154. pthread_t tid;
  155. #endif
  156. struct socket *conn_sock;
  157. struct timeval time_start, time_now, time_diff;
  158. double seconds;
  159. unsigned long recv_calls = 0;
  160. unsigned long notifications = 0;
  161. int flags;
  162. struct sockaddr_in addr;
  163. socklen_t len;
  164. union sctp_notification *snp;
  165. struct sctp_paddr_change *spc;
  166. struct timeval note_time;
  167. unsigned int infotype;
  168. struct sctp_recvv_rn rn;
  169. socklen_t infolen = sizeof(struct sctp_recvv_rn);
  170. unsigned long messages = 0;
  171. unsigned long long first_length = 0;
  172. unsigned long long sum = 0;
  173. unsigned long long round_bytes = 0;
  174. struct timeval round_start = (struct timeval){0};
  175. time_t round_timeout = 0;
  176. conn_sock = *(struct socket **)arg;
  177. #if !defined(_WIN32)
  178. tid = pthread_self();
  179. pthread_detach(tid);
  180. #endif
  181. buf = malloc(BUFFERSIZE);
  182. flags = 0;
  183. len = (socklen_t)sizeof(struct sockaddr_in);
  184. infotype = 0;
  185. memset(&rn, 0, sizeof(struct sctp_recvv_rn));
  186. n = usrsctp_recvv(conn_sock, buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
  187. &infolen, &infotype, &flags);
  188. gettimeofday(&time_start, NULL);
  189. if (round_duration > 0) {
  190. gettimeofday(&round_start, NULL);
  191. round_timeout = calc_round_timeout(round_start);
  192. }
  193. while (n > 0) {
  194. recv_calls++;
  195. if (flags & MSG_NOTIFICATION) {
  196. notifications++;
  197. gettimeofday(&note_time, NULL);
  198. printf("notification arrived at %f\n", note_time.tv_sec+(double)note_time.tv_usec/1000000.0);
  199. snp = (union sctp_notification *)buf;
  200. if (snp->sn_header.sn_type == SCTP_PEER_ADDR_CHANGE) {
  201. spc = &snp->sn_paddr_change;
  202. printf("SCTP_PEER_ADDR_CHANGE: state=%u, error=%u\n",spc->spc_state, spc->spc_error);
  203. }
  204. } else {
  205. if (very_verbose) {
  206. printf("Message received\n");
  207. }
  208. sum += n;
  209. if (flags & MSG_EOR) {
  210. messages++;
  211. if (first_length == 0) {
  212. first_length = sum;
  213. }
  214. if (round_duration > 0) {
  215. round_bytes += first_length;
  216. }
  217. }
  218. }
  219. if (round_duration > 0 && round_timeout <= time(NULL)) {
  220. gettimeofday(&time_now, NULL);
  221. timersub(&time_now, &round_start, &time_diff);
  222. seconds = time_diff.tv_sec + (double)time_diff.tv_usec/1000000.0;
  223. fprintf(stdout, "throughput for the last %f seconds: %f B/s\n", seconds, (double)round_bytes / seconds);
  224. round_bytes = 0;
  225. gettimeofday(&round_start, NULL);
  226. round_timeout = calc_round_timeout(round_start);
  227. }
  228. flags = 0;
  229. len = (socklen_t)sizeof(struct sockaddr_in);
  230. infolen = sizeof(struct sctp_recvv_rn);
  231. infotype = 0;
  232. memset(&rn, 0, sizeof(struct sctp_recvv_rn));
  233. n = usrsctp_recvv(conn_sock, (void *) buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
  234. &infolen, &infotype, &flags);
  235. }
  236. if (n < 0) {
  237. perror("sctp_recvv");
  238. }
  239. gettimeofday(&time_now, NULL);
  240. timersub(&time_now, &time_start, &time_diff);
  241. seconds = time_diff.tv_sec + (double)time_diff.tv_usec/1000000.0;
  242. printf("%llu, %lu, %lu, %llu, %f, %f, %lu\n",
  243. first_length, messages, recv_calls, sum, seconds, (double)first_length * (double)messages / seconds, notifications);
  244. fflush(stdout);
  245. usrsctp_close(conn_sock);
  246. free(buf);
  247. #ifdef _WIN32
  248. return 0;
  249. #else
  250. return (NULL);
  251. #endif
  252. }
  253. static int
  254. send_cb(struct socket *sock, uint32_t sb_free, void *ulp_info) {
  255. struct sctp_sendv_spa sendv_spa;
  256. if ((cb_messages == 0) && verbose) {
  257. printf("Start sending ");
  258. if (number_of_messages > 0) {
  259. printf("%ld messages ", (long)number_of_messages);
  260. }
  261. if (runtime > 0) {
  262. printf("for %u seconds ...", runtime);
  263. }
  264. printf("\n");
  265. fflush(stdout);
  266. }
  267. sendv_spa.sendv_flags = SCTP_SEND_SNDINFO_VALID | SCTP_SEND_PRINFO_VALID;
  268. sendv_spa.sendv_sndinfo.snd_sid = 0;
  269. sendv_spa.sendv_sndinfo.snd_flags = 0;
  270. if (unordered != 0) {
  271. sendv_spa.sendv_sndinfo.snd_flags |= SCTP_UNORDERED;
  272. }
  273. sendv_spa.sendv_sndinfo.snd_ppid = 0;
  274. sendv_spa.sendv_sndinfo.snd_context = 0;
  275. sendv_spa.sendv_sndinfo.snd_assoc_id = 0;
  276. sendv_spa.sendv_prinfo.pr_policy = 0;
  277. switch (policy) {
  278. #ifdef SCTP_PR_SCTP_NONE
  279. case 0:
  280. sendv_spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_NONE;
  281. break;
  282. #endif
  283. #ifdef SCTP_PR_SCTP_TTL
  284. case 1:
  285. sendv_spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_TTL;
  286. break;
  287. #endif
  288. #ifdef SCTP_PR_SCTP_RTX
  289. case 2:
  290. sendv_spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_RTX;
  291. break;
  292. #endif
  293. #ifdef SCTP_PR_SCTP_BUF
  294. case 3:
  295. sendv_spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_BUF;
  296. break;
  297. #endif
  298. default:
  299. printf("Unknown PR-SCTP policy.\n");
  300. break;
  301. }
  302. sendv_spa.sendv_prinfo.pr_value = timetolive;
  303. while (!done && ((number_of_messages == 0) || (cb_messages < (number_of_messages - 1)))) {
  304. if (very_verbose) {
  305. printf("Sending message number %lu.\n", cb_messages + 1);
  306. }
  307. if (usrsctp_sendv(psock, buffer, length,
  308. (struct sockaddr *) &remote_addr, 1,
  309. (void *)&sendv_spa, (socklen_t)sizeof(struct sctp_sendv_spa), SCTP_SENDV_SPA,
  310. 0) < 0) {
  311. if (errno != EWOULDBLOCK && errno != EAGAIN) {
  312. perror("usrsctp_sendv (cb)");
  313. exit(1);
  314. } else {
  315. if (very_verbose){
  316. printf("EWOULDBLOCK or EAGAIN for message number %lu - will retry\n", cb_messages + 1);
  317. }
  318. /* send until EWOULDBLOCK then exit callback. */
  319. return (1);
  320. }
  321. }
  322. cb_messages++;
  323. }
  324. if ((done == 1) || (cb_messages == (number_of_messages - 1))) {
  325. if (very_verbose) {
  326. printf("Sending final message number %lu.\n", cb_messages + 1);
  327. }
  328. sendv_spa.sendv_sndinfo.snd_flags |= SCTP_EOF;
  329. if (usrsctp_sendv(psock, buffer, length, (struct sockaddr *) &remote_addr, 1,
  330. (void *)&sendv_spa, (socklen_t)sizeof(struct sctp_sendv_spa), SCTP_SENDV_SPA,
  331. 0) < 0) {
  332. if (errno != EWOULDBLOCK && errno != EAGAIN) {
  333. perror("usrsctp_sendv (cb)");
  334. exit(1);
  335. } else {
  336. if (very_verbose){
  337. printf("EWOULDBLOCK or EAGAIN for final message number %lu - will retry\n", cb_messages + 1);
  338. }
  339. /* send until EWOULDBLOCK then exit callback. */
  340. return (1);
  341. }
  342. }
  343. cb_messages++;
  344. done = 2;
  345. }
  346. return (1);
  347. }
  348. static int
  349. server_receive_cb(struct socket *sock, union sctp_sockstore addr, void *data,
  350. size_t datalen, struct sctp_rcvinfo rcv, int flags, void *ulp_info)
  351. {
  352. struct timeval now, diff_time;
  353. double seconds;
  354. if (data == NULL) {
  355. gettimeofday(&now, NULL);
  356. timersub(&now, &start_time, &diff_time);
  357. seconds = diff_time.tv_sec + (double)diff_time.tv_usec / 1000000.0;
  358. printf("%llu, %lu, %llu, %f, %f\n",
  359. cb_first_length, cb_messages, cb_sum, seconds, (double)cb_first_length * (double)cb_messages / seconds);
  360. usrsctp_close(sock);
  361. cb_first_length = 0;
  362. cb_sum = 0;
  363. cb_messages = 0;
  364. return (1);
  365. }
  366. if (cb_first_length == 0) {
  367. cb_first_length = (unsigned int)datalen;
  368. gettimeofday(&start_time, NULL);
  369. }
  370. cb_sum += datalen;
  371. cb_messages++;
  372. free(data);
  373. return (1);
  374. }
  375. static int
  376. client_receive_cb(struct socket *sock, union sctp_sockstore addr, void *data,
  377. size_t datalen, struct sctp_rcvinfo rcv, int flags, void *ulp_info)
  378. {
  379. free(data);
  380. return (1);
  381. }
  382. int main(int argc, char **argv)
  383. {
  384. #ifndef _WIN32
  385. int c, rc;
  386. #endif
  387. socklen_t addr_len;
  388. struct sockaddr_in local_addr;
  389. struct timeval time_start, time_now, time_diff;
  390. int client;
  391. uint16_t local_port, remote_port, port, local_udp_port, remote_udp_port;
  392. int rcvbufsize=0, sndbufsize=0, myrcvbufsize, mysndbufsize;
  393. socklen_t intlen;
  394. double seconds;
  395. double throughput;
  396. int nodelay = 0;
  397. struct sctp_assoc_value av;
  398. struct sctp_udpencaps encaps;
  399. struct sctp_sendv_spa sendv_spa;
  400. unsigned long messages = 0;
  401. #ifdef _WIN32
  402. unsigned long srcAddr;
  403. HANDLE tid;
  404. #else
  405. in_addr_t srcAddr;
  406. pthread_t tid;
  407. #endif
  408. int fragpoint = 0;
  409. struct sctp_setadaptation ind = {0};
  410. #ifdef _WIN32
  411. char *opt;
  412. int optind;
  413. #endif
  414. unordered = 0;
  415. length = DEFAULT_LENGTH;
  416. number_of_messages = DEFAULT_NUMBER_OF_MESSAGES;
  417. port = DEFAULT_PORT;
  418. remote_udp_port = 0;
  419. local_udp_port = 9899;
  420. verbose = 0;
  421. very_verbose = 0;
  422. srcAddr = htonl(INADDR_ANY);
  423. round_duration = 0;
  424. memset((void *) &remote_addr, 0, sizeof(struct sockaddr_in));
  425. memset((void *) &local_addr, 0, sizeof(struct sockaddr_in));
  426. #ifndef _WIN32
  427. while ((c = getopt(argc, argv, "a:cd:DE:f:l:L:n:p:P:R:S:t:T:uU:vV")) != -1)
  428. switch(c) {
  429. case 'a':
  430. ind.ssb_adaptation_ind = atoi(optarg);
  431. break;
  432. case 'c':
  433. use_cb = 1;
  434. break;
  435. case 'd':
  436. round_duration = atoi(optarg);
  437. break;
  438. case 'D':
  439. nodelay = 1;
  440. break;
  441. case 'l':
  442. length = atoi(optarg);
  443. break;
  444. case 'n':
  445. number_of_messages = atoi(optarg);
  446. break;
  447. case 'p':
  448. port = atoi(optarg);
  449. break;
  450. case 'P':
  451. policy = atoi(optarg);
  452. break;
  453. case 'E':
  454. local_udp_port = atoi(optarg);
  455. break;
  456. case 'f':
  457. fragpoint = atoi(optarg);
  458. break;
  459. case 'L':
  460. if (inet_pton(AF_INET, optarg, &srcAddr) != 1) {
  461. printf("Can't parse %s\n", optarg);
  462. }
  463. break;
  464. case 'R':
  465. rcvbufsize = atoi(optarg);
  466. break;
  467. case 'S':
  468. sndbufsize = atoi(optarg);
  469. break;
  470. case 't':
  471. timetolive = atoi(optarg);
  472. break;
  473. case 'T':
  474. runtime = atoi(optarg);
  475. number_of_messages = 0;
  476. break;
  477. case 'u':
  478. unordered = 1;
  479. break;
  480. case 'U':
  481. remote_udp_port = atoi(optarg);
  482. break;
  483. case 'v':
  484. verbose = 1;
  485. break;
  486. case 'V':
  487. verbose = 1;
  488. very_verbose = 1;
  489. break;
  490. default:
  491. fprintf(stderr, "%s", Usage);
  492. exit(1);
  493. }
  494. #else
  495. for (optind = 1; optind < argc; optind++) {
  496. if (argv[optind][0] == '-') {
  497. switch (argv[optind][1]) {
  498. case 'a':
  499. if (++optind >= argc) {
  500. printf("%s", Usage);
  501. exit(1);
  502. }
  503. opt = argv[optind];
  504. ind.ssb_adaptation_ind = atoi(opt);
  505. break;
  506. case 'c':
  507. use_cb = 1;
  508. break;
  509. case 'l':
  510. if (++optind >= argc) {
  511. printf("%s", Usage);
  512. exit(1);
  513. }
  514. opt = argv[optind];
  515. length = atoi(opt);
  516. break;
  517. case 'p':
  518. if (++optind >= argc) {
  519. printf("%s", Usage);
  520. exit(1);
  521. }
  522. opt = argv[optind];
  523. port = atoi(opt);
  524. break;
  525. case 'P':
  526. if (++optind >= argc) {
  527. printf("%s", Usage);
  528. exit(1);
  529. }
  530. opt = argv[optind];
  531. policy = atoi(opt);
  532. break;
  533. case 'n':
  534. if (++optind >= argc) {
  535. printf("%s", Usage);
  536. exit(1);
  537. }
  538. opt = argv[optind];
  539. number_of_messages = atoi(opt);
  540. break;
  541. case 'f':
  542. if (++optind >= argc) {
  543. printf("%s", Usage);
  544. exit(1);
  545. }
  546. opt = argv[optind];
  547. fragpoint = atoi(opt);
  548. break;
  549. case 'L':
  550. if (++optind >= argc) {
  551. printf("%s", Usage);
  552. exit(1);
  553. }
  554. opt = argv[optind];
  555. inet_pton(AF_INET, opt, &srcAddr);
  556. break;
  557. case 'U':
  558. if (++optind >= argc) {
  559. printf("%s", Usage);
  560. exit(1);
  561. }
  562. opt = argv[optind];
  563. remote_udp_port = atoi(opt);
  564. break;
  565. case 'E':
  566. if (++optind >= argc) {
  567. printf("%s", Usage);
  568. exit(1);
  569. }
  570. opt = argv[optind];
  571. local_udp_port = atoi(opt);
  572. break;
  573. case 'R':
  574. if (++optind >= argc) {
  575. printf("%s", Usage);
  576. exit(1);
  577. }
  578. opt = argv[optind];
  579. rcvbufsize = atoi(opt);
  580. break;
  581. case 'S':
  582. if (++optind >= argc) {
  583. printf("%s", Usage);
  584. exit(1);
  585. }
  586. opt = argv[optind];
  587. sndbufsize = atoi(opt);
  588. break;
  589. case 't':
  590. if (++optind >= argc) {
  591. printf("%s", Usage);
  592. exit(1);
  593. }
  594. opt = argv[optind];
  595. timetolive = atoi(opt);
  596. break;
  597. case 'T':
  598. if (++optind >= argc) {
  599. printf("%s", Usage);
  600. exit(1);
  601. }
  602. opt = argv[optind];
  603. runtime = atoi(opt);
  604. number_of_messages = 0;
  605. break;
  606. case 'u':
  607. unordered = 1;
  608. break;
  609. case 'v':
  610. verbose = 1;
  611. break;
  612. case 'V':
  613. verbose = 1;
  614. very_verbose = 1;
  615. break;
  616. case 'D':
  617. nodelay = 1;
  618. break;
  619. default:
  620. printf("%s", Usage);
  621. exit(1);
  622. }
  623. } else {
  624. break;
  625. }
  626. }
  627. #endif
  628. if (optind == argc) {
  629. client = 0;
  630. local_port = port;
  631. remote_port = 0;
  632. } else {
  633. client = 1;
  634. local_port = 0;
  635. remote_port = port;
  636. }
  637. local_addr.sin_family = AF_INET;
  638. #ifdef HAVE_SIN_LEN
  639. local_addr.sin_len = sizeof(struct sockaddr_in);
  640. #endif
  641. local_addr.sin_port = htons(local_port);
  642. local_addr.sin_addr.s_addr = srcAddr;
  643. usrsctp_init(local_udp_port, NULL, debug_printf_stack);
  644. #ifdef SCTP_DEBUG
  645. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
  646. #endif
  647. usrsctp_sysctl_set_sctp_blackhole(2);
  648. usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
  649. usrsctp_sysctl_set_sctp_enable_sack_immediately(1);
  650. if (client) {
  651. if (use_cb) {
  652. if (!(psock = usrsctp_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP, client_receive_cb, send_cb, length, NULL))) {
  653. perror("user_socket");
  654. exit(1);
  655. }
  656. } else {
  657. if (!(psock = usrsctp_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL))) {
  658. perror("user_socket");
  659. exit(1);
  660. }
  661. }
  662. } else {
  663. if (use_cb) {
  664. if (!(psock = usrsctp_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP, server_receive_cb, NULL, 0, NULL))) {
  665. perror("user_socket");
  666. exit(1);
  667. }
  668. } else {
  669. if (!(psock = usrsctp_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL))) {
  670. perror("user_socket");
  671. exit(1);
  672. }
  673. }
  674. }
  675. if (usrsctp_bind(psock, (struct sockaddr *)&local_addr, sizeof(struct sockaddr_in)) == -1) {
  676. perror("usrsctp_bind");
  677. exit(1);
  678. }
  679. if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_ADAPTATION_LAYER, (const void*)&ind, (socklen_t)sizeof(struct sctp_setadaptation)) < 0) {
  680. perror("setsockopt");
  681. }
  682. if (!client) {
  683. if (rcvbufsize) {
  684. if (usrsctp_setsockopt(psock, SOL_SOCKET, SO_RCVBUF, &rcvbufsize, sizeof(int)) < 0) {
  685. perror("setsockopt: rcvbuf");
  686. }
  687. }
  688. if (verbose) {
  689. intlen = sizeof(int);
  690. if (usrsctp_getsockopt(psock, SOL_SOCKET, SO_RCVBUF, &myrcvbufsize, (socklen_t *)&intlen) < 0) {
  691. perror("getsockopt: rcvbuf");
  692. } else {
  693. fprintf(stdout,"Receive buffer size: %d.\n", myrcvbufsize);
  694. }
  695. }
  696. if (usrsctp_listen(psock, 1) < 0) {
  697. perror("usrsctp_listen");
  698. exit(1);
  699. }
  700. while (1) {
  701. memset(&remote_addr, 0, sizeof(struct sockaddr_in));
  702. addr_len = sizeof(struct sockaddr_in);
  703. if (use_cb) {
  704. if (usrsctp_accept(psock, (struct sockaddr *) &remote_addr, &addr_len) == NULL) {
  705. perror("usrsctp_accept");
  706. continue;
  707. }
  708. } else {
  709. struct socket **conn_sock;
  710. conn_sock = (struct socket **) malloc(sizeof(struct socket *));
  711. if ((*conn_sock = usrsctp_accept(psock, (struct sockaddr *) &remote_addr, &addr_len)) == NULL) {
  712. perror("usrsctp_accept");
  713. continue;
  714. }
  715. #ifdef _WIN32
  716. if ((tid = CreateThread(NULL, 0, &handle_connection, (void *)conn_sock, 0, NULL)) == NULL) {
  717. fprintf(stderr, "CreateThread() failed with error: %lu\n", GetLastError());
  718. #else
  719. if ((rc = pthread_create(&tid, NULL, &handle_connection, (void *)conn_sock)) != 0) {
  720. fprintf(stderr, "pthread_create: %s\n", strerror(rc));
  721. #endif
  722. usrsctp_close(*conn_sock);
  723. continue;
  724. }
  725. }
  726. if (verbose) {
  727. /* const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
  728. inet_ntoa(remote_addr.sin_addr) */
  729. char addrbuf[INET_ADDRSTRLEN];
  730. printf("Connection accepted from %s:%d\n", inet_ntop(AF_INET, &(remote_addr.sin_addr), addrbuf, INET_ADDRSTRLEN), ntohs(remote_addr.sin_port));
  731. }
  732. }
  733. /* usrsctp_close(psock); unreachable */
  734. } else {
  735. memset(&encaps, 0, sizeof(struct sctp_udpencaps));
  736. encaps.sue_address.ss_family = AF_INET;
  737. encaps.sue_port = htons(remote_udp_port);
  738. if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
  739. perror("setsockopt");
  740. }
  741. remote_addr.sin_family = AF_INET;
  742. #ifdef HAVE_SIN_LEN
  743. remote_addr.sin_len = sizeof(struct sockaddr_in);
  744. #endif
  745. if (!inet_pton(AF_INET, argv[optind], &remote_addr.sin_addr.s_addr)){
  746. printf("error: invalid destination address\n");
  747. exit(1);
  748. }
  749. remote_addr.sin_port = htons(remote_port);
  750. /* TODO fragpoint stuff */
  751. if (nodelay == 1) {
  752. optval = 1;
  753. } else {
  754. optval = 0;
  755. }
  756. usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_NODELAY, &optval, sizeof(int));
  757. if (fragpoint) {
  758. av.assoc_id = 0;
  759. av.assoc_value = fragpoint;
  760. if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_MAXSEG, &av, sizeof(struct sctp_assoc_value)) < 0) {
  761. perror("setsockopt: SCTP_MAXSEG");
  762. }
  763. }
  764. if (sndbufsize) {
  765. if (usrsctp_setsockopt(psock, SOL_SOCKET, SO_SNDBUF, &sndbufsize, sizeof(int)) < 0) {
  766. perror("setsockopt: sndbuf");
  767. }
  768. }
  769. if (verbose) {
  770. intlen = sizeof(int);
  771. if (usrsctp_getsockopt(psock, SOL_SOCKET, SO_SNDBUF, &mysndbufsize, (socklen_t *)&intlen) < 0) {
  772. perror("setsockopt: SO_SNDBUF");
  773. } else {
  774. fprintf(stdout,"Send buffer size: %d.\n", mysndbufsize);
  775. }
  776. }
  777. buffer = malloc(length);
  778. memset(buffer, 'b', length);
  779. if (usrsctp_connect(psock, (struct sockaddr *) &remote_addr, sizeof(struct sockaddr_in)) == -1 ) {
  780. perror("usrsctp_connect");
  781. exit(1);
  782. }
  783. gettimeofday(&time_start, NULL);
  784. done = 0;
  785. if (runtime > 0) {
  786. #ifndef _WIN32
  787. signal(SIGALRM, stop_sender);
  788. alarm(runtime);
  789. #else
  790. fprintf(stderr, "You cannot set the runtime in Windows yet\n");
  791. exit(-1);
  792. #endif
  793. }
  794. if (use_cb) {
  795. while (done < 2 && (cb_messages < (number_of_messages - 1))) {
  796. #ifdef _WIN32
  797. Sleep(1000);
  798. #else
  799. sleep(1);
  800. #endif
  801. }
  802. } else {
  803. sendv_spa.sendv_flags = SCTP_SEND_SNDINFO_VALID | SCTP_SEND_PRINFO_VALID;
  804. sendv_spa.sendv_sndinfo.snd_sid = 0;
  805. sendv_spa.sendv_sndinfo.snd_flags = 0;
  806. if (unordered != 0) {
  807. sendv_spa.sendv_sndinfo.snd_flags |= SCTP_UNORDERED;
  808. }
  809. sendv_spa.sendv_sndinfo.snd_ppid = 0;
  810. sendv_spa.sendv_sndinfo.snd_context = 0;
  811. sendv_spa.sendv_sndinfo.snd_assoc_id = 0;
  812. sendv_spa.sendv_prinfo.pr_policy = 0;
  813. switch (policy) {
  814. #ifdef SCTP_PR_SCTP_NONE
  815. case 0:
  816. sendv_spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_NONE;
  817. break;
  818. #endif
  819. #ifdef SCTP_PR_SCTP_TTL
  820. case 1:
  821. sendv_spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_TTL;
  822. break;
  823. #endif
  824. #ifdef SCTP_PR_SCTP_RTX
  825. case 2:
  826. sendv_spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_RTX;
  827. break;
  828. #endif
  829. #ifdef SCTP_PR_SCTP_BUF
  830. case 3:
  831. sendv_spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_BUF;
  832. break;
  833. #endif
  834. default:
  835. printf("Unknown PR-SCTP policy.\n");
  836. break;
  837. }
  838. sendv_spa.sendv_prinfo.pr_value = timetolive;
  839. if (verbose) {
  840. printf("Start sending ");
  841. if (number_of_messages > 0) {
  842. printf("%ld messages ", (long)number_of_messages);
  843. }
  844. if (runtime > 0) {
  845. printf("for %u seconds ...", runtime);
  846. }
  847. printf("\n");
  848. fflush(stdout);
  849. }
  850. while (!done && ((number_of_messages == 0) || (messages < (number_of_messages - 1)))) {
  851. if (very_verbose) {
  852. printf("Sending message number %lu.\n", messages + 1);
  853. }
  854. if (usrsctp_sendv(psock, buffer, length, (struct sockaddr *) &remote_addr, 1,
  855. (void *)&sendv_spa, (socklen_t)sizeof(struct sctp_sendv_spa), SCTP_SENDV_SPA,
  856. 0) < 0) {
  857. perror("usrsctp_sendv");
  858. exit(1);
  859. }
  860. messages++;
  861. }
  862. if (very_verbose) {
  863. printf("Sending message number %lu.\n", messages + 1);
  864. }
  865. sendv_spa.sendv_sndinfo.snd_flags |= SCTP_EOF;
  866. if (usrsctp_sendv(psock, buffer, length, (struct sockaddr *) &remote_addr, 1,
  867. (void *)&sendv_spa, (socklen_t)sizeof(struct sctp_sendv_spa), SCTP_SENDV_SPA,
  868. 0) < 0) {
  869. perror("usrsctp_sendv");
  870. exit(1);
  871. }
  872. messages++;
  873. }
  874. free (buffer);
  875. if (verbose) {
  876. printf("Closing socket.\n");
  877. }
  878. usrsctp_close(psock);
  879. gettimeofday(&time_now, NULL);
  880. timersub(&time_now, &time_start, &time_diff);
  881. seconds = time_diff.tv_sec + (double)time_diff.tv_usec/1000000;
  882. printf("%s of %lu messages of length %d took %f seconds.\n",
  883. "Sending", messages, length, seconds);
  884. throughput = (double)messages * (double)length / seconds;
  885. printf("Throughput was %f Byte/sec.\n", throughput);
  886. }
  887. while (usrsctp_finish() != 0) {
  888. #ifdef _WIN32
  889. Sleep(1000);
  890. #else
  891. sleep(1);
  892. #endif
  893. }
  894. return 0;
  895. }