echo_server.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /*
  31. * Usage: echo_server [local_encaps_port] [remote_encaps_port]
  32. *
  33. * Example
  34. * Server: $ ./echo_server 11111 22222
  35. * Client: $ ./client 127.0.0.1 7 0 22222 11111
  36. */
  37. #ifdef _WIN32
  38. #define _CRT_SECURE_NO_WARNINGS
  39. #endif
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <stdarg.h>
  44. #include <sys/types.h>
  45. #ifndef _WIN32
  46. #include <unistd.h>
  47. #include <sys/socket.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #endif
  51. #include <usrsctp.h>
  52. #include "programs_helper.h"
  53. #define PORT 7
  54. #define BUFFER_SIZE 10240
  55. #define SLEEP 1
  56. const int use_cb = 0;
  57. static int
  58. receive_cb(struct socket *sock, union sctp_sockstore addr, void *data,
  59. size_t datalen, struct sctp_rcvinfo rcv, int flags, void *ulp_info)
  60. {
  61. char namebuf[INET6_ADDRSTRLEN];
  62. const char *name;
  63. uint16_t port;
  64. if (data) {
  65. if (flags & MSG_NOTIFICATION) {
  66. printf("Notification of length %d received.\n", (int)datalen);
  67. } else {
  68. switch (addr.sa.sa_family) {
  69. #ifdef INET
  70. case AF_INET:
  71. name = inet_ntop(AF_INET, &addr.sin.sin_addr, namebuf, INET_ADDRSTRLEN);
  72. port = ntohs(addr.sin.sin_port);
  73. break;
  74. #endif
  75. #ifdef INET6
  76. case AF_INET6:
  77. name = inet_ntop(AF_INET6, &addr.sin6.sin6_addr, namebuf, INET6_ADDRSTRLEN),
  78. port = ntohs(addr.sin6.sin6_port);
  79. break;
  80. #endif
  81. case AF_CONN:
  82. #ifdef _WIN32
  83. if (_snprintf(namebuf, INET6_ADDRSTRLEN, "%p", addr.sconn.sconn_addr) < 0) {
  84. #else
  85. if (snprintf(namebuf, INET6_ADDRSTRLEN, "%p", addr.sconn.sconn_addr) < 0) {
  86. #endif
  87. namebuf[0] = '\0';
  88. }
  89. name = namebuf;
  90. port = ntohs(addr.sconn.sconn_port);
  91. break;
  92. default:
  93. name = "???";
  94. port = 0;
  95. break;
  96. }
  97. printf("Msg of length %d received from %s:%u on stream %u with SSN %u and TSN %u, PPID %u, context %u.\n",
  98. (int)datalen,
  99. name,
  100. port,
  101. rcv.rcv_sid,
  102. rcv.rcv_ssn,
  103. rcv.rcv_tsn,
  104. (uint32_t)ntohl(rcv.rcv_ppid),
  105. rcv.rcv_context);
  106. if (flags & MSG_EOR) {
  107. struct sctp_sndinfo snd_info;
  108. snd_info.snd_sid = rcv.rcv_sid;
  109. snd_info.snd_flags = 0;
  110. if (rcv.rcv_flags & SCTP_UNORDERED) {
  111. snd_info.snd_flags |= SCTP_UNORDERED;
  112. }
  113. snd_info.snd_ppid = rcv.rcv_ppid;
  114. snd_info.snd_context = 0;
  115. snd_info.snd_assoc_id = rcv.rcv_assoc_id;
  116. if (usrsctp_sendv(sock, data, datalen, NULL, 0, &snd_info, sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
  117. perror("sctp_sendv");
  118. }
  119. }
  120. }
  121. free(data);
  122. }
  123. return (1);
  124. }
  125. int
  126. main(int argc, char *argv[])
  127. {
  128. struct socket *sock;
  129. struct sockaddr_in6 addr;
  130. struct sctp_udpencaps encaps;
  131. struct sctp_event event;
  132. uint16_t event_types[] = {SCTP_ASSOC_CHANGE,
  133. SCTP_PEER_ADDR_CHANGE,
  134. SCTP_REMOTE_ERROR,
  135. SCTP_SHUTDOWN_EVENT,
  136. SCTP_ADAPTATION_INDICATION,
  137. SCTP_PARTIAL_DELIVERY_EVENT};
  138. unsigned int i;
  139. struct sctp_assoc_value av;
  140. const int on = 1;
  141. ssize_t n;
  142. int flags;
  143. socklen_t from_len;
  144. char buffer[BUFFER_SIZE];
  145. char name[INET6_ADDRSTRLEN];
  146. socklen_t infolen;
  147. struct sctp_rcvinfo rcv_info;
  148. unsigned int infotype;
  149. if (argc > 1) {
  150. usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
  151. } else {
  152. usrsctp_init(9899, NULL, debug_printf_stack);
  153. }
  154. #ifdef SCTP_DEBUG
  155. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
  156. #endif
  157. usrsctp_sysctl_set_sctp_blackhole(2);
  158. usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
  159. if ((sock = usrsctp_socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP, use_cb?receive_cb:NULL, NULL, 0, NULL)) == NULL) {
  160. perror("usrsctp_socket");
  161. }
  162. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_I_WANT_MAPPED_V4_ADDR, (const void*)&on, (socklen_t)sizeof(int)) < 0) {
  163. perror("usrsctp_setsockopt SCTP_I_WANT_MAPPED_V4_ADDR");
  164. }
  165. memset(&av, 0, sizeof(struct sctp_assoc_value));
  166. av.assoc_id = SCTP_ALL_ASSOC;
  167. av.assoc_value = 47;
  168. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_CONTEXT, (const void*)&av, (socklen_t)sizeof(struct sctp_assoc_value)) < 0) {
  169. perror("usrsctp_setsockopt SCTP_CONTEXT");
  170. }
  171. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) < 0) {
  172. perror("usrsctp_setsockopt SCTP_RECVRCVINFO");
  173. }
  174. if (argc > 2) {
  175. memset(&encaps, 0, sizeof(struct sctp_udpencaps));
  176. encaps.sue_address.ss_family = AF_INET6;
  177. encaps.sue_port = htons(atoi(argv[2]));
  178. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
  179. perror("usrsctp_setsockopt SCTP_REMOTE_UDP_ENCAPS_PORT");
  180. }
  181. }
  182. memset(&event, 0, sizeof(event));
  183. event.se_assoc_id = SCTP_FUTURE_ASSOC;
  184. event.se_on = 1;
  185. for (i = 0; i < (unsigned int)(sizeof(event_types)/sizeof(uint16_t)); i++) {
  186. event.se_type = event_types[i];
  187. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event)) < 0) {
  188. perror("usrsctp_setsockopt SCTP_EVENT");
  189. }
  190. }
  191. memset((void *)&addr, 0, sizeof(struct sockaddr_in6));
  192. #ifdef HAVE_SIN6_LEN
  193. addr.sin6_len = sizeof(struct sockaddr_in6);
  194. #endif
  195. addr.sin6_family = AF_INET6;
  196. addr.sin6_port = htons(PORT);
  197. addr.sin6_addr = in6addr_any;
  198. if (usrsctp_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6)) < 0) {
  199. perror("usrsctp_bind");
  200. }
  201. if (usrsctp_listen(sock, 1) < 0) {
  202. perror("usrsctp_listen");
  203. }
  204. while (1) {
  205. if (use_cb) {
  206. #ifdef _WIN32
  207. Sleep(SLEEP * 1000);
  208. #else
  209. sleep(SLEEP);
  210. #endif
  211. } else {
  212. from_len = (socklen_t)sizeof(struct sockaddr_in6);
  213. flags = 0;
  214. infolen = (socklen_t)sizeof(struct sctp_rcvinfo);
  215. n = usrsctp_recvv(sock, (void*)buffer, BUFFER_SIZE, (struct sockaddr *) &addr, &from_len, (void *)&rcv_info,
  216. &infolen, &infotype, &flags);
  217. if (n > 0) {
  218. if (flags & MSG_NOTIFICATION) {
  219. printf("Notification of length %llu received.\n", (unsigned long long)n);
  220. } else {
  221. if (infotype == SCTP_RECVV_RCVINFO) {
  222. printf("Msg of length %llu received from %s:%u on stream %u with SSN %u and TSN %u, PPID %u, context %u, complete %d.\n",
  223. (unsigned long long)n,
  224. inet_ntop(AF_INET6, &addr.sin6_addr, name, INET6_ADDRSTRLEN), ntohs(addr.sin6_port),
  225. rcv_info.rcv_sid,
  226. rcv_info.rcv_ssn,
  227. rcv_info.rcv_tsn,
  228. (uint32_t)ntohl(rcv_info.rcv_ppid),
  229. rcv_info.rcv_context,
  230. (flags & MSG_EOR) ? 1 : 0);
  231. if (flags & MSG_EOR) {
  232. struct sctp_sndinfo snd_info;
  233. snd_info.snd_sid = rcv_info.rcv_sid;
  234. snd_info.snd_flags = 0;
  235. if (rcv_info.rcv_flags & SCTP_UNORDERED) {
  236. snd_info.snd_flags |= SCTP_UNORDERED;
  237. }
  238. snd_info.snd_ppid = rcv_info.rcv_ppid;
  239. snd_info.snd_context = 0;
  240. snd_info.snd_assoc_id = rcv_info.rcv_assoc_id;
  241. if (usrsctp_sendv(sock, buffer, (size_t)n, NULL, 0, &snd_info, (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
  242. perror("sctp_sendv");
  243. }
  244. }
  245. } else {
  246. printf("Msg of length %llu received from %s:%u, complete %d.\n",
  247. (unsigned long long)n,
  248. inet_ntop(AF_INET6, &addr.sin6_addr, name, INET6_ADDRSTRLEN), ntohs(addr.sin6_port),
  249. (flags & MSG_EOR) ? 1 : 0);
  250. }
  251. }
  252. } else {
  253. break;
  254. }
  255. }
  256. }
  257. usrsctp_close(sock);
  258. while (usrsctp_finish() != 0) {
  259. #ifdef _WIN32
  260. Sleep(SLEEP * 1000);
  261. #else
  262. sleep(SLEEP);
  263. #endif
  264. }
  265. return (0);
  266. }