ekr_server.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 <sys/types.h>
  37. #include <stdarg.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 PORT 5001
  52. #define MAX_PACKET_SIZE (1<<16)
  53. #define SLEEP 1
  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 buf[MAX_PACKET_SIZE];
  67. char *dump_buf;
  68. ssize_t length;
  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 *s, 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_close(s);
  143. }
  144. return (1);
  145. }
  146. int
  147. main(int argc, char *argv[])
  148. {
  149. struct sockaddr_in sin;
  150. struct sockaddr_conn sconn;
  151. #ifdef _WIN32
  152. SOCKET fd;
  153. #else
  154. int fd, rc;
  155. #endif
  156. struct socket *s;
  157. #ifdef _WIN32
  158. HANDLE tid;
  159. #else
  160. pthread_t tid;
  161. #endif
  162. #ifdef _WIN32
  163. WSADATA wsaData;
  164. #endif
  165. if (argc < 4) {
  166. fprintf(stderr, "error: this program requires 4 arguments!\n");
  167. exit(EXIT_FAILURE);
  168. }
  169. #ifdef _WIN32
  170. if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
  171. fprintf(stderr, "WSAStartup failed\n");
  172. exit (EXIT_FAILURE);
  173. }
  174. #endif
  175. usrsctp_init(0, conn_output, debug_printf_stack);
  176. /* set up a connected UDP socket */
  177. #ifdef _WIN32
  178. if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
  179. fprintf(stderr, "socket() failed with error: %d\n", WSAGetLastError());
  180. exit(EXIT_FAILURE);
  181. }
  182. #else
  183. if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  184. perror("socket");
  185. exit(EXIT_FAILURE);
  186. }
  187. #endif
  188. memset(&sin, 0, sizeof(struct sockaddr_in));
  189. sin.sin_family = AF_INET;
  190. #ifdef HAVE_SIN_LEN
  191. sin.sin_len = sizeof(struct sockaddr_in);
  192. #endif
  193. sin.sin_port = htons(atoi(argv[2]));
  194. if (!inet_pton(AF_INET, argv[1], &sin.sin_addr.s_addr)){
  195. fprintf(stderr, "error: invalid address\n");
  196. exit(EXIT_FAILURE);
  197. }
  198. #ifdef _WIN32
  199. if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
  200. fprintf(stderr, "bind() failed with error: %d\n", WSAGetLastError());
  201. exit(EXIT_FAILURE);
  202. }
  203. #else
  204. if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
  205. perror("bind");
  206. exit(EXIT_FAILURE);
  207. }
  208. #endif
  209. memset(&sin, 0, sizeof(struct sockaddr_in));
  210. sin.sin_family = AF_INET;
  211. #ifdef HAVE_SIN_LEN
  212. sin.sin_len = sizeof(struct sockaddr_in);
  213. #endif
  214. sin.sin_port = htons(atoi(argv[4]));
  215. if (!inet_pton(AF_INET, argv[3], &sin.sin_addr.s_addr)){
  216. printf("error: invalid address\n");
  217. exit(EXIT_FAILURE);
  218. }
  219. #ifdef _WIN32
  220. if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
  221. fprintf(stderr, "connect() failed with error: %d\n", WSAGetLastError());
  222. exit(EXIT_FAILURE);
  223. }
  224. #else
  225. if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
  226. perror("connect");
  227. exit(EXIT_FAILURE);
  228. }
  229. #endif
  230. #ifdef SCTP_DEBUG
  231. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
  232. #endif
  233. usrsctp_sysctl_set_sctp_ecn_enable(0);
  234. usrsctp_register_address((void *)&fd);
  235. #ifdef _WIN32
  236. if ((tid = CreateThread(NULL, 0, &handle_packets, (void *)&fd, 0, NULL)) == NULL) {
  237. fprintf(stderr, "CreateThread() failed with error: %lu\n", GetLastError());
  238. exit(EXIT_FAILURE);
  239. }
  240. #else
  241. if ((rc = pthread_create(&tid, NULL, &handle_packets, (void *)&fd)) != 0) {
  242. fprintf(stderr, "pthread_create: %s\n", strerror(rc));
  243. exit(EXIT_FAILURE);
  244. }
  245. #endif
  246. if ((s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, receive_cb, NULL, 0, NULL)) == NULL) {
  247. perror("usrsctp_socket");
  248. }
  249. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  250. sconn.sconn_family = AF_CONN;
  251. #ifdef HAVE_SCONN_LEN
  252. sconn.sconn_len = sizeof(struct sockaddr_conn);
  253. #endif
  254. sconn.sconn_port = htons(PORT);
  255. sconn.sconn_addr = (void *)&fd;
  256. if (usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
  257. perror("usrsctp_bind");
  258. }
  259. if (usrsctp_listen(s, 1) < 0) {
  260. perror("usrsctp_listen");
  261. }
  262. while (1) {
  263. if (usrsctp_accept(s, NULL, NULL) == NULL) {
  264. perror("usrsctp_accept");
  265. }
  266. }
  267. return (0);
  268. }