echo_server_upcall.c 7.5 KB

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