tls_test_server.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. #include <sys/types.h>
  25. #include <sys/time.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include "ssl.h"
  31. #include "tls.h"
  32. typedef int (*IO_HANDLER)(int i);
  33. typedef struct __tls_mngr {
  34. int sock;
  35. TLS_CONTEXT ctx;
  36. IO_HANDLER handler;
  37. } TLS_MNGR;
  38. static TLS_MNGR tls_mngr[100] = {{0}};
  39. static int mngr_size = 0;
  40. static int width = 0;
  41. static char *file_prefix = "";
  42. static fd_set regifds;
  43. static int master_socket = 0;
  44. void init_event_mngr (void)
  45. {
  46. mngr_size = 0;
  47. FD_ZERO (&regifds);
  48. }
  49. int tls_setblocking(int s, int blocking)
  50. {
  51. unsigned mode = fcntl(s, F_GETFL, 0);
  52. if (mode < 0)
  53. return -1;
  54. if (blocking)
  55. mode &= ~(O_NDELAY | O_NONBLOCK);
  56. else
  57. mode |= O_NDELAY | O_NONBLOCK;
  58. return fcntl(s, F_SETFL, mode);
  59. }
  60. void regi_sock (int sock, TLS_CONTEXT ctx, IO_HANDLER handler)
  61. {
  62. FD_SET (sock, &regifds);
  63. if (sock + 1 > width)
  64. width = sock + 1;
  65. tls_mngr[mngr_size].sock = sock;
  66. tls_mngr[mngr_size].ctx = ctx;
  67. tls_mngr[mngr_size].handler = handler;
  68. mngr_size++;
  69. tls_setblocking(sock, 0);
  70. printf("socket %d registered, ctx = %p, mngr_size = %d, width = %d\n",
  71. sock, ctx, mngr_size, width);
  72. }
  73. int get_width (void)
  74. {
  75. int i;
  76. int lwidth = 1;
  77. for (i = 0; i < mngr_size; i++) {
  78. if (tls_mngr[i].sock + 1 > lwidth)
  79. lwidth = tls_mngr[i].sock + 1;
  80. }
  81. return lwidth;
  82. }
  83. int tls_read_buffer(TLS_MNGR mngr, char *buf, int size)
  84. {
  85. int bytes = 0;
  86. TLS_CONTEXT ctx = mngr.ctx;
  87. do {
  88. int ret = tls_read(ctx, buf + bytes, size - bytes);
  89. int err = tls_get_error(ctx, ret);
  90. printf("tls_read returned %d\n", ret);
  91. switch (err) {
  92. case SSL_ERROR_NONE:
  93. bytes += ret;
  94. break;
  95. case SSL_ERROR_WANT_READ:
  96. printf("ssl error want read\n");
  97. return bytes;
  98. break;
  99. case SSL_ERROR_WANT_WRITE:
  100. break;
  101. case SSL_ERROR_ZERO_RETURN:
  102. printf("shutdown\n");
  103. tls_shutdown(ctx);
  104. return bytes;
  105. case SSL_ERROR_SYSCALL:
  106. perror("tls_syscall");
  107. tls_shutdown(ctx);
  108. FD_CLR(mngr.sock, &regifds);
  109. return bytes;
  110. default:
  111. printf("TLS error code %d\n", err);
  112. return bytes;
  113. }
  114. if (tls_pending(ctx)) {
  115. printf("read pending\n");
  116. }
  117. } while (tls_pending(ctx));
  118. printf("normal read %d bytes\n", bytes);
  119. return bytes;
  120. }
  121. int tls_slave_handler(int i)
  122. {
  123. unsigned char buf[50000];
  124. char fname[100];
  125. FILE *fp;
  126. int bytes = tls_read_buffer(tls_mngr[i], buf, sizeof(buf));
  127. printf("buffer read %d bytes\n", bytes);
  128. if (bytes > 0) {
  129. sprintf(fname, "%s%02d.txt", file_prefix, i);
  130. fp = fopen(fname, "a");
  131. if (fp == NULL)
  132. perror("tls_slave_handler fopen");
  133. else {
  134. int ret = fwrite(buf, sizeof(char), bytes, fp);
  135. if (ret != bytes)
  136. perror("tls_slave_handler write");
  137. fclose(fp);
  138. }
  139. }
  140. return 0;
  141. }
  142. int tls_master_handler(int i)
  143. {
  144. TLS_CONTEXT ctx_slave;
  145. TLS_ISSUES tls_issues = {0};
  146. int sock;
  147. printf("tls_master_handler\n");
  148. tls_issues.master_socket = master_socket;
  149. sock = init_tls_slave(tls_issues, tls_mngr[i].ctx, &ctx_slave);
  150. if (sock < 0) {
  151. perror("init_tls_slave");
  152. return -1;
  153. }
  154. regi_sock(sock, ctx_slave, tls_slave_handler);
  155. return 0;
  156. }
  157. int event_mngr ()
  158. {
  159. struct timeval tv;
  160. int retv;
  161. int i;
  162. fd_set readfds;
  163. tv.tv_sec = 5;
  164. tv.tv_usec = 0;
  165. readfds = regifds;
  166. retv = select(width, &readfds, NULL, NULL, &tv);
  167. printf("select returned %d\n", retv);
  168. if (retv > 0) {
  169. for (i=0; i < mngr_size; i++) {
  170. if (FD_ISSET(tls_mngr[i].sock, &readfds)) {
  171. tls_mngr[i].handler(i);
  172. }
  173. }
  174. }
  175. if (retv < 0)
  176. perror("select");
  177. return retv;
  178. }
  179. int main (int argc, char *argv[])
  180. {
  181. TLS_ISSUES tls_issues = {0};
  182. TLS_CONTEXT ctx;
  183. fd_set readfds;
  184. if (argc != 2) {
  185. printf("Usage: tls_test_server <file prefix>\n");
  186. exit(0);
  187. }
  188. file_prefix = strdup(argv[1]);
  189. init_event_mngr();
  190. if (init_tls_master(&master_socket,
  191. tls_issues,
  192. &ctx) < 0) {
  193. printf("init_tls_master failed\n");
  194. exit (1);
  195. }
  196. regi_sock(master_socket, ctx, tls_master_handler);
  197. for (;;) {
  198. readfds = regifds;
  199. event_mngr();
  200. usleep(100000L);
  201. }
  202. return 0;
  203. }