2 * uhttpd - Tiny single-threaded httpd - Main header
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
25 #define LIB_EXT "dylib"
30 static struct ustream_ssl_ops *ops;
34 int uh_tls_init(const char *key, const char *crt)
36 static bool _init = false;
42 dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
44 fprintf(stderr, "Failed to load ustream-ssl library: %s\n", dlerror());
48 ops = dlsym(dlh, "ustream_ssl_ops");
50 fprintf(stderr, "Could not find required symbol 'ustream_ssl_ops' in ustream-ssl library\n");
54 ctx = ops->context_new(true);
56 fprintf(stderr, "Failed to initialize ustream-ssl\n");
60 if (ops->context_set_crt_file(ctx, crt) ||
61 ops->context_set_key_file(ctx, key)) {
62 fprintf(stderr, "Failed to load certificate/key files\n");
69 static void tls_ustream_read_cb(struct ustream *s, int bytes)
71 struct client *cl = container_of(s, struct client, ssl);
73 uh_client_read_cb(cl);
76 static void tls_ustream_write_cb(struct ustream *s, int bytes)
78 struct client *cl = container_of(s, struct client, ssl);
80 if (cl->dispatch.write_cb)
81 cl->dispatch.write_cb(cl);
84 static void tls_notify_state(struct ustream *s)
86 struct client *cl = container_of(s, struct client, ssl);
88 uh_client_notify_state(cl);
91 void uh_tls_client_attach(struct client *cl)
93 cl->us = &cl->ssl.stream;
94 ops->init(&cl->ssl, &cl->sfd.stream, ctx, true);
95 cl->us->notify_read = tls_ustream_read_cb;
96 cl->us->notify_write = tls_ustream_write_cb;
97 cl->us->notify_state = tls_notify_state;
100 void uh_tls_client_detach(struct client *cl)
102 ustream_free(&cl->ssl.stream);