st_client.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright (C) 2011-2013 Michael Tuexen
  3. * Copyright (C) 2011-2015 Colin Caughie
  4. * Copyright (C) 2011-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. /*
  33. * Usage: st_client local_addr local_port remote_addr remote_port remote_sctp_port
  34. */
  35. #ifdef _WIN32
  36. #define _CRT_SECURE_NO_WARNINGS
  37. #endif
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <stdarg.h>
  42. #include <sys/types.h>
  43. #ifndef _WIN32
  44. #include <sys/socket.h>
  45. #include <sys/time.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <errno.h>
  49. #include <pthread.h>
  50. #include <unistd.h>
  51. #else
  52. #include <winsock2.h>
  53. #include <ws2tcpip.h>
  54. #endif
  55. #include <usrsctp.h>
  56. #include "programs_helper.h"
  57. #define MAX_PACKET_SIZE (1<<16)
  58. #define BUFFER_SIZE 80
  59. #define DISCARD_PPID 39
  60. #define HTTP_PPID 63
  61. #define TIMER_INTERVAL_MSECS 10
  62. static int connecting = 0;
  63. static int finish = 0;
  64. static uint64_t
  65. get_milliseconds_count(void)
  66. {
  67. #ifdef _WIN32
  68. // obtain number of milliseconds since system started
  69. return GetTickCount64();
  70. #else
  71. struct timeval tv;
  72. gettimeofday(&tv, NULL); /* get current time */
  73. return (tv.tv_sec * 1000LL + tv.tv_usec / 1000);
  74. #endif
  75. }
  76. static void
  77. #ifdef _WIN32
  78. handle_events(SOCKET sock, struct socket* s, void* sconn_addr)
  79. #else
  80. handle_events(int sock, struct socket* s, void* sconn_addr)
  81. #endif
  82. {
  83. char *dump_buf;
  84. ssize_t length;
  85. char buf[MAX_PACKET_SIZE];
  86. fd_set rfds;
  87. struct timeval tv;
  88. uint64_t next_fire_time = get_milliseconds_count();
  89. uint64_t last_fire_time = next_fire_time;
  90. uint64_t now = get_milliseconds_count();
  91. uint32_t wait_time;
  92. while (!finish) {
  93. if (now > next_fire_time) {
  94. usrsctp_handle_timers((uint32_t)(now - last_fire_time));
  95. last_fire_time = now;
  96. next_fire_time = now + TIMER_INTERVAL_MSECS;
  97. }
  98. wait_time = (uint32_t)(next_fire_time - now);
  99. tv.tv_sec = wait_time / 1000;
  100. tv.tv_usec = (wait_time % 1000) * 1000;
  101. FD_ZERO(&rfds);
  102. FD_SET(sock, &rfds);
  103. #ifdef _WIN32
  104. select(0 /* ignored */, &rfds, NULL, NULL, &tv);
  105. #else
  106. select(sock + 1, &rfds, NULL, NULL, &tv);
  107. #endif
  108. if (FD_ISSET(sock, &rfds)) {
  109. length = recv(sock, buf, MAX_PACKET_SIZE, 0);
  110. if (length > 0) {
  111. if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) {
  112. fprintf(stderr, "%s", dump_buf);
  113. usrsctp_freedumpbuffer(dump_buf);
  114. }
  115. usrsctp_conninput(sconn_addr, buf, (size_t)length, 0);
  116. }
  117. }
  118. }
  119. }
  120. static void
  121. on_connect(struct socket* s)
  122. {
  123. struct sctp_sndinfo sndinfo;
  124. char buffer[BUFFER_SIZE];
  125. int bufferlen;
  126. /* memset(buffer, 'A', BUFFER_SIZE); */
  127. /* bufferlen = BUFFER_SIZE; */
  128. bufferlen = snprintf(buffer, BUFFER_SIZE, "GET / HTTP/1.0\r\nUser-agent: libusrsctp\r\nConnection: close\r\n\r\n");
  129. if (bufferlen < 0) {
  130. return;
  131. }
  132. sndinfo.snd_sid = 0;
  133. sndinfo.snd_flags = 0;
  134. sndinfo.snd_ppid = htonl(DISCARD_PPID);
  135. sndinfo.snd_context = 0;
  136. sndinfo.snd_assoc_id = 0;
  137. if (usrsctp_sendv(s, buffer, bufferlen, NULL, 0, (void *)&sndinfo,
  138. (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
  139. perror("usrsctp_sendv");
  140. }
  141. }
  142. static void
  143. on_socket_readable(struct socket* s) {
  144. char buffer[BUFFER_SIZE];
  145. union sctp_sockstore addr;
  146. socklen_t fromlen = sizeof(addr);
  147. struct sctp_rcvinfo rcv_info;
  148. socklen_t infolen = sizeof(rcv_info);
  149. unsigned int infotype = 0;
  150. int flags = 0;
  151. ssize_t retval;
  152. /* Keep reading until there is no more data */
  153. for (;;) {
  154. retval = usrsctp_recvv(s, buffer, sizeof(buffer), (struct sockaddr*) &addr,
  155. &fromlen, &rcv_info, &infolen, &infotype, &flags);
  156. if (retval < 0) {
  157. if (errno != EWOULDBLOCK) {
  158. perror("usrsctp_recvv");
  159. finish = 1;
  160. }
  161. return;
  162. } else if (retval == 0) {
  163. printf("socket was disconnected\n");
  164. finish = 1;
  165. return;
  166. }
  167. if (flags & MSG_NOTIFICATION) {
  168. printf("Notification of length %d received.\n", (int)retval);
  169. } else {
  170. printf("Msg of length %d received via %p:%u on stream %d with SSN %u and TSN %u, PPID %u, context %u.\n",
  171. (int)retval,
  172. addr.sconn.sconn_addr,
  173. ntohs(addr.sconn.sconn_port),
  174. rcv_info.rcv_sid,
  175. rcv_info.rcv_ssn,
  176. rcv_info.rcv_tsn,
  177. (uint32_t)ntohl(rcv_info.rcv_ppid),
  178. rcv_info.rcv_context);
  179. }
  180. }
  181. }
  182. static void
  183. handle_upcall(struct socket *s, void *arg, int flags)
  184. {
  185. int events = usrsctp_get_events(s);
  186. if (connecting) {
  187. if (events & SCTP_EVENT_ERROR) {
  188. connecting = 0;
  189. finish = 1;
  190. } else if (events & SCTP_EVENT_WRITE) {
  191. connecting = 0;
  192. on_connect(s);
  193. }
  194. return;
  195. }
  196. if (events & SCTP_EVENT_READ) {
  197. on_socket_readable(s);
  198. }
  199. }
  200. static int
  201. conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df)
  202. {
  203. char *dump_buf;
  204. #ifdef _WIN32
  205. SOCKET *fdp;
  206. #else
  207. int *fdp;
  208. #endif
  209. #ifdef _WIN32
  210. fdp = (SOCKET *)addr;
  211. #else
  212. fdp = (int *)addr;
  213. #endif
  214. if ((dump_buf = usrsctp_dumppacket(buf, length, SCTP_DUMP_OUTBOUND)) != NULL) {
  215. fprintf(stderr, "%s", dump_buf);
  216. usrsctp_freedumpbuffer(dump_buf);
  217. }
  218. #ifdef _WIN32
  219. if (send(*fdp, buf, (int)length, 0) == SOCKET_ERROR) {
  220. return (WSAGetLastError());
  221. #else
  222. if (send(*fdp, buf, length, 0) < 0) {
  223. return (errno);
  224. #endif
  225. } else {
  226. return (0);
  227. }
  228. }
  229. /* Usage: st_client local_addr local_port remote_addr remote_port remote_sctp_port */
  230. int
  231. main(int argc, char *argv[])
  232. {
  233. struct sockaddr_in sin;
  234. struct sockaddr_conn sconn;
  235. #ifdef _WIN32
  236. SOCKET fd;
  237. #else
  238. int fd;
  239. #endif
  240. struct socket *s;
  241. int retval;
  242. #ifdef _WIN32
  243. WSADATA wsaData;
  244. #endif
  245. const int on = 1;
  246. if (argc < 6) {
  247. printf("Usage: st_client local_addr local_port remote_addr remote_port remote_sctp_port\n");
  248. return (-1);
  249. }
  250. #ifdef _WIN32
  251. if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
  252. printf("WSAStartup failed\n");
  253. exit(EXIT_FAILURE);
  254. }
  255. #endif
  256. usrsctp_init_nothreads(0, conn_output, debug_printf_stack);
  257. /* set up a connected UDP socket */
  258. #ifdef _WIN32
  259. if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
  260. printf("socket() failed with error: %d\n", WSAGetLastError());
  261. exit(EXIT_FAILURE);
  262. }
  263. #else
  264. if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  265. perror("socket");
  266. exit(EXIT_FAILURE);
  267. }
  268. #endif
  269. memset(&sin, 0, sizeof(struct sockaddr_in));
  270. sin.sin_family = AF_INET;
  271. #ifdef HAVE_SIN_LEN
  272. sin.sin_len = sizeof(struct sockaddr_in);
  273. #endif
  274. sin.sin_port = htons(atoi(argv[2]));
  275. if (!inet_pton(AF_INET, argv[1], &sin.sin_addr.s_addr)){
  276. printf("error: invalid address\n");
  277. exit(EXIT_FAILURE);
  278. }
  279. #ifdef _WIN32
  280. if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
  281. printf("bind() failed with error: %d\n", WSAGetLastError());
  282. exit(EXIT_FAILURE);
  283. }
  284. #else
  285. if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
  286. perror("bind");
  287. exit(EXIT_FAILURE);
  288. }
  289. #endif
  290. memset(&sin, 0, sizeof(struct sockaddr_in));
  291. sin.sin_family = AF_INET;
  292. #ifdef HAVE_SIN_LEN
  293. sin.sin_len = sizeof(struct sockaddr_in);
  294. #endif
  295. sin.sin_port = htons(atoi(argv[4]));
  296. if (!inet_pton(AF_INET, argv[3], &sin.sin_addr.s_addr)){
  297. printf("error: invalid address\n");
  298. exit(EXIT_FAILURE);
  299. }
  300. #ifdef _WIN32
  301. if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
  302. printf("connect() failed with error: %d\n", WSAGetLastError());
  303. exit(EXIT_FAILURE);
  304. }
  305. #else
  306. if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
  307. perror("connect");
  308. exit(EXIT_FAILURE);
  309. }
  310. #endif
  311. #ifdef SCTP_DEBUG
  312. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
  313. #endif
  314. usrsctp_sysctl_set_sctp_ecn_enable(0);
  315. usrsctp_register_address((void *)&fd);
  316. if ((s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
  317. perror("usrsctp_socket");
  318. exit(EXIT_FAILURE);
  319. }
  320. usrsctp_setsockopt(s, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int));
  321. usrsctp_set_non_blocking(s, 1);
  322. usrsctp_set_upcall(s, handle_upcall, NULL);
  323. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  324. sconn.sconn_family = AF_CONN;
  325. #ifdef HAVE_SCONN_LEN
  326. sconn.sconn_len = sizeof(struct sockaddr_conn);
  327. #endif
  328. sconn.sconn_port = htons(0);
  329. sconn.sconn_addr = NULL;
  330. if (usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
  331. perror("usrsctp_bind");
  332. exit(EXIT_FAILURE);
  333. }
  334. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  335. sconn.sconn_family = AF_CONN;
  336. #ifdef HAVE_SCONN_LEN
  337. sconn.sconn_len = sizeof(struct sockaddr_conn);
  338. #endif
  339. sconn.sconn_port = htons(atoi(argv[5]));
  340. sconn.sconn_addr = &fd;
  341. retval = usrsctp_connect(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn));
  342. if (retval < 0 && errno != EWOULDBLOCK && errno != EINPROGRESS) {
  343. perror("usrsctp_connect");
  344. exit(EXIT_FAILURE);
  345. }
  346. connecting = 1;
  347. handle_events(fd, s, sconn.sconn_addr);
  348. return (0);
  349. }