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