Beispiel #1
0
void be_connect(void *data, int fd)
{
    printf("connected %s %d\n", (char *)data, fd);
    epdata_t *epd = NULL;
    epd = smp_malloc(sizeof(epdata_t));

    if(!epd) {
        close(fd);
        return;
    }

    epd->fd = fd;
    epd->stime = now;

    epd->se_ptr = se_add(loop_fd, fd, epd);
    epd->timeout_ptr = add_timeout(epd, 1000, timeout_handle);

    epd->out_buf = smp_malloc(1024);
    epd->out_buf_len = sprintf(epd->out_buf,
                               "GET / HTTP/1.1\r\nUser-Agent: curl/7.24.0\r\nHost: www.163.com\r\nAccept: */*\r\n\r\n");
    epd->out_buf_sended = 0;

    se_be_read(epd->se_ptr, client_be_write);
    client_be_write(epd->se_ptr);
}
Beispiel #2
0
static int network_be_accept ( se_ptr_t *ptr )
{
    epdata_t *epd = NULL;
    int acc_trys = 0, client_fd = -1;
    struct sockaddr_in remote_addr;
    int addr_len = sizeof ( struct sockaddr_in );

    while ( acc_trys++ < 3 ) {
        //addr_len = sizeof( struct sockaddr_in );
        client_fd = accept ( server_fd, ( struct sockaddr * ) &remote_addr, &addr_len );

        if ( client_fd < 0 && errno != EAGAIN && errno != EWOULDBLOCK ) {
            break;

        }

        if ( client_fd < 0 ) {
            continue;
        }

        if ( !setnonblocking ( client_fd ) ) {
            close ( client_fd );
            return 0;
        }

        epd = malloc ( sizeof ( epdata_t ) );

        if ( !epd ) {
            close ( client_fd );
            return 0;
        }

        epd->fd = client_fd;
        epd->client_addr = remote_addr.sin_addr;
        epd->stime = now;
        epd->status = STEP_WAIT;
        epd->headers = NULL;
        epd->header_len = 0;
        epd->contents = NULL;
        epd->data_len = 0;
        epd->content_length = -1;
        epd->_header_length = 0;
        epd->keepalive = -1;
        epd->process_timeout = 0;
        epd->iov_buf_count = 0;
        epd->websocket = NULL;

        epd->se_ptr = se_add ( loop_fd, client_fd, epd );
        epd->timeout_ptr = add_timeout ( epd, STEP_WAIT_TIMEOUT, timeout_handle );

        se_be_read ( epd->se_ptr, network_be_read );

        serv_status.active_counts++;
        serv_status.connect_counts++;

        acc_trys = 0;
    }
}
static int lua_co_dup_setpeername(lua_State *L)
{
    if(!lua_isuserdata(L, 1) || !lua_isstring(L, 2)) {
        lua_pushnil(L);
        lua_pushstring(L, "Error params!");
        return 2;
    }

    cosocket_t *c*k = (cosocket_t *) lua_touserdata(L, 1);

    if(c*k->fd > -1) {
        se_delete(c*k->ptr);
        c*k->ptr = NULL;
        close(c*k->fd);
    }

    if(lua_isnumber(L, 3)) {
        int fd = 0, slen = sizeof(c*k->addr);
        bzero(&c*k->addr, slen);
        c*k->addr.sin_family = AF_INET;
        c*k->addr.sin_port = htons(lua_tonumber(L, 3));
        const char *host = lua_tostring(L, 2);

        if(inet_aton(host, &c*k->addr.sin_addr) == 0) {
            struct hostent *hp = NULL;

            if((hp = gethostbyname(host)) == 0) {
                lua_pushnil(L);
                lua_pushstring(L, "Nslookup Error!");
                return 2;
            }

            memcpy(&c*k->addr.sin_addr.s_addr, hp->h_addr, hp->h_length);
        }

        if((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
            lua_pushnil(L);
            lua_pushstring(L, "Connect Error!");
            return 2;
        }

        if(!se_set_nonblocking(fd , 1)) {
            close(fd);
            lua_pushnil(L);
            lua_pushstring(L, "Connect Error!");
            return 2;
        }

        c*k->ptr = se_add(_loop_fd, fd, c*k);
        c*k->fd = fd;
        c*k->status = 2;
        lua_pushboolean(L, 1);
        return 1;
    }

    return 0;
}
Beispiel #4
0
static void be_accept(int client_fd, struct in_addr client_addr)
{
    int sockaddr_len = sizeof(struct sockaddr);
    struct sockaddr_in addr;
    /* Disable the Nagle (TCP No Delay) algorithm */
    int flag = 1;
    int ret = setsockopt(client_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag));

    if(ret == -1) {
        LOGF(ERR, "Couldn't setsockopt(TCP_NODELAY)");
    }

    if(!set_nonblocking(client_fd, 1)) {
        close(client_fd);
        return;
    }

    epdata_t *epd = malloc(sizeof(epdata_t));

    if(!epd) {
        close(client_fd);
        return;
    }

    bzero(epd, sizeof(epdata_t));

    epd->L = new_lua_thread(_L);

    if(epd->L) {
        lua_pushlightuserdata(epd->L, epd);
        lua_setglobal(epd->L, "__epd__");
    }

    epd->fd = client_fd;
    epd->client_addr = client_addr;
    getsockname(epd->fd, (struct sockaddr *) &addr, &sockaddr_len);
    epd->server_addr = addr.sin_addr;
    epd->status = STEP_WAIT;
    epd->content_length = -1;
    epd->keepalive = -1;
    epd->response_sendfile_fd = -1;
    epd->timeout_type = 0;
    //epd->start_time = longtime();

    epd->se_ptr = se_add(loop_fd, client_fd, epd);
    epd->timeout_ptr = add_timeout(epd, STEP_WAIT_TIMEOUT, timeout_handle);

    se_be_read(epd->se_ptr, network_be_read);

    serv_status.active_counts++;
    serv_status.connect_counts++;
}
Beispiel #5
0
static void be_ssl_accept(int client_fd, struct in_addr client_addr)
{
    if(!set_nonblocking(client_fd, 1)) {
        close(client_fd);
        return;
    }

    epdata_t *epd = malloc(sizeof(epdata_t));

    if(!epd) {
        close(client_fd);
        return;
    }

    bzero(epd, sizeof(epdata_t));

    if(NULL == ssl_ctx) {
        free(epd);
        LOGF(ERR, "ssl ctx not inited");
        close(client_fd);
        return;
    }

    if(ssl_epd_idx == -1) {
        epd->ssl_verify = 1;
    }

    epd->L = new_lua_thread(_L);

    if(epd->L) {
        lua_pushlightuserdata(epd->L, epd);
        lua_setglobal(epd->L, "__epd__");
    }

    epd->fd = client_fd;
    epd->client_addr = client_addr;
    epd->status = STEP_WAIT;
    epd->content_length = -1;
    epd->keepalive = -1;
    epd->response_sendfile_fd = -1;
    //epd->start_time = longtime();

    epd->se_ptr = se_add(loop_fd, client_fd, epd);
    epd->timeout_ptr = add_timeout(epd, STEP_WAIT_TIMEOUT, timeout_handle);

    se_be_read(epd->se_ptr, _be_ssl_accept);

    serv_status.active_counts++;
    serv_status.connect_counts++;
}
Beispiel #6
0
void network_worker ( void *_process_func, int process_count, int process_at )
{
    if ( sizeof ( epdata_t ) != 4096 ) {
        printf ( "warning sizof(epdata_t) = %ld\n", sizeof ( epdata_t ) );    // must be 4096
    }

    signal ( SIGPIPE, SIG_IGN );

    _shm_serv_status = shm_malloc ( sizeof ( serv_status_t ) );

    if ( _shm_serv_status == NULL ) {
        perror ( "shm malloc failed\n" );
        signal ( SIGHUP, SIG_IGN );
        exit ( 1 );
    }

    shm_serv_status = _shm_serv_status->p;
    memcpy ( shm_serv_status, &serv_status, sizeof ( serv_status_t ) );

    process_func = _process_func;

    init_mime_types();

    loop_fd = se_create ( EPD_POOL_SIZE );
    set_loop_fd ( loop_fd, process_count );

    se_ptr_t *se = se_add ( loop_fd, server_fd, NULL );

    se_be_read ( se, network_be_accept );

    while ( 1 ) {

        se_loop ( loop_fd, EPOLL_WAITOUT, without_jobs );

        if ( checkProcessForExit() || has_error_for_exit ) {
            break;
        }
    }
}
Beispiel #7
0
static int si_row_end(void *p, unsigned row)
{
	struct service_info *si = p;
	struct domain *dom;

	assert(si != NULL);
	if (!row) /* ignore first row */
		return 0;

	if (!si->domain || !si->path_prefix || !si->module)
		return -1;

	dom = dom_find(si->domain);
	if (!dom)
		return -1;

	if (se_add(dom, si->path_prefix, si->module))
		return -1;

	si_free(si);
	return 0;
}
Beispiel #8
0
static void be_accept(int client_fd, struct in_addr client_addr)
{
    if(!set_nonblocking(client_fd, 1)) {
        close(client_fd);
        return;
    }

    epdata_t *epd = NULL;
    epd = smp_malloc(sizeof(epdata_t));

    if(!epd) {
        close(client_fd);
        return;
    }

    epd->fd = client_fd;
    epd->client_addr = client_addr;
    epd->stime = now;

    epd->se_ptr = se_add(loop_fd, client_fd, epd);
    epd->timeout_ptr = add_timeout(epd, 1000, timeout_handle);

    se_be_read(epd->se_ptr, be_read);
}
static int _be_connect(cosocket_t *c*k, int fd, int yielded)
{
    int flag = 1;
    int ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag));

    c*k->fd = fd;
    c*k->ptr = se_add(_loop_fd, fd, c*k);

    c*k->status = 2;
    c*k->in_read_action = 0;

    if(c*k->use_ssl) {
        if(c*k->ctx == NULL) {
            c*k->ctx = SSL_CTX_new(SSLv23_client_method());

            if(c*k->ctx == NULL) {
                connection_pool_counter_operate(c*k->pool_key, -1);
                se_delete(c*k->ptr);
                close(c*k->fd);

                c*k->ptr = NULL;
                c*k->fd = -1;
                c*k->status = 0;

                lua_pushnil(c*k->L);
                lua_pushstring(c*k->L, "SSL_CTX_new Error");
                return 2;
            }
        }

        c*k->ssl = SSL_new(c*k->ctx);

        if(c*k->ssl == NULL) {
            connection_pool_counter_operate(c*k->pool_key, -1);
            se_delete(c*k->ptr);
            close(c*k->fd);

            c*k->ptr = NULL;
            c*k->fd = -1;
            c*k->status = 0;

            SSL_CTX_free(c*k->ctx);
            c*k->ctx = NULL;

            lua_pushnil(c*k->L);
            lua_pushstring(c*k->L, "SSL_new Error");
            return 2;
        }

        SSL_set_fd(c*k->ssl, c*k->fd);
        se_be_read(c*k->ptr, cosocket_be_ssl_connected);

        if(SSL_connect(c*k->ssl) == 1) {
            se_be_pri(c*k->ptr, NULL);
            lua_pushboolean(c*k->L, 1);
            c*k->inuse = 0;
            return 1;
        }

        c*k->timeout_ptr = add_timeout(c*k, c*k->timeout, timeout_handle);

        return -2;
    }

    se_be_pri(c*k->ptr, NULL);
    lua_pushboolean(c*k->L, 1);
    c*k->inuse = 0;

    return 1;
}