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