build: remove install prefix override
[project/ustream-ssl.git] / ustream-ssl.c
index afb01f35f1d6b9a3cc16d9c85ca1568c67748a87..bbc6b1132871414f588a07e60bf5430fc4dbf538 100644 (file)
@@ -1,3 +1,21 @@
+/*
+ * ustream-ssl - library for SSL over ustream
+ *
+ * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
 #include <errno.h>
 
 #include <openssl/ssl.h>
@@ -62,21 +80,22 @@ static void ustream_ssl_check_conn(struct ustream_ssl *us)
        ustream_ssl_error(us, ret);
 }
 
-static void ustream_ssl_notify_read(struct ustream *s, int bytes)
+static bool __ustream_ssl_poll(struct ustream *s)
 {
        struct ustream_ssl *us = container_of(s->next, struct ustream_ssl, stream);
        char *buf;
-       int wr = 0, len, ret;
+       int len, ret;
+       bool more = false;
 
        ustream_ssl_check_conn(us);
        if (!us->connected || us->error)
-               return;
-
-       buf = ustream_reserve(&us->stream, 1, &len);
-       if (!len)
-               return;
+               return false;
 
        do {
+               buf = ustream_reserve(&us->stream, 1, &len);
+               if (!len)
+                       break;
+
                ret = SSL_read(us->ssl, buf, len);
                if (ret < 0) {
                        ret = SSL_get_error(us->ssl, ret);
@@ -93,13 +112,16 @@ static void ustream_ssl_notify_read(struct ustream *s, int bytes)
                        break;
                }
 
-               buf += ret;
-               len -= ret;
-               wr += ret;
+               ustream_fill_read(&us->stream, ret);
+               more = true;
        } while (1);
 
-       if (wr)
-               ustream_fill_read(&us->stream, wr);
+       return more;
+}
+
+static void ustream_ssl_notify_read(struct ustream *s, int bytes)
+{
+       __ustream_ssl_poll(s);
 }
 
 static void ustream_ssl_notify_write(struct ustream *s, int bytes)
@@ -165,6 +187,15 @@ static void ustream_ssl_free(struct ustream *s)
        us->error = false;
 }
 
+static bool ustream_ssl_poll(struct ustream *s)
+{
+       struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
+       bool fd_poll;
+
+       fd_poll = ustream_poll(us->conn);
+       return __ustream_ssl_poll(s) || fd_poll;
+}
+
 static void ustream_ssl_stream_init(struct ustream_ssl *us)
 {
        struct ustream *conn = us->conn;
@@ -176,6 +207,7 @@ static void ustream_ssl_stream_init(struct ustream_ssl *us)
 
        s->free = ustream_ssl_free;
        s->write = ustream_ssl_write;
+       s->poll = ustream_ssl_poll;
        s->set_read_blocked = ustream_ssl_set_read_blocked;
        ustream_init_defaults(s);
 }