ekr_client.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2011-2013 Michael Tuexen
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #ifdef _WIN32
  31. #define _CRT_SECURE_NO_WARNINGS
  32. #endif
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <stdarg.h>
  37. #include <sys/types.h>
  38. #ifndef _WIN32
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <errno.h>
  43. #include <pthread.h>
  44. #include <unistd.h>
  45. #else
  46. #include <winsock2.h>
  47. #include <ws2tcpip.h>
  48. #endif
  49. #include <usrsctp.h>
  50. #include "programs_helper.h"
  51. #define MAX_PACKET_SIZE (1<<16)
  52. #define BUFFER_SIZE 80
  53. #define DISCARD_PPID 39
  54. #ifdef _WIN32
  55. static DWORD WINAPI
  56. #else
  57. static void *
  58. #endif
  59. handle_packets(void *arg)
  60. {
  61. #ifdef _WIN32
  62. SOCKET *fdp;
  63. #else
  64. int *fdp;
  65. #endif
  66. char *dump_buf;
  67. ssize_t length;
  68. char buf[MAX_PACKET_SIZE];
  69. #ifdef _WIN32
  70. fdp = (SOCKET *)arg;
  71. #else
  72. fdp = (int *)arg;
  73. #endif
  74. for (;;) {
  75. #if defined(__NetBSD__)
  76. pthread_testcancel();
  77. #endif
  78. length = recv(*fdp, buf, MAX_PACKET_SIZE, 0);
  79. if (length > 0) {
  80. if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) {
  81. fprintf(stderr, "%s", dump_buf);
  82. usrsctp_freedumpbuffer(dump_buf);
  83. }
  84. usrsctp_conninput(fdp, buf, (size_t)length, 0);
  85. }
  86. }
  87. #ifdef _WIN32
  88. return 0;
  89. #else
  90. return (NULL);
  91. #endif
  92. }
  93. static int
  94. conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df)
  95. {
  96. char *dump_buf;
  97. #ifdef _WIN32
  98. SOCKET *fdp;
  99. #else
  100. int *fdp;
  101. #endif
  102. #ifdef _WIN32
  103. fdp = (SOCKET *)addr;
  104. #else
  105. fdp = (int *)addr;
  106. #endif
  107. if ((dump_buf = usrsctp_dumppacket(buf, length, SCTP_DUMP_OUTBOUND)) != NULL) {
  108. fprintf(stderr, "%s", dump_buf);
  109. usrsctp_freedumpbuffer(dump_buf);
  110. }
  111. #ifdef _WIN32
  112. if (send(*fdp, buf, (int)length, 0) == SOCKET_ERROR) {
  113. return (WSAGetLastError());
  114. #else
  115. if (send(*fdp, buf, length, 0) < 0) {
  116. return (errno);
  117. #endif
  118. } else {
  119. return (0);
  120. }
  121. }
  122. static int
  123. receive_cb(struct socket *sock, union sctp_sockstore addr, void *data,
  124. size_t datalen, struct sctp_rcvinfo rcv, int flags, void *ulp_info)
  125. {
  126. if (data) {
  127. if (flags & MSG_NOTIFICATION) {
  128. printf("Notification of length %d received.\n", (int)datalen);
  129. } else {
  130. printf("Msg of length %d received via %p:%u on stream %u with SSN %u and TSN %u, PPID %u, context %u.\n",
  131. (int)datalen,
  132. addr.sconn.sconn_addr,
  133. ntohs(addr.sconn.sconn_port),
  134. rcv.rcv_sid,
  135. rcv.rcv_ssn,
  136. rcv.rcv_tsn,
  137. (uint32_t)ntohl(rcv.rcv_ppid),
  138. rcv.rcv_context);
  139. }
  140. free(data);
  141. } else {
  142. usrsctp_deregister_address(ulp_info);
  143. usrsctp_close(sock);
  144. }
  145. return (1);
  146. }
  147. int
  148. main(int argc, char *argv[])
  149. {
  150. struct sockaddr_in sin;
  151. struct sockaddr_conn sconn;
  152. #ifdef _WIN32
  153. SOCKET fd;
  154. #else
  155. int fd, rc;
  156. #endif
  157. struct socket *s;
  158. #ifdef _WIN32
  159. HANDLE tid;
  160. #else
  161. pthread_t tid;
  162. #endif
  163. struct sctp_sndinfo sndinfo;
  164. char buffer[BUFFER_SIZE];
  165. #ifdef _WIN32
  166. WSADATA wsaData;
  167. #endif
  168. if (argc < 4) {
  169. fprintf(stderr, "error: this program requires 4 arguments!\n");
  170. exit(EXIT_FAILURE);
  171. }
  172. #ifdef _WIN32
  173. if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
  174. fprintf(stderr, "WSAStartup failed\n");
  175. exit (EXIT_FAILURE);
  176. }
  177. #endif
  178. usrsctp_init(0, conn_output, debug_printf_stack);
  179. /* set up a connected UDP socket */
  180. #ifdef _WIN32
  181. if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
  182. fprintf(stderr, "socket() failed with error: %d\n", WSAGetLastError());
  183. exit(EXIT_FAILURE);
  184. }
  185. #else
  186. if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  187. perror("socket");
  188. exit(EXIT_FAILURE);
  189. }
  190. #endif
  191. memset(&sin, 0, sizeof(struct sockaddr_in));
  192. sin.sin_family = AF_INET;
  193. #ifdef HAVE_SIN_LEN
  194. sin.sin_len = sizeof(struct sockaddr_in);
  195. #endif
  196. sin.sin_port = htons(atoi(argv[2]));
  197. if (!inet_pton(AF_INET, argv[1], &sin.sin_addr.s_addr)){
  198. fprintf(stderr, "error: invalid address\n");
  199. exit(EXIT_FAILURE);
  200. }
  201. #ifdef _WIN32
  202. if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
  203. fprintf(stderr, "bind() failed with error: %d\n", WSAGetLastError());
  204. exit(EXIT_FAILURE);
  205. }
  206. #else
  207. if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
  208. perror("bind");
  209. exit(EXIT_FAILURE);
  210. }
  211. #endif
  212. memset(&sin, 0, sizeof(struct sockaddr_in));
  213. sin.sin_family = AF_INET;
  214. #ifdef HAVE_SIN_LEN
  215. sin.sin_len = sizeof(struct sockaddr_in);
  216. #endif
  217. sin.sin_port = htons(atoi(argv[4]));
  218. if (!inet_pton(AF_INET, argv[3], &sin.sin_addr.s_addr)){
  219. printf("error: invalid address\n");
  220. exit(EXIT_FAILURE);
  221. }
  222. #ifdef _WIN32
  223. if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
  224. fprintf(stderr, "connect() failed with error: %d\n", WSAGetLastError());
  225. exit(EXIT_FAILURE);
  226. }
  227. #else
  228. if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
  229. perror("connect");
  230. exit(EXIT_FAILURE);
  231. }
  232. #endif
  233. #ifdef SCTP_DEBUG
  234. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
  235. #endif
  236. usrsctp_sysctl_set_sctp_ecn_enable(0);
  237. usrsctp_register_address((void *)&fd);
  238. #ifdef _WIN32
  239. if ((tid = CreateThread(NULL, 0, &handle_packets, (void *)&fd, 0, NULL)) == NULL) {
  240. fprintf(stderr, "CreateThread() failed with error: %lu\n", GetLastError());
  241. exit(EXIT_FAILURE);
  242. }
  243. #else
  244. if ((rc = pthread_create(&tid, NULL, &handle_packets, (void *)&fd)) != 0) {
  245. fprintf(stderr, "pthread_create: %s\n", strerror(rc));
  246. exit(EXIT_FAILURE);
  247. }
  248. #endif
  249. if ((s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, receive_cb, NULL, 0, &fd)) == NULL) {
  250. perror("usrsctp_socket");
  251. }
  252. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  253. sconn.sconn_family = AF_CONN;
  254. #ifdef HAVE_SCONN_LEN
  255. sconn.sconn_len = sizeof(struct sockaddr_conn);
  256. #endif
  257. sconn.sconn_port = htons(0);
  258. sconn.sconn_addr = NULL;
  259. if (usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
  260. perror("usrsctp_bind");
  261. }
  262. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  263. sconn.sconn_family = AF_CONN;
  264. #ifdef HAVE_SCONN_LEN
  265. sconn.sconn_len = sizeof(struct sockaddr_conn);
  266. #endif
  267. sconn.sconn_port = htons(5001);
  268. sconn.sconn_addr = &fd;
  269. if (usrsctp_connect(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
  270. perror("usrsctp_connect");
  271. }
  272. memset(buffer, 'A', BUFFER_SIZE);
  273. sndinfo.snd_sid = 1;
  274. sndinfo.snd_flags = 0;
  275. sndinfo.snd_ppid = htonl(DISCARD_PPID);
  276. sndinfo.snd_context = 0;
  277. sndinfo.snd_assoc_id = 0;
  278. if (usrsctp_sendv(s, buffer, BUFFER_SIZE, NULL, 0, (void *)&sndinfo,
  279. (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
  280. perror("usrsctp_sendv");
  281. }
  282. usrsctp_shutdown(s, SHUT_WR);
  283. while (usrsctp_finish() != 0) {
  284. #ifdef _WIN32
  285. Sleep(1000);
  286. #else
  287. sleep(1);
  288. #endif
  289. }
  290. #ifdef _WIN32
  291. TerminateThread(tid, 0);
  292. WaitForSingleObject(tid, INFINITE);
  293. if (closesocket(fd) == SOCKET_ERROR) {
  294. fprintf(stderr, "closesocket() failed with error: %d\n", WSAGetLastError());
  295. }
  296. WSACleanup();
  297. #else
  298. pthread_cancel(tid);
  299. pthread_join(tid, NULL);
  300. if (close(fd) < 0) {
  301. perror("close");
  302. }
  303. #endif
  304. return (0);
  305. }