chargen_server_upcall.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2012-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: chargen_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 <sys/socket.h>
  43. #include <unistd.h>
  44. #include <time.h>
  45. #include <errno.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #endif
  49. #include <usrsctp.h>
  50. #include "programs_helper.h"
  51. #define BUFFERSIZE 10240
  52. #define PORT 19
  53. char buffer[95];
  54. int done = 0;
  55. int send_done = 0;
  56. static void
  57. initBuffer(void) {
  58. int i, j;
  59. for (i = 32, j = 0; i < 126; i++, j++) {
  60. buffer[j] = i;
  61. }
  62. }
  63. unsigned int signCounter = 0;
  64. static void
  65. handle_upcall(struct socket *upcall_socket, void *upcall_data, int upcall_flags);
  66. static void
  67. handle_accept(struct socket *upcall_socket, void *upcall_data, int upcall_flags)
  68. {
  69. struct socket *conn_sock;
  70. if (((conn_sock = usrsctp_accept(upcall_socket, NULL, NULL)) == NULL)
  71. && (errno != EINPROGRESS)) {
  72. perror("usrsctp_accept");
  73. return;
  74. }
  75. done = 0;
  76. printf("connection accepted from socket %p\n", (void *)conn_sock);
  77. usrsctp_set_upcall(conn_sock, handle_upcall, NULL);
  78. }
  79. static void
  80. handle_upcall(struct socket *upcall_socket, void *upcall_data, int upcall_flags)
  81. {
  82. int events = usrsctp_get_events(upcall_socket);
  83. if (events & SCTP_EVENT_READ && !send_done) {
  84. char *buf;
  85. struct sctp_recvv_rn rn;
  86. ssize_t n;
  87. struct sockaddr_storage addr;
  88. buf = malloc(BUFFERSIZE);
  89. int recv_flags = 0;
  90. socklen_t len = (socklen_t)sizeof(struct sockaddr_storage);
  91. unsigned int infotype = 0;
  92. socklen_t infolen = sizeof(struct sctp_recvv_rn);
  93. memset(&rn, 0, sizeof(struct sctp_recvv_rn));
  94. n = usrsctp_recvv(upcall_socket, buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
  95. &infolen, &infotype, &recv_flags);
  96. if (n < 0) {
  97. perror("usrsctp_recvv");
  98. done = 1;
  99. usrsctp_close(upcall_socket);
  100. printf("client socket %p closed\n", (void *)upcall_socket);
  101. upcall_socket = NULL;
  102. return;
  103. }
  104. if (n == 0) {
  105. done = 1;
  106. usrsctp_close(upcall_socket);
  107. printf("client socket %p closed\n", (void *)upcall_socket);
  108. upcall_socket = NULL;
  109. return;
  110. }
  111. if (n > 0) {
  112. if (recv_flags & MSG_NOTIFICATION) {
  113. printf("Notification of length %d received.\n", (int)n);
  114. } else {
  115. printf("data of size %d received\n", (int)n);
  116. }
  117. }
  118. free(buf);
  119. }
  120. if ((events & SCTP_EVENT_WRITE) && !done) {
  121. struct sctp_sndinfo snd_info;
  122. snd_info.snd_sid = 0;
  123. snd_info.snd_flags = 0;
  124. snd_info.snd_ppid = 0;
  125. snd_info.snd_context = 0;
  126. snd_info.snd_assoc_id = 0;
  127. if (usrsctp_sendv(upcall_socket, buffer, strlen(buffer), NULL, 0, &snd_info, (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
  128. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  129. send_done = 1;
  130. usrsctp_close(upcall_socket);
  131. printf("client socket %p closed\n", (void *)upcall_socket);
  132. return;
  133. }
  134. }
  135. }
  136. return;
  137. }
  138. int
  139. main(int argc, char *argv[])
  140. {
  141. struct socket *listening_socket;
  142. struct sockaddr_in6 addr;
  143. struct sctp_udpencaps encaps;
  144. struct sctp_assoc_value av;
  145. const int on = 1;
  146. if (argc > 1) {
  147. usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
  148. } else {
  149. usrsctp_init(9899, NULL, debug_printf_stack);
  150. }
  151. #ifdef SCTP_DEBUG
  152. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
  153. #endif
  154. usrsctp_sysctl_set_sctp_blackhole(2);
  155. usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
  156. if ((listening_socket = usrsctp_socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
  157. perror("usrsctp_socket");
  158. }
  159. usrsctp_set_non_blocking(listening_socket, 1);
  160. if (usrsctp_setsockopt(listening_socket, IPPROTO_SCTP, SCTP_I_WANT_MAPPED_V4_ADDR, (const void*)&on, (socklen_t)sizeof(int)) < 0) {
  161. perror("usrsctp_setsockopt SCTP_I_WANT_MAPPED_V4_ADDR");
  162. }
  163. memset(&av, 0, sizeof(struct sctp_assoc_value));
  164. av.assoc_id = SCTP_ALL_ASSOC;
  165. av.assoc_value = 47;
  166. if (usrsctp_setsockopt(listening_socket, IPPROTO_SCTP, SCTP_CONTEXT, (const void*)&av, (socklen_t)sizeof(struct sctp_assoc_value)) < 0) {
  167. perror("usrsctp_setsockopt SCTP_CONTEXT");
  168. }
  169. if (usrsctp_setsockopt(listening_socket, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) < 0) {
  170. perror("usrsctp_setsockopt SCTP_RECVRCVINFO");
  171. }
  172. if (argc > 2) {
  173. memset(&encaps, 0, sizeof(struct sctp_udpencaps));
  174. encaps.sue_address.ss_family = AF_INET6;
  175. encaps.sue_port = htons(atoi(argv[2]));
  176. if (usrsctp_setsockopt(listening_socket, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
  177. perror("usrsctp_setsockopt SCTP_REMOTE_UDP_ENCAPS_PORT");
  178. }
  179. }
  180. initBuffer();
  181. memset((void *)&addr, 0, sizeof(struct sockaddr_in6));
  182. #ifdef HAVE_SIN6_LEN
  183. addr.sin6_len = sizeof(struct sockaddr_in6);
  184. #endif
  185. addr.sin6_family = AF_INET6;
  186. addr.sin6_port = htons(PORT);
  187. addr.sin6_addr = in6addr_any;
  188. if (usrsctp_bind(listening_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6)) < 0) {
  189. perror("usrsctp_bind");
  190. }
  191. if (usrsctp_listen(listening_socket, 1) < 0) {
  192. perror("usrsctp_listen");
  193. }
  194. usrsctp_set_upcall(listening_socket, handle_accept, NULL);
  195. while (1) {
  196. #ifdef _WIN32
  197. Sleep(1*1000);
  198. #else
  199. sleep(1);
  200. #endif
  201. }
  202. return (0);
  203. }