discard_server_upcall.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_upcall [local_encaps_port] [remote_encaps_port]
  32. */
  33. #ifdef _WIN32
  34. #define _CRT_SECURE_NO_WARNINGS
  35. #endif
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <stdarg.h>
  40. #include <sys/types.h>
  41. #ifndef _WIN32
  42. #include <unistd.h>
  43. #include <sys/socket.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #endif
  47. #include <usrsctp.h>
  48. #include "programs_helper.h"
  49. #define BUFFERSIZE 10240
  50. #define PORT 9
  51. static void
  52. handle_upcall(struct socket *sock, void *data, int flgs)
  53. {
  54. char namebuf[INET6_ADDRSTRLEN];
  55. const char *name;
  56. uint16_t port;
  57. char *buf;
  58. int events;
  59. while ((events = usrsctp_get_events(sock)) && (events & SCTP_EVENT_READ)) {
  60. struct sctp_recvv_rn rn;
  61. ssize_t n;
  62. struct sockaddr_storage addr;
  63. buf = malloc(BUFFERSIZE);
  64. int flags = 0;
  65. socklen_t len = (socklen_t)sizeof(struct sockaddr_storage);
  66. unsigned int infotype = 0;
  67. socklen_t infolen = sizeof(struct sctp_recvv_rn);
  68. memset(&rn, 0, sizeof(struct sctp_recvv_rn));
  69. n = usrsctp_recvv(sock, buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
  70. &infolen, &infotype, &flags);
  71. if (n < 0) {
  72. perror("usrsctp_recvv");
  73. }
  74. if (n > 0) {
  75. if (flags & MSG_NOTIFICATION) {
  76. printf("Notification of length %d received.\n", (int)n);
  77. } else {
  78. /*
  79. #ifdef _WIN32
  80. _write(_fileno(stdout), buf, (unsigned int)n);
  81. #else
  82. if (write(fileno(stdout), buf, n) < 0) {
  83. perror("write");
  84. }
  85. #endif
  86. */
  87. switch (addr.ss_family) {
  88. #ifdef INET
  89. case AF_INET: {
  90. struct sockaddr_in addr4;
  91. memcpy(&addr4, (struct sockaddr_in *)&addr, sizeof(struct sockaddr_in));
  92. name = inet_ntop(AF_INET, &addr4.sin_addr, namebuf, INET_ADDRSTRLEN);
  93. port = ntohs(addr4.sin_port);
  94. break;
  95. }
  96. #endif
  97. #ifdef INET6
  98. case AF_INET6: {
  99. struct sockaddr_in6 addr6;
  100. memcpy(&addr6, (struct sockaddr_in6 *)&addr, sizeof(struct sockaddr_in6));
  101. name = inet_ntop(AF_INET6, &addr6.sin6_addr, namebuf, INET6_ADDRSTRLEN),
  102. port = ntohs(addr6.sin6_port);
  103. break;
  104. }
  105. #endif
  106. default:
  107. name = NULL;
  108. port = 0;
  109. break;
  110. }
  111. if (name == NULL) {
  112. printf("inet_ntop failed\n");
  113. free(buf);
  114. return;
  115. }
  116. printf("Msg of length %d received from %s:%u on stream %d with SSN %u and TSN %u, PPID %u, context %u.\n",
  117. (int)n,
  118. namebuf,
  119. port,
  120. rn.recvv_rcvinfo.rcv_sid,
  121. rn.recvv_rcvinfo.rcv_ssn,
  122. rn.recvv_rcvinfo.rcv_tsn,
  123. (uint32_t)ntohl(rn.recvv_rcvinfo.rcv_ppid),
  124. rn.recvv_rcvinfo.rcv_context);
  125. }
  126. }
  127. free(buf);
  128. }
  129. return;
  130. }
  131. int
  132. main(int argc, char *argv[])
  133. {
  134. struct socket *sock;
  135. struct sockaddr_in6 addr;
  136. struct sctp_udpencaps encaps;
  137. struct sctp_event event;
  138. uint16_t event_types[] = {SCTP_ASSOC_CHANGE,
  139. SCTP_PEER_ADDR_CHANGE,
  140. SCTP_REMOTE_ERROR,
  141. SCTP_SHUTDOWN_EVENT,
  142. SCTP_ADAPTATION_INDICATION,
  143. SCTP_PARTIAL_DELIVERY_EVENT};
  144. unsigned int i;
  145. struct sctp_assoc_value av;
  146. const int on = 1;
  147. if (argc > 1) {
  148. usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
  149. } else {
  150. usrsctp_init(9899, NULL, debug_printf_stack);
  151. }
  152. #ifdef SCTP_DEBUG
  153. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
  154. #endif
  155. usrsctp_sysctl_set_sctp_blackhole(2);
  156. usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
  157. if ((sock = usrsctp_socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
  158. perror("usrsctp_socket");
  159. }
  160. usrsctp_set_non_blocking(sock, 1);
  161. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_I_WANT_MAPPED_V4_ADDR, (const void*)&on, (socklen_t)sizeof(int)) < 0) {
  162. perror("usrsctp_setsockopt SCTP_I_WANT_MAPPED_V4_ADDR");
  163. }
  164. memset(&av, 0, sizeof(struct sctp_assoc_value));
  165. av.assoc_id = SCTP_ALL_ASSOC;
  166. av.assoc_value = 47;
  167. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_CONTEXT, (const void*)&av, (socklen_t)sizeof(struct sctp_assoc_value)) < 0) {
  168. perror("usrsctp_setsockopt SCTP_CONTEXT");
  169. }
  170. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) < 0) {
  171. perror("usrsctp_setsockopt SCTP_RECVRCVINFO");
  172. }
  173. if (argc > 2) {
  174. memset(&encaps, 0, sizeof(struct sctp_udpencaps));
  175. encaps.sue_address.ss_family = AF_INET6;
  176. encaps.sue_port = htons(atoi(argv[2]));
  177. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
  178. perror("usrsctp_setsockopt SCTP_REMOTE_UDP_ENCAPS_PORT");
  179. }
  180. }
  181. memset(&event, 0, sizeof(event));
  182. event.se_assoc_id = SCTP_FUTURE_ASSOC;
  183. event.se_on = 1;
  184. for (i = 0; i < (unsigned int)(sizeof(event_types)/sizeof(uint16_t)); i++) {
  185. event.se_type = event_types[i];
  186. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event)) < 0) {
  187. perror("usrsctp_setsockopt SCTP_EVENT");
  188. }
  189. }
  190. usrsctp_set_upcall(sock, handle_upcall, NULL);
  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. #ifdef _WIN32
  206. Sleep(1*1000);
  207. #else
  208. sleep(1);
  209. #endif
  210. }
  211. return (0);
  212. }