src/server/daemon/session.c

Fri, 05 Dec 2025 17:37:48 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 05 Dec 2025 17:37:48 +0100
changeset 649
3887fd7e8bd7
parent 415
d938228c382e
permissions
-rw-r--r--

add session_create

/*
 * 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.
 */

#include "../public/nsapi.h"

#include "session.h"

NSAPISession* nsapisession_create(pool_handle_t *pool) {
    NSAPISession *sn = pool_malloc(pool, sizeof(NSAPISession));
    if(!sn) {
        return NULL;
    }
    
    ZERO(sn, sizeof(NSAPISession));
    
    sn->sn.pool = pool;
    
    sn->sn.client = pblock_create_pool(sn->sn.pool, 8);
    if(!sn->sn.client) {
        pool_free(pool, sn);
        return NULL;
    }
    sn->sn.fill = 1;
    
    return sn;
}

int nsapisession_setconnection(NSAPISession *sn, Connection *conn, netbuf *inbuf, IOStream **io) {
    SessionHandler *sh = conn->session_handler;
    WSBool ssl;
    IOStream *sio = sh->create_iostream(sh, conn, sn->sn.pool, &ssl);
    if(!sio) {
        return 1;
    }
    *io = sio;
    IOStream *http = httpstream_new(sn->sn.pool, sio);
    if(!http) {
        return 1;
    }
    sn->connection = conn;
    sn->netbuf = inbuf;
    sn->sn.csd = http;
    sn->sn.ssl = ssl;
    sn->sn.inbuf = inbuf;
    sn->sn.inbuf->sd = http;
    return 0;
}

int nsapisession_set_stream(NSAPISession *sn, SYS_NETFD csd) {
    IOStream *http = httpstream_new(sn->sn.pool, csd);
    if(!http) {
        return 1;
    }
    netbuf *inbuf = netbuf_open(csd, 1024);
    if(!inbuf) {
        return 1;
    }
    sn->sn.csd = http;
    sn->sn.inbuf = inbuf;
    sn->netbuf = inbuf;
    return 0;
}

NSAPI_PUBLIC Session *session_create(SYS_NETFD csd, struct sockaddr_in *sac) {
    pool_handle_t *pool = pool_create();
    if(!pool) {
        return NULL;
    }
    
    NSAPISession *sn = nsapisession_create(pool);
    if(!sn) {
        pool_destroy(pool);
        return NULL;
    }
    
    if(nsapisession_set_stream(sn, csd)) {
        pool_destroy(pool);
        return NULL;
    }
    
    sn->sn.iaddr = sac->sin_addr;
    sn->sn.csd_open = 1;
    sn->sn.fill = 0;
    sn->sn.ssl = 0;
    sn->sn.clientauth = 0;
    
    return (Session*)sn;
}

NSAPI_PUBLIC char *session_dns_lookup(Session *s, int verify) {
    // TODO: implement
    return NULL;
}

// new
NSAPI_PUBLIC void* session_get_config(Session *s) {
    NSAPISession *sn = (NSAPISession*)s;
    return sn->config;
}

mercurial