Example #1
0
int listeners_Create(int port, int ssl, void* userdata) {
    // startup socket system
    if (!so_Startup()) {
        return 0;
    }

    // check if a server is already using this port:
    if (listeners_GetByPort(port, NULL)) {
#ifdef CONNECTIONSDEBUG
        printinfo("[connections-server] port %d already used");
#endif
        return 0;
    }

    // allocate struct:
    struct listener* l = malloc(sizeof(*l));
    if (!l) {
#ifdef CONNECTIONSDEBUG
        printinfo("[connections-server] malloc failed");
#endif
        return 0;
    }

    // initialise struct:
    memset(l, 0, sizeof(*l));
    l->socket = so_CreateSocket(1, IPTYPE_IPV6);
    l->port = port;
    l->ssl = ssl;
    l->userdata = userdata;
    if (l->socket < 0) {
#ifdef CONNECTIONSDEBUG
        printinfo("[connections-server] so_CreateSocket() failed");
#endif
        free(l);
        return 0;
    }
    if (!so_MakeSocketListen(l->socket, port, IPTYPE_IPV6, "any")) {
#ifdef CONNECTIONSDEBUG
        printinfo("[connections-server] so_MakeSocketListen() failed");
#endif
        so_CloseSocket(l->socket);
        free(l);
        return 0;
    }
    l->next = listeners;
    listeners = l;
#ifdef CONNECTIONSDEBUG
    printinfo("[connections-server] new server at port %d (%d)", l->port, l->socket);
#endif
    return 1;
}
Example #2
0
// Set a new socket to the given connection
static int connections_SetSocket(struct connection* c, int iptype) {
    // close old socket
    if (c->socket >= 0) {
        so_CloseSSLSocket(c->socket, &c->sslptr);
    }
    // create socket
    if (iptype == IPTYPE_IPV4) {
        c->socket = so_CreateSocket(1, IPTYPE_IPV4);
        c->iptype = IPTYPE_IPV4;
    } else {
        c->socket = so_CreateSocket(1, IPTYPE_IPV6);
        c->iptype = IPTYPE_IPV6;
    }
    // check if socket is there
    if (c->socket < 0) {
        return 0;
    }
    // set a low delay option if desired
    if (c->lowdelay) {
        int flag = 1;
        setsockopt(c->socket, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
    }
    return 1;
}