discard_server.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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: discard_server [local_encaps_port] [remote_encaps_port]
  32. *
  33. * Example
  34. * Server: $ ./discard_server 11111 22222
  35. * Client: $ ./client 127.0.0.1 9 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 9
  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. }
  107. free(data);
  108. }
  109. return (1);
  110. }
  111. int
  112. main(int argc, char *argv[])
  113. {
  114. struct socket *sock;
  115. struct sockaddr_in6 addr;
  116. struct sctp_udpencaps encaps;
  117. struct sctp_event event;
  118. uint16_t event_types[] = {SCTP_ASSOC_CHANGE,
  119. SCTP_PEER_ADDR_CHANGE,
  120. SCTP_REMOTE_ERROR,
  121. SCTP_SHUTDOWN_EVENT,
  122. SCTP_ADAPTATION_INDICATION,
  123. SCTP_PARTIAL_DELIVERY_EVENT};
  124. unsigned int i;
  125. struct sctp_assoc_value av;
  126. const int on = 1;
  127. ssize_t n;
  128. int flags;
  129. socklen_t from_len;
  130. char buffer[BUFFER_SIZE];
  131. char name[INET6_ADDRSTRLEN];
  132. socklen_t infolen;
  133. struct sctp_rcvinfo rcv_info;
  134. unsigned int infotype;
  135. if (argc > 1) {
  136. usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
  137. } else {
  138. usrsctp_init(9899, NULL, debug_printf_stack);
  139. }
  140. #ifdef SCTP_DEBUG
  141. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
  142. #endif
  143. usrsctp_sysctl_set_sctp_blackhole(2);
  144. usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
  145. if ((sock = usrsctp_socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP, use_cb?receive_cb:NULL, NULL, 0, NULL)) == NULL) {
  146. perror("usrsctp_socket");
  147. }
  148. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_I_WANT_MAPPED_V4_ADDR, (const void*)&on, (socklen_t)sizeof(int)) < 0) {
  149. perror("usrsctp_setsockopt SCTP_I_WANT_MAPPED_V4_ADDR");
  150. }
  151. memset(&av, 0, sizeof(struct sctp_assoc_value));
  152. av.assoc_id = SCTP_ALL_ASSOC;
  153. av.assoc_value = 47;
  154. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_CONTEXT, (const void*)&av, (socklen_t)sizeof(struct sctp_assoc_value)) < 0) {
  155. perror("usrsctp_setsockopt SCTP_CONTEXT");
  156. }
  157. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) < 0) {
  158. perror("usrsctp_setsockopt SCTP_RECVRCVINFO");
  159. }
  160. if (argc > 2) {
  161. memset(&encaps, 0, sizeof(struct sctp_udpencaps));
  162. encaps.sue_address.ss_family = AF_INET6;
  163. encaps.sue_port = htons(atoi(argv[2]));
  164. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
  165. perror("usrsctp_setsockopt SCTP_REMOTE_UDP_ENCAPS_PORT");
  166. }
  167. }
  168. memset(&event, 0, sizeof(event));
  169. event.se_assoc_id = SCTP_FUTURE_ASSOC;
  170. event.se_on = 1;
  171. for (i = 0; i < (unsigned int)(sizeof(event_types)/sizeof(uint16_t)); i++) {
  172. event.se_type = event_types[i];
  173. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event)) < 0) {
  174. perror("usrsctp_setsockopt SCTP_EVENT");
  175. }
  176. }
  177. memset((void *)&addr, 0, sizeof(struct sockaddr_in6));
  178. #ifdef HAVE_SIN6_LEN
  179. addr.sin6_len = sizeof(struct sockaddr_in6);
  180. #endif
  181. addr.sin6_family = AF_INET6;
  182. addr.sin6_port = htons(PORT);
  183. addr.sin6_addr = in6addr_any;
  184. if (usrsctp_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6)) < 0) {
  185. perror("usrsctp_bind");
  186. }
  187. if (usrsctp_listen(sock, 1) < 0) {
  188. perror("usrsctp_listen");
  189. }
  190. while (1) {
  191. if (use_cb) {
  192. #ifdef _WIN32
  193. Sleep(SLEEP * 1000);
  194. #else
  195. sleep(SLEEP);
  196. #endif
  197. } else {
  198. from_len = (socklen_t)sizeof(struct sockaddr_in6);
  199. flags = 0;
  200. infolen = (socklen_t)sizeof(struct sctp_rcvinfo);
  201. n = usrsctp_recvv(sock, (void*)buffer, BUFFER_SIZE, (struct sockaddr *) &addr, &from_len, (void *)&rcv_info,
  202. &infolen, &infotype, &flags);
  203. if (n > 0) {
  204. if (flags & MSG_NOTIFICATION) {
  205. printf("Notification of length %llu received.\n", (unsigned long long)n);
  206. } else {
  207. if (infotype == SCTP_RECVV_RCVINFO) {
  208. 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",
  209. (unsigned long long)n,
  210. inet_ntop(AF_INET6, &addr.sin6_addr, name, INET6_ADDRSTRLEN), ntohs(addr.sin6_port),
  211. rcv_info.rcv_sid,
  212. rcv_info.rcv_ssn,
  213. rcv_info.rcv_tsn,
  214. (uint32_t)ntohl(rcv_info.rcv_ppid),
  215. rcv_info.rcv_context,
  216. (flags & MSG_EOR) ? 1 : 0);
  217. } else {
  218. printf("Msg of length %llu received from %s:%u, complete %d.\n",
  219. (unsigned long long)n,
  220. inet_ntop(AF_INET6, &addr.sin6_addr, name, INET6_ADDRSTRLEN), ntohs(addr.sin6_port),
  221. (flags & MSG_EOR) ? 1 : 0);
  222. }
  223. }
  224. } else {
  225. break;
  226. }
  227. }
  228. }
  229. usrsctp_close(sock);
  230. while (usrsctp_finish() != 0) {
  231. #ifdef _WIN32
  232. Sleep(SLEEP * 1000);
  233. #else
  234. sleep(SLEEP);
  235. #endif
  236. }
  237. return (0);
  238. }