2
0

ustream-example-server.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * ustream-ssl - library for SSL over ustream
  3. *
  4. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <stdio.h>
  21. #include <getopt.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <signal.h>
  26. #include <libubox/ustream.h>
  27. #include <libubox/uloop.h>
  28. #include <libubox/usock.h>
  29. #include "ustream-ssl.h"
  30. static struct ustream_ssl_ctx *ctx;
  31. static struct uloop_fd server;
  32. static const char *port = "10000";
  33. static struct client *next_client = NULL;
  34. struct client {
  35. struct sockaddr_in sin;
  36. struct ustream_fd s;
  37. struct ustream_ssl ssl;
  38. int ctr;
  39. int state;
  40. };
  41. enum {
  42. STATE_INITIAL,
  43. STATE_HEADERS,
  44. STATE_DONE,
  45. };
  46. static void client_read_cb(struct ustream *s, int bytes)
  47. {
  48. struct client *cl = container_of(s, struct client, ssl.stream);
  49. struct ustream_buf *buf = s->r.head;
  50. char *newline, *str;
  51. do {
  52. str = ustream_get_read_buf(s, NULL);
  53. if (!str)
  54. break;
  55. newline = strchr(buf->data, '\n');
  56. if (!newline)
  57. break;
  58. *newline = 0;
  59. switch (cl->state) {
  60. case STATE_INITIAL:
  61. ustream_printf(s, "HTTP/1.1 200 OK\nContent-Type:text/plain\n\n");
  62. ustream_printf(s, "Got request header: %s\n", str);
  63. cl->state++;
  64. break;
  65. case STATE_HEADERS:
  66. switch(str[0]) {
  67. case '\r':
  68. case '\n':
  69. s->eof = true;
  70. ustream_state_change(s);
  71. cl->state++;
  72. break;
  73. default:
  74. ustream_printf(s, "%s\n", str);
  75. break;
  76. }
  77. break;
  78. default:
  79. break;
  80. }
  81. ustream_consume(s, newline + 1 - str);
  82. cl->ctr += newline + 1 - str;
  83. } while(1);
  84. if (s->w.data_bytes > 256 && !ustream_read_blocked(s)) {
  85. fprintf(stderr, "Block read, bytes: %d\n", s->w.data_bytes);
  86. ustream_set_read_blocked(s, true);
  87. }
  88. }
  89. static void client_close(struct client *cl)
  90. {
  91. fprintf(stderr, "Connection closed\n");
  92. ustream_free(&cl->ssl.stream);
  93. ustream_free(&cl->s.stream);
  94. close(cl->s.fd.fd);
  95. free(cl);
  96. }
  97. static void client_notify_write(struct ustream *s, int bytes)
  98. {
  99. fprintf(stderr, "Wrote %d bytes, pending: %d\n", bytes, s->w.data_bytes);
  100. if (s->w.data_bytes < 128 && ustream_read_blocked(s)) {
  101. fprintf(stderr, "Unblock read\n");
  102. ustream_set_read_blocked(s, false);
  103. }
  104. }
  105. static void client_notify_state(struct ustream *s)
  106. {
  107. struct client *cl = container_of(s, struct client, ssl.stream);
  108. if (!s->eof)
  109. return;
  110. fprintf(stderr, "eof!, pending: %d, total: %d\n", s->w.data_bytes, cl->ctr);
  111. if (!s->w.data_bytes)
  112. return client_close(cl);
  113. }
  114. static void client_notify_connected(struct ustream_ssl *ssl)
  115. {
  116. fprintf(stderr, "SSL connection established\n");
  117. }
  118. static void client_notify_error(struct ustream_ssl *ssl, int error, const char *str)
  119. {
  120. struct client *cl = container_of(ssl, struct client, ssl);
  121. fprintf(stderr, "SSL connection error(%d): %s\n", error, str);
  122. client_close(cl);
  123. }
  124. static void server_cb(struct uloop_fd *fd, unsigned int events)
  125. {
  126. struct client *cl;
  127. unsigned int sl = sizeof(struct sockaddr_in);
  128. int sfd;
  129. if (!next_client)
  130. next_client = calloc(1, sizeof(*next_client));
  131. cl = next_client;
  132. sfd = accept(server.fd, (struct sockaddr *) &cl->sin, &sl);
  133. if (sfd < 0) {
  134. fprintf(stderr, "Accept failed\n");
  135. return;
  136. }
  137. cl->ssl.stream.string_data = true;
  138. cl->ssl.stream.notify_read = client_read_cb;
  139. cl->ssl.stream.notify_state = client_notify_state;
  140. cl->ssl.stream.notify_write = client_notify_write;
  141. cl->ssl.notify_connected = client_notify_connected;
  142. cl->ssl.notify_error = client_notify_error;
  143. ustream_fd_init(&cl->s, sfd);
  144. ustream_ssl_init(&cl->ssl, &cl->s.stream, ctx, true);
  145. next_client = NULL;
  146. fprintf(stderr, "New connection\n");
  147. }
  148. static int run_server(void)
  149. {
  150. server.cb = server_cb;
  151. server.fd = usock(USOCK_TCP | USOCK_SERVER | USOCK_IPV4ONLY | USOCK_NUMERIC, "127.0.0.1", port);
  152. if (server.fd < 0) {
  153. perror("usock");
  154. return 1;
  155. }
  156. uloop_init();
  157. uloop_fd_add(&server, ULOOP_READ);
  158. uloop_run();
  159. return 0;
  160. }
  161. static int usage(const char *name)
  162. {
  163. fprintf(stderr, "Usage: %s -p <port>\n", name);
  164. return 1;
  165. }
  166. int main(int argc, char **argv)
  167. {
  168. int ch;
  169. signal(SIGPIPE, SIG_IGN);
  170. ctx = ustream_ssl_context_new(true);
  171. ustream_ssl_context_set_crt_file(ctx, "example.crt");
  172. ustream_ssl_context_set_key_file(ctx, "example.key");
  173. while ((ch = getopt(argc, argv, "p:")) != -1) {
  174. switch(ch) {
  175. case 'p':
  176. port = optarg;
  177. break;
  178. default:
  179. return usage(argv[0]);
  180. }
  181. }
  182. return run_server();
  183. }