daytime_server_upcall.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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: daytime_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. #include <time.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 DAYTIME_PPID 40
  51. #define PORT 13
  52. static void
  53. handle_accept(struct socket *sock, void *data, int flags)
  54. {
  55. struct socket *conn_sock;
  56. char buffer[80];
  57. time_t now;
  58. socklen_t addr_len = 0;
  59. struct sctp_sndinfo sndinfo;
  60. if (((conn_sock = usrsctp_accept(sock, NULL, &addr_len)) == NULL)
  61. && (errno != EINPROGRESS)) {
  62. perror("usrsctp_accept");
  63. return;
  64. }
  65. time(&now);
  66. #ifdef _WIN32
  67. if (_snprintf(buffer, sizeof(buffer), "%s", ctime(&now)) < 0) {
  68. #else
  69. if (snprintf(buffer, sizeof(buffer), "%s", ctime(&now)) < 0) {
  70. #endif
  71. buffer[0] = '\0';
  72. }
  73. sndinfo.snd_sid = 0;
  74. sndinfo.snd_flags = 0;
  75. sndinfo.snd_ppid = htonl(DAYTIME_PPID);
  76. sndinfo.snd_context = 0;
  77. sndinfo.snd_assoc_id = 0;
  78. usrsctp_sendv(conn_sock, buffer, strlen(buffer), NULL, 0, (void *)&sndinfo,
  79. (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0);
  80. usrsctp_close(conn_sock);
  81. }
  82. int
  83. main(int argc, char *argv[])
  84. {
  85. struct socket *sock;
  86. struct sockaddr_in addr;
  87. struct sctp_udpencaps encaps;
  88. if (argc > 1) {
  89. usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
  90. } else {
  91. usrsctp_init(9899, NULL, debug_printf_stack);
  92. }
  93. #ifdef SCTP_DEBUG
  94. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
  95. #endif
  96. usrsctp_sysctl_set_sctp_blackhole(2);
  97. usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
  98. if ((sock = usrsctp_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
  99. perror("usrsctp_socket");
  100. }
  101. usrsctp_set_non_blocking(sock, 1);
  102. if (argc > 2) {
  103. memset(&encaps, 0, sizeof(struct sctp_udpencaps));
  104. encaps.sue_address.ss_family = AF_INET;
  105. encaps.sue_port = htons(atoi(argv[2]));
  106. if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
  107. perror("setsockopt");
  108. }
  109. }
  110. memset((void *)&addr, 0, sizeof(struct sockaddr_in));
  111. #ifdef HAVE_SIN_LEN
  112. addr.sin_len = sizeof(struct sockaddr_in);
  113. #endif
  114. addr.sin_family = AF_INET;
  115. addr.sin_port = htons(PORT);
  116. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  117. if (usrsctp_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
  118. perror("usrsctp_bind");
  119. }
  120. if (usrsctp_listen(sock, 1) < 0) {
  121. perror("usrsctp_listen");
  122. }
  123. usrsctp_set_upcall(sock, handle_accept, NULL);
  124. while (1) {
  125. #ifdef _WIN32
  126. Sleep(1*1000);
  127. #else
  128. sleep(1);
  129. #endif
  130. }
  131. return (0);
  132. }