1 #include <sys/socket.h>
2 #include <netinet/in.h>
10 #include <libubox/ustream.h>
11 #include <libubox/uloop.h>
12 #include <libubox/usock.h>
13 #include "ustream-ssl.h"
17 static struct uloop_fd server;
18 static const char *port = "10000";
19 static struct client *next_client = NULL;
22 struct sockaddr_in sin;
25 struct ustream_ssl ssl;
37 static void client_read_cb(struct ustream *s, int bytes)
39 struct client *cl = container_of(s, struct client, ssl.stream);
40 struct ustream_buf *buf = s->r.head;
44 str = ustream_get_read_buf(s, NULL);
48 newline = strchr(buf->data, '\n');
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);
64 ustream_state_change(s);
68 ustream_printf(s, "%s\n", str);
75 ustream_consume(s, newline + 1 - str);
76 cl->ctr += newline + 1 - str;
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);
85 static void client_close(struct ustream *s)
87 struct client *cl = container_of(s, struct client, ssl.stream);
89 fprintf(stderr, "Connection closed\n");
91 ustream_free(&cl->s.stream);
96 static void client_notify_write(struct ustream *s, int bytes)
98 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);
106 static void client_notify_state(struct ustream *s)
108 struct client *cl = container_of(s, struct client, ssl.stream);
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);
118 static void client_notify_connected(struct ustream_ssl *ssl)
120 fprintf(stderr, "SSL connection established\n");
123 static void client_notify_error(struct ustream_ssl *ssl, int error, const char *str)
125 fprintf(stderr, "SSL connection error(%d): %s\n", error, str);
128 static void server_cb(struct uloop_fd *fd, unsigned int events)
131 unsigned int sl = sizeof(struct sockaddr_in);
135 next_client = calloc(1, sizeof(*next_client));
138 sfd = accept(server.fd, (struct sockaddr *) &cl->sin, &sl);
140 fprintf(stderr, "Accept failed\n");
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;
151 ustream_fd_init(&cl->s, sfd);
152 ustream_ssl_init(&cl->ssl, &cl->s.stream, ctx, true);
154 fprintf(stderr, "New connection\n");
157 static int run_server(void)
160 server.cb = server_cb;
161 server.fd = usock(USOCK_TCP | USOCK_SERVER | USOCK_IPV4ONLY | USOCK_NUMERIC, "127.0.0.1", port);
168 uloop_fd_add(&server, ULOOP_READ);
174 static int usage(const char *name)
176 fprintf(stderr, "Usage: %s -p <port>\n", name);
180 int main(int argc, char **argv)
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");
188 while ((ch = getopt(argc, argv, "p:")) != -1) {
194 return usage(argv[0]);