src/server/util/io.h

Sat, 21 Jan 2017 12:46:31 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 21 Jan 2017 12:46:31 +0100
changeset 150
89af278b7dbb
parent 147
d050449c3b9e
child 161
aadda87bad1b
child 172
5580517faafc
permissions
-rw-r--r--

fixes socket fd leak when SSL_accept fails

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef IOSTREAM_H
#define	IOSTREAM_H

#include <openssl/ssl.h> 
#include "../public/nsapi.h"
#include "systems.h"

#ifdef _WIN32
#include <windows.h>
#include <winsock2.h>
#endif

#ifdef	__cplusplus
extern "C" {
#endif

#ifdef XP_UNIX
#define SYS_SOCKET int
#elif defined(XP_WIN32)
#define SYS_SOCKET SOCKET
#endif

typedef struct IOStream     IOStream;
typedef struct SysStream    SysStream;
typedef struct HttpStream   HttpStream;

typedef ssize_t(*io_write_f)(IOStream *, void *, size_t);
typedef ssize_t(*io_writev_f)(IOStream *, struct iovec *, int);
typedef ssize_t(*io_read_f)(IOStream *, void *, size_t);
typedef ssize_t(*io_sendfile_f)(IOStream *, sendfiledata *);
typedef void(*io_close_f)(IOStream *);
typedef void(*io_finish_f)(IOStream *);

struct IOStream {
    io_write_f    write;
    io_writev_f   writev;
    io_read_f     read;
    io_sendfile_f sendfile;
    io_close_f    close;
    io_finish_f   finish;
};

struct SysStream {
    IOStream st;
#ifdef XP_UNIX
    int      fd;
#elif defined(XP_WIN32)
    SOCKET   fd;
#endif
};

struct HttpStream {
    IOStream st;
    IOStream *fd;
    uint64_t max_read;
    uint64_t read;
    WSBool   chunked_enc;
    WSBool   buffered;
};

typedef struct SSLStream {
    IOStream st;
    SSL      *ssl;
    int      error;
} SSLStream;



/* system stream */
IOStream* sysstream_new(pool_handle_t *pool, SYS_SOCKET fd);

ssize_t net_sys_write(SysStream *st, void *buf, size_t nbytes);
ssize_t net_sys_writev(SysStream *st, struct iovec *iovec, int iovcnt);
ssize_t net_sys_read(SysStream *st, void *buf, size_t nbytes);
ssize_t net_sys_sendfile(SysStream *st, sendfiledata *sfd);
void net_sys_close(SysStream *st);

/* http stream */
IOStream* httpstream_new(pool_handle_t *pool, IOStream *fd);

ssize_t net_http_write(HttpStream *st, void *buf, size_t nbytes);
ssize_t net_http_writev(HttpStream *st, struct iovec *iovec, int iovcnt);
ssize_t net_http_read(HttpStream *st, void *buf, size_t nbytes);
ssize_t net_http_sendfile(HttpStream *st, sendfiledata *sfd);
void    net_http_close(HttpStream *st);
void    net_http_finish(HttpStream *st);

/* ssl stream */
IOStream* sslstream_new(pool_handle_t *pool, SSL *ssl);

ssize_t net_ssl_write(SSLStream *st, void *buf, size_t nbytes);
ssize_t net_ssl_writev(SSLStream *st, struct iovec *iovec, int iovcnt);
ssize_t net_ssl_read(SSLStream *st, void *buf, size_t nbytes);
void    net_ssl_close(SSLStream *st);
void    net_ssl_finish(SSLStream *st);

/* net_ functions */
ssize_t net_fallback_sendfile(IOStream *fd, sendfiledata *sfd);
void    net_finish(SYS_NETFD fd);

#ifdef	__cplusplus
}
#endif

#endif	/* IOSTREAM_H */

mercurial