use libm with cyassl
[project/ustream-ssl.git] / ustream-example.c
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
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include <libubox/ustream.h>
29 #include <libubox/uloop.h>
30 #include <libubox/usock.h>
31 #include "ustream-ssl.h"
32
33 static void *ctx;
34
35 static struct uloop_fd server;
36 static const char *port = "10000";
37 static struct client *next_client = NULL;
38
39 struct client {
40         struct sockaddr_in sin;
41
42         struct ustream_fd s;
43         struct ustream_ssl ssl;
44         int ctr;
45
46         int state;
47 };
48
49 enum {
50         STATE_INITIAL,
51         STATE_HEADERS,
52         STATE_DONE,
53 };
54
55 static void client_read_cb(struct ustream *s, int bytes)
56 {
57         struct client *cl = container_of(s, struct client, ssl.stream);
58         struct ustream_buf *buf = s->r.head;
59         char *newline, *str;
60
61         do {
62                 str = ustream_get_read_buf(s, NULL);
63                 if (!str)
64                         break;
65
66                 newline = strchr(buf->data, '\n');
67                 if (!newline)
68                         break;
69
70                 *newline = 0;
71                 switch (cl->state) {
72                 case STATE_INITIAL:
73                         ustream_printf(s, "HTTP/1.1 200 OK\nContent-Type:text/plain\n\n");
74                         ustream_printf(s, "Got request header: %s\n", str);
75                         cl->state++;
76                         break;
77                 case STATE_HEADERS:
78                         switch(str[0]) {
79                         case '\r':
80                         case '\n':
81                                 s->eof = true;
82                                 ustream_state_change(s);
83                                 cl->state++;
84                                 break;
85                         default:
86                                 ustream_printf(s, "%s\n", str);
87                                 break;
88                         }
89                         break;
90                 default:
91                         break;
92                 }
93                 ustream_consume(s, newline + 1 - str);
94                 cl->ctr += newline + 1 - str;
95         } while(1);
96
97         if (s->w.data_bytes > 256 && !ustream_read_blocked(s)) {
98                 fprintf(stderr, "Block read, bytes: %d\n", s->w.data_bytes);
99                 ustream_set_read_blocked(s, true);
100         }
101 }
102
103 static void client_close(struct ustream *s)
104 {
105         struct client *cl = container_of(s, struct client, ssl.stream);
106
107         fprintf(stderr, "Connection closed\n");
108         ustream_free(s);
109         ustream_free(&cl->s.stream);
110         close(cl->s.fd.fd);
111         free(cl);
112 }
113
114 static void client_notify_write(struct ustream *s, int bytes)
115 {
116         fprintf(stderr, "Wrote %d bytes, pending: %d\n", bytes, s->w.data_bytes);
117
118         if (s->w.data_bytes < 128 && ustream_read_blocked(s)) {
119                 fprintf(stderr, "Unblock read\n");
120                 ustream_set_read_blocked(s, false);
121         }
122 }
123
124 static void client_notify_state(struct ustream *s)
125 {
126         struct client *cl = container_of(s, struct client, ssl.stream);
127
128         if (!s->eof)
129                 return;
130
131         fprintf(stderr, "eof!, pending: %d, total: %d\n", s->w.data_bytes, cl->ctr);
132         if (!s->w.data_bytes)
133                 return client_close(s);
134 }
135
136 static void client_notify_connected(struct ustream_ssl *ssl)
137 {
138         fprintf(stderr, "SSL connection established\n");
139 }
140
141 static void client_notify_error(struct ustream_ssl *ssl, int error, const char *str)
142 {
143         fprintf(stderr, "SSL connection error(%d): %s\n", error, str);
144 }
145
146 static void server_cb(struct uloop_fd *fd, unsigned int events)
147 {
148         struct client *cl;
149         unsigned int sl = sizeof(struct sockaddr_in);
150         int sfd;
151
152         if (!next_client)
153                 next_client = calloc(1, sizeof(*next_client));
154
155         cl = next_client;
156         sfd = accept(server.fd, (struct sockaddr *) &cl->sin, &sl);
157         if (sfd < 0) {
158                 fprintf(stderr, "Accept failed\n");
159                 return;
160         }
161
162         cl->ssl.stream.string_data = true;
163         cl->ssl.stream.notify_read = client_read_cb;
164         cl->ssl.stream.notify_state = client_notify_state;
165         cl->ssl.stream.notify_write = client_notify_write;
166         cl->ssl.notify_connected = client_notify_connected;
167         cl->ssl.notify_error = client_notify_error;
168
169         ustream_fd_init(&cl->s, sfd);
170         ustream_ssl_init(&cl->ssl, &cl->s.stream, ctx, true);
171         next_client = NULL;
172         fprintf(stderr, "New connection\n");
173 }
174
175 static int run_server(void)
176 {
177
178         server.cb = server_cb;
179         server.fd = usock(USOCK_TCP | USOCK_SERVER | USOCK_IPV4ONLY | USOCK_NUMERIC, "127.0.0.1", port);
180         if (server.fd < 0) {
181                 perror("usock");
182                 return 1;
183         }
184
185         uloop_init();
186         uloop_fd_add(&server, ULOOP_READ);
187         uloop_run();
188
189         return 0;
190 }
191
192 static int usage(const char *name)
193 {
194         fprintf(stderr, "Usage: %s -p <port>\n", name);
195         return 1;
196 }
197
198 int main(int argc, char **argv)
199 {
200         int ch;
201
202         ctx = ustream_ssl_context_new(true);
203         ustream_ssl_context_set_crt_file(ctx, "example.crt");
204         ustream_ssl_context_set_key_file(ctx, "example.key");
205
206         while ((ch = getopt(argc, argv, "p:")) != -1) {
207                 switch(ch) {
208                 case 'p':
209                         port = optarg;
210                         break;
211                 default:
212                         return usage(argv[0]);
213                 }
214         }
215
216         return run_server();
217 }