1 #include <sys/socket.h>
2 #include <netinet/in.h>
9 #include <libubox/ustream.h>
10 #include <libubox/uloop.h>
11 #include <libubox/usock.h>
12 #include "ustream-ssl.h"
16 static struct uloop_fd server
;
17 static const char *port
= "10000";
18 static struct client
*next_client
= NULL
;
21 struct sockaddr_in sin
;
24 struct ustream_ssl ssl
;
36 static void client_read_cb(struct ustream
*s
, int bytes
)
38 struct client
*cl
= container_of(s
, struct client
, ssl
.stream
);
39 struct ustream_buf
*buf
= s
->r
.head
;
43 str
= ustream_get_read_buf(s
, NULL
);
47 newline
= strchr(buf
->data
, '\n');
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
);
63 ustream_state_change(s
);
67 ustream_printf(s
, "%s\n", str
);
74 ustream_consume(s
, newline
+ 1 - str
);
75 cl
->ctr
+= newline
+ 1 - str
;
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);
84 static void client_close(struct ustream
*s
)
86 struct client
*cl
= container_of(s
, struct client
, ssl
.stream
);
88 fprintf(stderr
, "Connection closed\n");
90 ustream_free(&cl
->s
.stream
);
95 static void client_notify_write(struct ustream
*s
, int bytes
)
97 fprintf(stderr
, "Wrote %d bytes, pending: %d\n", bytes
, s
->w
.data_bytes
);
99 if (s
->w
.data_bytes
< 128 && ustream_read_blocked(s
)) {
100 fprintf(stderr
, "Unblock read\n");
101 ustream_set_read_blocked(s
, false);
105 static void client_notify_state(struct ustream
*s
)
107 struct client
*cl
= container_of(s
, struct client
, ssl
.stream
);
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
);
117 static void client_notify_connected(struct ustream_ssl
*ssl
)
119 fprintf(stderr
, "SSL connection established\n");
122 static void client_notify_error(struct ustream_ssl
*ssl
, int error
, const char *str
)
124 fprintf(stderr
, "SSL connection error(%d): %s\n", error
, str
);
127 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
130 unsigned int sl
= sizeof(struct sockaddr_in
);
134 next_client
= calloc(1, sizeof(*next_client
));
137 sfd
= accept(server
.fd
, (struct sockaddr
*) &cl
->sin
, &sl
);
139 fprintf(stderr
, "Accept failed\n");
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
;
150 ustream_fd_init(&cl
->s
, sfd
);
151 ustream_ssl_init(&cl
->ssl
, &cl
->s
.stream
, ctx
, true);
153 fprintf(stderr
, "New connection\n");
156 static int run_server(void)
159 server
.cb
= server_cb
;
160 server
.fd
= usock(USOCK_TCP
| USOCK_SERVER
| USOCK_IPV4ONLY
| USOCK_NUMERIC
, "127.0.0.1", port
);
167 uloop_fd_add(&server
, ULOOP_READ
);
173 static int usage(const char *name
)
175 fprintf(stderr
, "Usage: %s -p <port>\n", name
);
179 int main(int argc
, char **argv
)
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");
187 while ((ch
= getopt(argc
, argv
, "p:")) != -1) {
193 return usage(argv
[0]);