2 * ustream-ssl - library for SSL over ustream
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
19 #include <sys/socket.h>
20 #include <netinet/in.h>
29 #include <libubox/ustream.h>
30 #include <libubox/uloop.h>
31 #include <libubox/usock.h>
32 #include "ustream-ssl.h"
36 static struct uloop_fd server
;
37 static const char *port
= "10000";
38 static struct client
*next_client
= NULL
;
41 struct sockaddr_in sin
;
44 struct ustream_ssl ssl
;
56 static void client_read_cb(struct ustream
*s
, int bytes
)
58 struct client
*cl
= container_of(s
, struct client
, ssl
.stream
);
59 struct ustream_buf
*buf
= s
->r
.head
;
63 str
= ustream_get_read_buf(s
, NULL
);
67 newline
= strchr(buf
->data
, '\n');
74 ustream_printf(s
, "HTTP/1.1 200 OK\nContent-Type:text/plain\n\n");
75 ustream_printf(s
, "Got request header: %s\n", str
);
83 ustream_state_change(s
);
87 ustream_printf(s
, "%s\n", str
);
94 ustream_consume(s
, newline
+ 1 - str
);
95 cl
->ctr
+= newline
+ 1 - str
;
98 if (s
->w
.data_bytes
> 256 && !ustream_read_blocked(s
)) {
99 fprintf(stderr
, "Block read, bytes: %d\n", s
->w
.data_bytes
);
100 ustream_set_read_blocked(s
, true);
104 static void client_close(struct ustream
*s
)
106 struct client
*cl
= container_of(s
, struct client
, ssl
.stream
);
108 fprintf(stderr
, "Connection closed\n");
110 ustream_free(&cl
->s
.stream
);
115 static void client_notify_write(struct ustream
*s
, int bytes
)
117 fprintf(stderr
, "Wrote %d bytes, pending: %d\n", bytes
, s
->w
.data_bytes
);
119 if (s
->w
.data_bytes
< 128 && ustream_read_blocked(s
)) {
120 fprintf(stderr
, "Unblock read\n");
121 ustream_set_read_blocked(s
, false);
125 static void client_notify_state(struct ustream
*s
)
127 struct client
*cl
= container_of(s
, struct client
, ssl
.stream
);
132 fprintf(stderr
, "eof!, pending: %d, total: %d\n", s
->w
.data_bytes
, cl
->ctr
);
133 if (!s
->w
.data_bytes
)
134 return client_close(s
);
137 static void client_notify_connected(struct ustream_ssl
*ssl
)
139 fprintf(stderr
, "SSL connection established\n");
142 static void client_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
144 fprintf(stderr
, "SSL connection error(%d): %s\n", error
, str
);
147 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
150 unsigned int sl
= sizeof(struct sockaddr_in
);
154 next_client
= calloc(1, sizeof(*next_client
));
157 sfd
= accept(server
.fd
, (struct sockaddr
*) &cl
->sin
, &sl
);
159 fprintf(stderr
, "Accept failed\n");
163 cl
->ssl
.stream
.string_data
= true;
164 cl
->ssl
.stream
.notify_read
= client_read_cb
;
165 cl
->ssl
.stream
.notify_state
= client_notify_state
;
166 cl
->ssl
.stream
.notify_write
= client_notify_write
;
167 cl
->ssl
.notify_connected
= client_notify_connected
;
168 cl
->ssl
.notify_error
= client_notify_error
;
170 ustream_fd_init(&cl
->s
, sfd
);
171 ustream_ssl_init(&cl
->ssl
, &cl
->s
.stream
, ctx
, true);
173 fprintf(stderr
, "New connection\n");
176 static int run_server(void)
179 server
.cb
= server_cb
;
180 server
.fd
= usock(USOCK_TCP
| USOCK_SERVER
| USOCK_IPV4ONLY
| USOCK_NUMERIC
, "127.0.0.1", port
);
187 uloop_fd_add(&server
, ULOOP_READ
);
193 static int usage(const char *name
)
195 fprintf(stderr
, "Usage: %s -p <port>\n", name
);
199 int main(int argc
, char **argv
)
203 signal(SIGPIPE
, SIG_IGN
);
204 ctx
= ustream_ssl_context_new(true);
205 ustream_ssl_context_set_crt_file(ctx
, "example.crt");
206 ustream_ssl_context_set_key_file(ctx
, "example.key");
208 while ((ch
= getopt(argc
, argv
, "p:")) != -1) {
214 return usage(argv
[0]);