ekr_peer.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 <stdarg.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <stdarg.h>
  38. #include <sys/types.h>
  39. #ifndef _WIN32
  40. #include <sys/socket.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include <errno.h>
  44. #include <pthread.h>
  45. #include <unistd.h>
  46. #else
  47. #include <winsock2.h>
  48. #include <ws2tcpip.h>
  49. #endif
  50. #include <usrsctp.h>
  51. #include "programs_helper.h"
  52. #define MAX_PACKET_SIZE (1<<16)
  53. #define LINE_LENGTH 80
  54. #define DISCARD_PPID 39
  55. #ifdef _WIN32
  56. static DWORD WINAPI
  57. #else
  58. static void *
  59. #endif
  60. handle_packets(void *arg)
  61. {
  62. #ifdef _WIN32
  63. SOCKET *fdp;
  64. #else
  65. int *fdp;
  66. #endif
  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. usrsctp_conninput(fdp, buf, (size_t)length, 0);
  81. }
  82. }
  83. #ifdef _WIN32
  84. return 0;
  85. #else
  86. return (NULL);
  87. #endif
  88. }
  89. static int
  90. conn_output(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df)
  91. {
  92. #ifdef _WIN32
  93. SOCKET *fdp;
  94. #else
  95. int *fdp;
  96. #endif
  97. #ifdef _WIN32
  98. fdp = (SOCKET *)addr;
  99. #else
  100. fdp = (int *)addr;
  101. #endif
  102. #ifdef _WIN32
  103. if (send(*fdp, buffer, (int)length, 0) == SOCKET_ERROR) {
  104. return (WSAGetLastError());
  105. #else
  106. if (send(*fdp, buffer, length, 0) < 0) {
  107. return (errno);
  108. #endif
  109. } else {
  110. return (0);
  111. }
  112. }
  113. static int
  114. receive_cb(struct socket *sock, union sctp_sockstore addr, void *data,
  115. size_t datalen, struct sctp_rcvinfo rcv, int flags, void *ulp_info)
  116. {
  117. if (data) {
  118. if (flags & MSG_NOTIFICATION) {
  119. handle_notification((union sctp_notification *)data, datalen);
  120. } else {
  121. printf("Msg of length %d received via %p:%u on stream %u with SSN %u and TSN %u, PPID %u, context %u.\n",
  122. (int)datalen,
  123. addr.sconn.sconn_addr,
  124. ntohs(addr.sconn.sconn_port),
  125. rcv.rcv_sid,
  126. rcv.rcv_ssn,
  127. rcv.rcv_tsn,
  128. (uint32_t)ntohl(rcv.rcv_ppid),
  129. rcv.rcv_context);
  130. }
  131. free(data);
  132. } else {
  133. usrsctp_deregister_address(ulp_info);
  134. usrsctp_close(sock);
  135. }
  136. return (1);
  137. }
  138. int
  139. main(int argc, char *argv[])
  140. {
  141. struct sockaddr_in sin;
  142. struct sockaddr_conn sconn;
  143. struct sctp_event event;
  144. uint16_t event_types[] = {SCTP_ASSOC_CHANGE,
  145. SCTP_PEER_ADDR_CHANGE,
  146. SCTP_SEND_FAILED_EVENT};
  147. unsigned int i;
  148. #ifdef _WIN32
  149. SOCKET fd;
  150. #else
  151. int fd, rc;
  152. #endif
  153. struct socket *s;
  154. #ifdef _WIN32
  155. HANDLE tid;
  156. #else
  157. pthread_t tid;
  158. #endif
  159. struct sctp_sndinfo sndinfo;
  160. char line[LINE_LENGTH];
  161. #ifdef _WIN32
  162. WSADATA wsaData;
  163. #endif
  164. if (argc < 4) {
  165. printf("error: this program requires 4 arguments!\n");
  166. exit(EXIT_FAILURE);
  167. }
  168. #ifdef _WIN32
  169. if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
  170. fprintf(stderr, "WSAStartup failed\n");
  171. exit(EXIT_FAILURE);
  172. }
  173. #endif
  174. usrsctp_init(0, conn_output, debug_printf_stack);
  175. /* set up a connected UDP socket */
  176. #ifdef _WIN32
  177. if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
  178. fprintf(stderr, "socket() failed with error: %d\n", WSAGetLastError());
  179. exit(EXIT_FAILURE);
  180. }
  181. #else
  182. if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  183. perror("socket");
  184. exit(EXIT_FAILURE);
  185. }
  186. #endif
  187. memset(&sin, 0, sizeof(struct sockaddr_in));
  188. sin.sin_family = AF_INET;
  189. #ifdef HAVE_SIN_LEN
  190. sin.sin_len = sizeof(struct sockaddr_in);
  191. #endif
  192. sin.sin_port = htons(atoi(argv[2]));
  193. if (!inet_pton(AF_INET, argv[1], &sin.sin_addr.s_addr)){
  194. printf("error: invalid address\n");
  195. exit(EXIT_FAILURE);
  196. }
  197. #ifdef _WIN32
  198. if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
  199. fprintf(stderr, "bind() failed with error: %d\n", WSAGetLastError());
  200. exit(EXIT_FAILURE);
  201. }
  202. #else
  203. if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
  204. perror("bind");
  205. exit(EXIT_FAILURE);
  206. }
  207. #endif
  208. memset(&sin, 0, sizeof(struct sockaddr_in));
  209. sin.sin_family = AF_INET;
  210. #ifdef HAVE_SIN_LEN
  211. sin.sin_len = sizeof(struct sockaddr_in);
  212. #endif
  213. sin.sin_port = htons(atoi(argv[4]));
  214. if (!inet_pton(AF_INET, argv[3], &sin.sin_addr.s_addr)){
  215. printf("error: invalid address\n");
  216. exit(EXIT_FAILURE);
  217. }
  218. #ifdef _WIN32
  219. if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
  220. fprintf(stderr, "connect() failed with error: %d\n", WSAGetLastError());
  221. exit(EXIT_FAILURE);
  222. }
  223. #else
  224. if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
  225. perror("connect");
  226. exit(EXIT_FAILURE);
  227. }
  228. #endif
  229. #ifdef _WIN32
  230. if ((tid = CreateThread(NULL, 0, &handle_packets, (void *)&fd, 0, NULL)) == NULL) {
  231. fprintf(stderr, "CreateThread() failed with error: %lu\n", GetLastError());
  232. exit(EXIT_FAILURE);
  233. }
  234. #else
  235. if ((rc = pthread_create(&tid, NULL, &handle_packets, (void *)&fd)) != 0) {
  236. fprintf(stderr, "pthread_create: %s\n", strerror(rc));
  237. exit(EXIT_FAILURE);
  238. }
  239. #endif
  240. #ifdef SCTP_DEBUG
  241. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
  242. #endif
  243. usrsctp_register_address((void *)&fd);
  244. usrsctp_sysctl_set_sctp_ecn_enable(0);
  245. if ((s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, receive_cb, NULL, 0, &fd)) == NULL) {
  246. perror("usrsctp_socket");
  247. }
  248. /* Enable the events of interest. */
  249. if (usrsctp_set_non_blocking(s, 1) < 0) {
  250. perror("usrsctp_set_non_blocking");
  251. }
  252. memset(&event, 0, sizeof(event));
  253. event.se_assoc_id = SCTP_ALL_ASSOC;
  254. event.se_on = 1;
  255. for (i = 0; i < sizeof(event_types)/sizeof(uint16_t); i++) {
  256. event.se_type = event_types[i];
  257. if (usrsctp_setsockopt(s, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event)) < 0) {
  258. perror("setsockopt SCTP_EVENT");
  259. }
  260. }
  261. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  262. sconn.sconn_family = AF_CONN;
  263. #ifdef HAVE_SCONN_LEN
  264. sconn.sconn_len = sizeof(struct sockaddr_conn);
  265. #endif
  266. sconn.sconn_port = htons(atoi(argv[5]));
  267. sconn.sconn_addr = &fd;
  268. if (usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
  269. perror("usrsctp_bind");
  270. }
  271. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  272. sconn.sconn_family = AF_CONN;
  273. #ifdef HAVE_SCONN_LEN
  274. sconn.sconn_len = sizeof(struct sockaddr_conn);
  275. #endif
  276. sconn.sconn_port = htons(atoi(argv[6]));
  277. sconn.sconn_addr = &fd;
  278. if (usrsctp_connect(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
  279. perror("usrsctp_connect");
  280. }
  281. for (;;) {
  282. #if defined(_WIN32) && !defined(__MINGW32__)
  283. if (gets_s(line, LINE_LENGTH) == NULL) {
  284. #else
  285. if (fgets(line, LINE_LENGTH, stdin) == NULL) {
  286. #endif
  287. if (usrsctp_shutdown(s, SHUT_WR) < 0) {
  288. perror("usrsctp_shutdown");
  289. }
  290. while (usrsctp_finish() != 0) {
  291. #ifdef _WIN32
  292. Sleep(1000);
  293. #else
  294. sleep(1);
  295. #endif
  296. }
  297. break;
  298. }
  299. sndinfo.snd_sid = 1;
  300. sndinfo.snd_flags = 0;
  301. sndinfo.snd_ppid = htonl(DISCARD_PPID);
  302. sndinfo.snd_context = 0;
  303. sndinfo.snd_assoc_id = 0;
  304. if (usrsctp_sendv(s, line, strlen(line), NULL, 0, (void *)&sndinfo,
  305. (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
  306. perror("usrsctp_sendv");
  307. }
  308. }
  309. while (usrsctp_finish() != 0) {
  310. #ifdef _WIN32
  311. Sleep(1000);
  312. #else
  313. sleep(1);
  314. #endif
  315. }
  316. #ifdef _WIN32
  317. TerminateThread(tid, 0);
  318. WaitForSingleObject(tid, INFINITE);
  319. if (closesocket(fd) == SOCKET_ERROR) {
  320. fprintf(stderr, "closesocket() failed with error: %d\n", WSAGetLastError());
  321. }
  322. WSACleanup();
  323. #else
  324. pthread_cancel(tid);
  325. pthread_join(tid, NULL);
  326. if (close(fd) < 0) {
  327. perror("close");
  328. }
  329. #endif
  330. return (0);
  331. }