UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef IOSTREAM_H 30 #define IOSTREAM_H 31 32 #include <openssl/ssl.h> 33 #include "../public/nsapi.h" 34 #include "systems.h" 35 36 #ifdef _WIN32 37 #include <windows.h> 38 #include <winsock2.h> 39 #endif 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 45 #ifdef XP_UNIX 46 #define SYS_SOCKET int 47 #elif defined(XP_WIN32) 48 #define SYS_SOCKET SOCKET 49 #endif 50 51 #define IO_MODE_BLOCKING 0 52 #define IO_MODE_NONBLOCKING 1 53 54 #define IO_POLL_NONE 0 55 #define IO_POLL_IN 1 56 #define IO_POLL_OUT 2 57 58 typedef struct IOStream IOStream; 59 typedef struct SysStream SysStream; 60 typedef struct HttpStream HttpStream; 61 62 typedef ssize_t(*io_write_f)(IOStream *, void *, size_t); 63 typedef ssize_t(*io_writev_f)(IOStream *, struct iovec *, int); 64 typedef ssize_t(*io_read_f)(IOStream *, void *, size_t); 65 typedef ssize_t(*io_sendfile_f)(IOStream *, sendfiledata *); 66 typedef void(*io_close_f)(IOStream *); 67 typedef void(*io_finish_f)(IOStream *); 68 typedef void(*io_setmode_f)(IOStream *, int); 69 typedef int(*io_poll_f)(IOStream *, EventHandler *, int, Event *); 70 71 struct IOStream { 72 io_write_f write; 73 io_writev_f writev; 74 io_read_f read; 75 io_sendfile_f sendfile; 76 io_close_f close; 77 io_finish_f finish; 78 io_setmode_f setmode; 79 io_poll_f poll; 80 int io_errno; 81 }; 82 83 struct SysStream { 84 IOStream st; 85 #ifdef XP_UNIX 86 int fd; 87 #elif defined(XP_WIN32) 88 SOCKET fd; 89 #endif 90 }; 91 92 struct HttpStream { 93 IOStream st; 94 IOStream *fd; 95 uint64_t max_read; 96 uint64_t read; 97 WSBool chunked_enc; 98 WSBool buffered; 99 }; 100 101 typedef struct SSLStream { 102 IOStream st; 103 SSL *ssl; 104 int error; 105 } SSLStream; 106 107 108 109 /* system stream */ 110 IOStream* sysstream_new(pool_handle_t *pool, SYS_SOCKET fd); 111 112 ssize_t net_sys_write(SysStream *st, void *buf, size_t nbytes); 113 ssize_t net_sys_writev(SysStream *st, struct iovec *iovec, int iovcnt); 114 ssize_t net_sys_read(SysStream *st, void *buf, size_t nbytes); 115 ssize_t net_sys_sendfile(SysStream *st, sendfiledata *sfd); 116 void net_sys_close(SysStream *st); 117 void net_sys_setmode(SysStream *st, int mode); 118 int net_sys_poll(SysStream *st, EventHandler *ev, int events, Event *cb); 119 120 /* http stream */ 121 IOStream* httpstream_new(pool_handle_t *pool, IOStream *fd); 122 123 ssize_t net_http_write(HttpStream *st, void *buf, size_t nbytes); 124 ssize_t net_http_writev(HttpStream *st, struct iovec *iovec, int iovcnt); 125 ssize_t net_http_read(HttpStream *st, void *buf, size_t nbytes); 126 ssize_t net_http_sendfile(HttpStream *st, sendfiledata *sfd); 127 void net_http_close(HttpStream *st); 128 void net_http_finish(HttpStream *st); 129 void net_http_setmode(HttpStream *st, int mode); 130 int net_http_poll(HttpStream *st, EventHandler *ev, int events, Event *cb); 131 132 /* ssl stream */ 133 IOStream* sslstream_new(pool_handle_t *pool, SSL *ssl); 134 135 ssize_t net_ssl_write(SSLStream *st, void *buf, size_t nbytes); 136 ssize_t net_ssl_writev(SSLStream *st, struct iovec *iovec, int iovcnt); 137 ssize_t net_ssl_read(SSLStream *st, void *buf, size_t nbytes); 138 void net_ssl_close(SSLStream *st); 139 void net_ssl_finish(SSLStream *st); 140 void net_ssl_setmode(SSLStream *st, int mode); 141 int net_ssl_poll(SSLStream *st, EventHandler *ev, int events, Event *cb); 142 143 /* net_ functions */ 144 ssize_t net_fallback_sendfile(IOStream *fd, sendfiledata *sfd); 145 void net_finish(SYS_NETFD fd); 146 147 #ifdef __cplusplus 148 } 149 #endif 150 151 #endif /* IOSTREAM_H */ 152 153