Beispiel #1
0
int client_socket (char * host, char * port) {
    int status;
    int lsock = tcpsocket();
    if (lsock<0) {
        logging(LOG_ERROR, "can't create socket !!!");
        return -1;
    }
    if (tcpnonblock(lsock)<0) {
        logging(LOG_ERROR, "set nonblock, error: %m");
        tcpclose(lsock);
        return -1;
    }
    status = tcpstrconnect(lsock, host, port);
    if (status<0) {
        logging(LOG_WARN, "connect failed, error: %m");
        tcpclose(lsock);
        return -1;
    }
    if (status==0) { // connected immediately
        logging(LOG_INFO,"connected immediately");
        tcpnodelay(lsock);
    } else {
        logging(LOG_INFO,"connecting to %s:%s...", host, port);
    }
    return lsock;
}
Beispiel #2
0
static int write_data_refresh_connection(uint32_t inode, uint32_t indx, uint64_t *chunkid)
{
	uint32_t ip;
	uint16_t port;
	uint8_t status;

	if (rw_sock >= 0) {
		tcpclose(rw_sock);
		rw_sock = -1;
	}

	status = fs_writechunk(&ip,&port, chunkid, inode, indx);
	if (status != 0) {
		fprintf(stderr, "status error\n");
		return -2;
	}

	rw_sock = tcpsocket();
	if (rw_sock < 0) {
		fprintf(stderr,"can't create tcp socket\n");
		return -1;
	}
	if (tcpnodelay(rw_sock)<0) {
		fprintf(stderr,"can't set TCP_NODELAY\n");
	}
	if (tcpnumconnect(rw_sock, ip, port)<0) {
		fprintf(stderr,"can't connect to (%d.%d.%d.%d:%u)\n",
		ip/(256*256*256)%256, ip/(256*256)%256, ip/256%256, ip%256, port);
		tcpclose(rw_sock);
		rw_sock = -1;
		return -1;
	}
	return 0;
}
Beispiel #3
0
/** send Update Message to server*/
void DNSUpdate::sendMsgTCP(unsigned int timeout){
    DnsMessage *a = NULL;
    int sockid = -1;
    
    try {
	pos_cliresolver res;
	res.tcp_timeout = timeout;
	sockid = res.tcpconnect(&server);
	res.tcpsendmessage(message, sockid);
	res.tcpwaitanswer(a, sockid);
	if (a->RCODE != RCODE_NOERROR) {
	    throw PException((char*)str_rcode(a->RCODE).c_str());
	}
    } catch (PException p) {
	
	if (a) 
	    delete a;
	if (sockid != -1) 
	    tcpclose(sockid);
	
	throw PException(p.message);
    }
	
    if (a) 
	delete a;
    if (sockid != -1) 
	tcpclose(sockid);
}
Beispiel #4
0
int main() {
    tcpsock ls = tcplisten("*:5555");
    assert(ls);

    go(client());

    tcpsock as = tcpaccept(ls);

    tcpsend(as, "ABC", 3);
    int rc = tcpflush(as);
    assert(rc == 0);

    char buf[16];
    ssize_t sz = tcprecvuntil(as, buf, sizeof(buf), '\n');
    assert(sz == 4);
    assert(buf[0] == '1' && buf[1] == '2' && buf[2] == '3' && buf[3] == '\n');
    sz = tcprecvuntil(as, buf, sizeof(buf), '\n');
    assert(sz == 3);
    assert(buf[0] == '4' && buf[1] == '5' && buf[2] == '\n');
    sz = tcprecvuntil(as, buf, 3, '\n');
    assert(sz == 0);
    assert(buf[0] == '6' && buf[1] == '7' && buf[2] == '8');

    tcpclose(as);
    tcpclose(ls);

    return 0;
}
Beispiel #5
0
int masterproxy_init(void) {
	pthread_attr_t thattr;

	lsock = tcpsocket();
	if (lsock<0) {
		//mfs_errlog(LOG_ERR,"main master server module: can't create socket");
		return -1;
	}
	tcpnonblock(lsock);
	tcpnodelay(lsock);
	// tcpreuseaddr(lsock);
	if (tcpsetacceptfilter(lsock)<0 && errno!=ENOTSUP) {
		// mfs_errlog_silent(LOG_NOTICE,"master proxy: can't set accept filter");
	}
	if (tcpstrlisten(lsock,"127.0.0.1",0,100)<0) {
		// mfs_errlog(LOG_ERR,"main master server module: can't listen on socket");
		tcpclose(lsock);
		lsock = -1;
		return -1;
	}
	if (tcpgetmyaddr(lsock,&proxyhost,&proxyport)<0) {
		tcpclose(lsock);
		lsock = -1;
		return -1;
	}

	terminate = 0;
	pthread_attr_init(&thattr);
	pthread_attr_setstacksize(&thattr,0x100000);
	//pthread_create(&proxythread,&thattr,masterproxy_loop,NULL);
	pthread_create(&proxythread,&thattr,masterproxy_acceptor,NULL);
	pthread_attr_destroy(&thattr);

	return 1;
}
Beispiel #6
0
void csserv_term(void) {
	csserventry *eptr,*eaptr;
	packetstruct *pptr,*paptr;

	syslog(LOG_NOTICE,"closing %s:%s",ListenHost,ListenPort);
	tcpclose(lsock);

	eptr = csservhead;
	while (eptr) {
		tcpclose(eptr->sock);
		if (eptr->inputpacket.packet) {
			free(eptr->inputpacket.packet);
		}
		pptr = eptr->outputhead;
		while (pptr) {
			if (pptr->packet) {
				free(pptr->packet);
			}
			paptr = pptr;
			pptr = pptr->next;
			free(paptr);
		}
		eaptr = eptr;
		eptr = eptr->next;
		free(eaptr);
	}
	csservhead=NULL;
	free(ListenHost);
	free(ListenPort);
}
Beispiel #7
0
/** get old RR entry from Dns Server */ 
DnsRR* DNSUpdate::get_oldDnsRR(){
 
    DnsRR* RemoteDnsRR= NULL;
    DnsMessage *q = NULL, *a = NULL;
    int sockid = -1;
    
    try {
	q = create_query(*zoneroot, QTYPE_AXFR);
	
	pos_cliresolver res;   
	sockid = res.tcpconnect(&server);  
	res.tcpsendmessage(q, sockid);
	
	res.tcpwaitanswer(a, sockid);
	if (a->RCODE != RCODE_NOERROR){    
	    
	    throw PException((char*)str_rcode(a->RCODE).c_str()); 
	}
	if (!a->answers.empty())
	    {
		RemoteDnsRR = new DnsRR();
		if (this->DnsRR_avail(a,*RemoteDnsRR) == false) 
		    {   
			delete RemoteDnsRR;
			RemoteDnsRR = NULL;
		    }
	    }
	
	if (a) {
	    // delete a; // FIXME: Why is this commented out? Memory leak!
	    a = 0;
	}
	if (q) {
	    delete q;
	    q = 0;
	}
	
	if (sockid != -1) 
	    tcpclose(sockid);
	
	return RemoteDnsRR;
	
    } catch (PException p) {
	if (q) {
	    delete q;
	    q = NULL;
	}

	if (a) {
	    delete a;
	    a = NULL;
	}
	if (sockid != -1) 
	    tcpclose(sockid);
	if (RemoteDnsRR) 
	    delete RemoteDnsRR;
	Log(Error) << "DDNS: Attempt to get old DNS record failed." << LogEnd;
	return 0;
    } 
}
Beispiel #8
0
int masterconn_initconnect(serventry *eptr) {
	int status;
	if (eptr->masteraddrvalid==0) {
		uint32_t mip,bip;
		uint16_t mport;
		if (tcpresolve(BindHost,NULL,&bip,NULL,1)>=0) {
			eptr->bindip = bip;
		} else {
			eptr->bindip = 0;
		}
		if (tcpresolve(MasterHost,MasterPort,&mip,&mport,0)>=0) {
			eptr->masterip = mip;
			eptr->masterport = mport;
			eptr->masteraddrvalid = 1;
		} else {
            MFSLOG(LOG_WARNING,"can't resolve master host/port (%s:%s)",MasterHost,MasterPort);
			return -1;
		}
	}
	eptr->sock=tcpsocket();
	if (eptr->sock<0) {
			MFSLOG(LOG_WARNING,"create socket, error: %m");
		return -1;
	}
	if (tcpnonblock(eptr->sock)<0) {
        MFSLOG(LOG_WARNING,"set nonblock, error: %m");
		tcpclose(eptr->sock);
		eptr->sock=-1;
		return -1;
	}
	if (eptr->bindip>0) {
		if (tcpnumbind(eptr->sock,eptr->bindip,0)<0) {
            MFSLOG(LOG_WARNING,"can't bind socket to given ip: %m");
			tcpclose(eptr->sock);
			eptr->sock=-1;
			return -1;
		}
	}
	status = tcpnumconnect(eptr->sock,eptr->masterip,eptr->masterport);
	if (status<0) {
        MFSLOG(LOG_WARNING,"connect failed, error: %m");
		tcpclose(eptr->sock);
		eptr->sock=-1;
		return -1;
	}
	if (status==0) {
		MFSLOG(LOG_NOTICE,"connected to Master immediately");
		masterconn_connected(eptr);
	} else {
		eptr->mode = CONNECTING;
		MFSLOG(LOG_NOTICE,"connecting ...");
	}
//	syslog(LOG_NOTICE,"the masconn's sock is %d,the connection id is %d,masterconnsingleton's sock is %d,masterconnsingleton's id is %d",eptr->sock,eptr->connection,masterconnsingleton->sock,masterconnsingleton->connection);
	return 0;
}
Beispiel #9
0
int masterconn_initconnect(masterconn *eptr) {
    int status;
    if (eptr->masteraddrvalid==0) {
        uint32_t mip,bip;
        uint16_t mport;
        if (tcpresolve(BindHost,NULL,&bip,NULL,1)<0) {
            bip = 0;
        }
        eptr->bindip = bip;
        if (tcpresolve(MasterHost,MasterPort,&mip,&mport,0)>=0) {
            eptr->masterip = mip;
            eptr->masterport = mport;
            eptr->masteraddrvalid = 1;
        } else {
            mfs_arg_syslog(LOG_WARNING,"can't resolve master host/port (%s:%s)",MasterHost,MasterPort);
            return -1;
        }
    }
    eptr->sock=tcpsocket();
    if (eptr->sock<0) {
        mfs_errlog(LOG_WARNING,"create socket, error");
        return -1;
    }
    if (tcpnonblock(eptr->sock)<0) {
        mfs_errlog(LOG_WARNING,"set nonblock, error");
        tcpclose(eptr->sock);
        eptr->sock = -1;
        return -1;
    }
    if (eptr->bindip>0) {
        if (tcpnumbind(eptr->sock,eptr->bindip,0)<0) {
            mfs_errlog(LOG_WARNING,"can't bind socket to given ip");
            tcpclose(eptr->sock);
            eptr->sock = -1;
            return -1;
        }
    }
    status = tcpnumconnect(eptr->sock,eptr->masterip,eptr->masterport);
    if (status<0) {
        mfs_errlog(LOG_WARNING,"connect failed, error");
        tcpclose(eptr->sock);
        eptr->sock = -1;
        eptr->masteraddrvalid = 0;
        return -1;
    }
    if (status==0) {
        syslog(LOG_NOTICE,"connected to Master immediately");
        masterconn_connected(eptr);
    } else {
        eptr->mode = CONNECTING;
        eptr->conntime = monotonic_seconds();
        syslog(LOG_NOTICE,"connecting ...");
    }
    return 0;
}
Beispiel #10
0
void matomlserv_reload(void) {
	char *oldListenHost,*oldListenPort;
	int newlsock;

	oldListenHost = ListenHost;
	oldListenPort = ListenPort;
	ListenHost = cfg_getstr("MATOML_LISTEN_HOST","*");
	ListenPort = cfg_getstr("MATOML_LISTEN_PORT","9419");
	if (strcmp(oldListenHost,ListenHost)==0 && strcmp(oldListenPort,ListenPort)==0) {
		free(oldListenHost);
		free(oldListenPort);
		mfs_arg_syslog(LOG_NOTICE,"master <-> metaloggers module: socket address hasn't changed (%s:%s)",ListenHost,ListenPort);
		return;
	}

	newlsock = tcpsocket();
	if (newlsock<0) {
		mfs_errlog(LOG_WARNING,"master <-> metaloggers module: socket address has changed, but can't create new socket");
		free(ListenHost);
		free(ListenPort);
		ListenHost = oldListenHost;
		ListenPort = oldListenPort;
		return;
	}
	tcpnonblock(newlsock);
	tcpnodelay(newlsock);
	tcpreuseaddr(newlsock);
	if (tcpsetacceptfilter(newlsock)<0 && errno!=ENOTSUP) {
		mfs_errlog_silent(LOG_NOTICE,"master <-> metaloggers module: can't set accept filter");
	}
	if (tcpstrlisten(newlsock,ListenHost,ListenPort,100)<0) {
		mfs_arg_errlog(LOG_ERR,"master <-> metaloggers module: socket address has changed, but can't listen on socket (%s:%s)",ListenHost,ListenPort);
		free(ListenHost);
		free(ListenPort);
		ListenHost = oldListenHost;
		ListenPort = oldListenPort;
		tcpclose(newlsock);
		return;
	}
	mfs_arg_syslog(LOG_NOTICE,"master <-> metaloggers module: socket address has changed, now listen on %s:%s",ListenHost,ListenPort);
	free(oldListenHost);
	free(oldListenPort);
	tcpclose(lsock);
	lsock = newlsock;

	ChangelogSecondsToRemember = cfg_getuint16("MATOML_LOG_PRESERVE_SECONDS",600);
	if (ChangelogSecondsToRemember>3600) {
		syslog(LOG_WARNING,"Number of seconds of change logs to be preserved in master is too big (%"PRIu16") - decreasing to 3600 seconds",ChangelogSecondsToRemember);
		ChangelogSecondsToRemember=3600;
	}
}
Beispiel #11
0
/*
 *
    should use evbuffer_free(evb); to free this evbuffer
 * */
static struct http_response * http_req(char * ip, int port, char * verb, char * path, char * data){

    char port_str[6];
    sprintf(port_str, "%d", port);

    int cs = client_socket(ip, port_str);
    char * request_buffer = alloca(100 + strlen(path)); // in stack do not need free
    char response_buffer[1024*8];

    sprintf(request_buffer, "%s %s HTTP/1.0\r\nContent-Length: %d\r\n\r\n%s\r\n\r\n", verb, path, strlen(data), data);
    //sprintf(request_buffer, "GET %s HTTP/1.0\r\nUser-Agent: curl/7.19.7 (i486-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15\r\n\r\n", path);
    fprintf(stderr, "%s", request_buffer);

    int rst = tcptowrite(cs, request_buffer, strlen(request_buffer) , 30000);
    printf("tcptowrite rst: %d", rst);
    int readed = 0;

    struct evbuffer *evb = evbuffer_new();
    /*evbuffer_read(evb, cs, 30);*/

    while ((readed = tcptoread(cs, response_buffer, 1024*8, 30000)) > 0){
        printf("readed len: %d \n", readed);
        evbuffer_add(evb, response_buffer, readed);
    }

    struct evbuffer_ptr evbptr = evbuffer_search(evb, "\r\n\r\n", 4, NULL);

    struct evbuffer *headers = evbuffer_new();
    evbuffer_remove_buffer(evb, headers, evbptr.pos);
    /*evbuffer_drain(evb, evbptr.pos);*/
    tcpclose(cs);
    int code = parse_response_status_code(headers);
    return http_response_new(code, headers, evb);
}
Beispiel #12
0
static void rep_cleanup(replication *r) {
	int i;
	if (r->opened) {
		hdd_close(r->chunkid);
	}
	if (r->created) {
		hdd_delete(r->chunkid,0);
	}
	for (i=0 ; i<r->srccnt ; i++) {
		if (r->repsources[i].sock>=0) {
			tcpclose(r->repsources[i].sock);
		}
		if (r->repsources[i].packet) {
			free(r->repsources[i].packet);
		}
	}
	if (r->fds) {
		free(r->fds);
	}
	if (r->repsources) {
		free(r->repsources);
	}
	if (r->xorbuff) {
		free(r->xorbuff);
	}
}
Beispiel #13
0
int main(int argc, char *argv[])
{
    int port = 5555;
    if(argc > 1)
        port = atoi(argv[1]);

    ipaddr  addr    = iplocal(NULL, port, 0);
    tcpsock ls      = tcplisten(addr, 10);
    if(!ls) 
    {
        perror("Can't open listening socket");
        return 1;
    } 

    while(1)
    {
        tcpsock as = tcpaccept(ls, -1);
        printf("New connection!\n");

        tcpsend(as, "What's your name?\r\n", 19, -1);
        tcpflush(as, -1);

        tcpclose(as);
    }

    return 0;
}
Beispiel #14
0
static void* masterproxy_acceptor(void *args) {
	pthread_t clientthread;
	pthread_attr_t thattr;
	int sock;
	(void)args;

	pthread_attr_init(&thattr);
	pthread_attr_setstacksize(&thattr,0x100000);
	pthread_attr_setdetachstate(&thattr,PTHREAD_CREATE_DETACHED);

	while (terminate==0) {
		sock = tcptoaccept(lsock,1000);
		if (sock>=0) {
			int *s = malloc(sizeof(int));
			// memory is freed inside pthread routine !!!
			*s = sock;
			tcpnodelay(sock);
			if (pthread_create(&clientthread,&thattr,masterproxy_server,s)<0) {
				free(s);
				tcpclose(sock);
			}
		}
	}

	pthread_attr_destroy(&thattr);
	return NULL;
}
Beispiel #15
0
void wsockclose(wsock s) {
    assert(s->u);
    tcpclose(s->u);
    wsock_str_term(&s->url);
    wsock_str_term(&s->subprotocol);
    free(s);
}
Beispiel #16
0
void masterconn_term(void) {
	packetstruct *pptr,*paptr;
//	syslog(LOG_INFO,"closing %s:%s",MasterHost,MasterPort);
	masterconn *eptr = masterconnsingleton;

	if (eptr->mode!=FREE && eptr->mode!=CONNECTING) {
		tcpclose(eptr->sock);

		if (eptr->inputpacket.packet) {
			free(eptr->inputpacket.packet);
		}
		pptr = eptr->outputhead;
		while (pptr) {
			if (pptr->packet) {
				free(pptr->packet);
			}
			paptr = pptr;
			pptr = pptr->next;
			free(paptr);
		}
	}

	free(eptr);
	masterconnsingleton = NULL;
}
Beispiel #17
0
int main(int argc, char *argv[]) {

    int port = 5555;
    if(argc > 1)
        port = atoi(argv[1]);

    tcpsock ls = tcplisten(iplocal(NULL, port, 0));
    if(!ls) {
        perror("Can't open listening socket");
        return 1;
    }

    while(1) {
        tcpsock as = tcpaccept(ls, -1);

        tcpsend(as, "What's your name?\r\n", 19, -1);
        tcpflush(as, -1);

        char inbuf[256];
        size_t sz = tcprecvuntil(as, inbuf, sizeof(inbuf), "\r", 1, -1);

        inbuf[sz - 1] = 0;
        char outbuf[256];
        int rc = snprintf(outbuf, sizeof(outbuf), "Hello, %s!\n", inbuf);

        sz = tcpsend(as, outbuf, rc, -1);
        tcpflush(as, -1);

        tcpclose(as);
    }
}
Beispiel #18
0
void masterconn_disconnection_check(void) {
    masterconn *eptr = masterconnsingleton;
    in_packetstruct *ipptr,*ipaptr;
    out_packetstruct *opptr,*opaptr;

    if (eptr->mode == KILL) {
        masterconn_beforeclose(eptr);
        tcpclose(eptr->sock);
        if (eptr->input_packet) {
            free(eptr->input_packet);
        }
        ipptr = eptr->inputhead;
        while (ipptr) {
            ipaptr = ipptr;
            ipptr = ipptr->next;
            free(ipaptr);
        }
        opptr = eptr->outputhead;
        while (opptr) {
            opaptr = opptr;
            opptr = opptr->next;
            free(opaptr);
        }
        eptr->mode = FREE;
    }
}
Beispiel #19
0
coroutine void client(int port) {
    ipaddr addr = ipremote("127.0.0.1", port, 0, -1);
    tcpsock cs = tcpconnect(addr, -1);
    assert(cs);

    char ipstr[16] = {0};
    ipaddrstr(addr, ipstr);
    assert(errno == 0);
    assert(strcmp(ipstr, "127.0.0.1") == 0);

    int fd = tcpdetach(cs);
    assert(fd != -1);
    cs = tcpattach(fd, 0);
    assert(cs);

    msleep(now() + 100);

    char buf[16];
    size_t sz = tcprecv(cs, buf, 3, -1);
    assert(sz == 3 && buf[0] == 'A' && buf[1] == 'B' && buf[2] == 'C');

    sz = tcpsend(cs, "123\n45\n6789", 11, -1);
    assert(sz == 11 && errno == 0);
    tcpflush(cs, -1);
    assert(errno == 0);

    tcpclose(cs);
}
coroutine void dialogue(tcpsock as) {
    int64_t deadline = now() + 10000;

    tcpsend(as, "What's your name?\r\n", 19, deadline);
    if(errno != 0)
        goto cleanup;
    tcpflush(as, deadline);
    if(errno != 0)
        goto cleanup;

    char inbuf[256];
    size_t sz = tcprecvuntil(as, inbuf, sizeof(inbuf), "\r", 1, deadline);
    if(errno != 0)
        goto cleanup;

    inbuf[sz - 1] = 0;
    char outbuf[256];
    int rc = snprintf(outbuf, sizeof(outbuf), "Hello, %s!\r\n", inbuf);

    sz = tcpsend(as, outbuf, rc, deadline);
    if(errno != 0)
        goto cleanup;
    tcpflush(as, deadline);
    if(errno != 0)
        goto cleanup;

    cleanup:
    tcpclose(as);
}
Beispiel #21
0
void masterconn_connecttimeout(masterconn *eptr) {
    syslog(LOG_WARNING,"connection timed out");
    tcpclose(eptr->sock);
    eptr->sock = -1;
    eptr->mode = FREE;
    eptr->masteraddrvalid = 0;
}
Beispiel #22
0
void masterconn_term(void) {
    masterconn *eptr = masterconnsingleton;
    in_packetstruct *ipptr,*ipaptr;
    out_packetstruct *opptr,*opaptr;

    if (eptr->mode!=FREE) {
        tcpclose(eptr->sock);
        if (eptr->mode!=CONNECTING) {
            if (eptr->input_packet) {
                free(eptr->input_packet);
            }
            ipptr = eptr->inputhead;
            while (ipptr) {
                ipaptr = ipptr;
                ipptr = ipptr->next;
                free(ipaptr);
            }
            opptr = eptr->outputhead;
            while (opptr) {
                opaptr = opptr;
                opptr = opptr->next;
                free(opaptr);
            }
        }
    }

    masterconn_read(NULL,0.0); // free internal read buffer

    free(eptr);

    free(MasterHost);
    free(MasterPort);
    free(BindHost);
    masterconnsingleton = NULL;
}
Beispiel #23
0
void matomlserv_term(void) {
	matomlserventry *eptr,*eaptr;
	packetstruct *pptr,*paptr;
	syslog(LOG_INFO,"master <-> metaloggers module: closing %s:%s",ListenHost,ListenPort);
	tcpclose(lsock);

	eptr = matomlservhead;
	while (eptr) {
		matomlserv_beforeclose(eptr);
		if (eptr->inputpacket.packet) {
			free(eptr->inputpacket.packet);
		}
		pptr = eptr->outputhead;
		while (pptr) {
			if (pptr->packet) {
				free(pptr->packet);
			}
			paptr = pptr;
			pptr = pptr->next;
			free(paptr);
		}
		eaptr = eptr;
		eptr = eptr->next;
		free(eaptr);
	}
	matomlservhead=NULL;

	free(ListenHost);
	free(ListenPort);
}
Beispiel #24
0
coroutine void dialogue(tcpsock as, chan ch) {
    chs(ch, int, CONN_ESTABLISHED);

    int64_t deadline = now() + 10000;

    tcpsend(as, "What's your name?\r\n", 19, deadline);
    if(errno != 0)
        goto cleanup;
    tcpflush(as, deadline);
    if(errno != 0)
        goto cleanup;

    char inbuf[256];
    size_t sz = tcprecvuntil(as, inbuf, sizeof(inbuf), "\r", 1, deadline);
    if(errno != 0)
        goto cleanup;

    inbuf[sz - 1] = 0;
    char outbuf[256];
    int rc = snprintf(outbuf, sizeof(outbuf), "Hello, %s!\r\n", inbuf);

    sz = tcpsend(as, outbuf, rc, deadline);
    if(errno != 0)
        goto cleanup;
    tcpflush(as, deadline);

    cleanup:
    if(errno == 0)
        chs(ch, int, CONN_SUCCEEDED);
    else
        chs(ch, int, CONN_FAILED);
    tcpclose(as);
}
Beispiel #25
0
void masterconn_term(void) {
    packetstruct *pptr,*paptr;
    masterconn *eptr = masterconnsingleton;

    if (eptr->mode!=FREE) {
        tcpclose(eptr->sock);
        if (eptr->mode!=CONNECTING) {
            if (eptr->inputpacket.packet) {
                free(eptr->inputpacket.packet);
            }
            pptr = eptr->outputhead;
            while (pptr) {
                if (pptr->packet) {
                    free(pptr->packet);
                }
                paptr = pptr;
                pptr = pptr->next;
                free(paptr);
            }
        }
    }

    free(eptr);
    free(MasterHost);
    free(MasterPort);
    free(BindHost);
    masterconnsingleton = NULL;
}
Beispiel #26
0
void csserv_reload(void) {
	char *oldListenHost,*oldListenPort;
	int newlsock;

//	ThreadedServer = 1-ThreadedServer;

	oldListenHost = ListenHost;
	oldListenPort = ListenPort;
	ListenHost = cfg_getstr("CSSERV_LISTEN_HOST","*");
	ListenPort = cfg_getstr("CSSERV_LISTEN_PORT",DEFAULT_CS_DATA_PORT);
	if (strcmp(oldListenHost,ListenHost)==0 && strcmp(oldListenPort,ListenPort)==0) {
		free(oldListenHost);
		free(oldListenPort);
		mfs_arg_syslog(LOG_NOTICE,"main server module: socket address hasn't changed (%s:%s)",ListenHost,ListenPort);
		return;
	}

	newlsock = tcpsocket();
	if (newlsock<0) {
		mfs_errlog(LOG_WARNING,"main server module: socket address has changed, but can't create new socket");
		free(ListenHost);
		free(ListenPort);
		ListenHost = oldListenHost;
		ListenPort = oldListenPort;
		return;
	}
	tcpnonblock(newlsock);
	tcpnodelay(newlsock);
	tcpreuseaddr(newlsock);
	if (tcpstrlisten(newlsock,ListenHost,ListenPort,100)<0) {
		mfs_arg_errlog(LOG_ERR,"main server module: socket address has changed, but can't listen on socket (%s:%s)",ListenHost,ListenPort);
		free(ListenHost);
		free(ListenPort);
		ListenHost = oldListenHost;
		ListenPort = oldListenPort;
		tcpclose(newlsock);
		return;
	}
	if (tcpsetacceptfilter(newlsock)<0 && errno!=ENOTSUP) {
		mfs_errlog_silent(LOG_NOTICE,"main server module: can't set accept filter");
	}
	mfs_arg_syslog(LOG_NOTICE,"main server module: socket address has changed, now listen on %s:%s",ListenHost,ListenPort);
	free(oldListenHost);
	free(oldListenPort);
	tcpclose(lsock);
	lsock = newlsock;
}
Beispiel #27
0
int mainserv_connect(uint32_t fwdip,uint16_t fwdport,uint32_t timeout) {
	int fwdsock;
	fwdsock = tcpsocket();
	if (fwdsock<0) {
		mfs_errlog(LOG_WARNING,"create socket, error");
		return -1;
	}
	if (tcpnonblock(fwdsock)<0) {
		mfs_errlog(LOG_WARNING,"set nonblock, error");
		tcpclose(fwdsock);
		return -1;
	}
	if (tcpnumtoconnect(fwdsock,fwdip,fwdport,timeout)<0) {
		mfs_arg_errlog(LOG_WARNING,"connect to %u.%u.%u.%u:%u failed, error",(fwdip>>24)&0xFF,(fwdip>>16)&0xFF,(fwdip>>8)&0xFF,fwdip&0xFF,fwdport);
		tcpclose(fwdsock);
		return -1;
	}
Beispiel #28
0
int client()
{
    int cs = client_socket("127.0.0.1", "9991");
    tcptowrite(cs, "helloworl", 10, 1000);
    /*printf("write ok %d \n", n); */
    tcpclose(cs);
    return 0;
}
void
nsldapi_close_connection( LDAP *ld, Sockbuf *sb )
{
	if ( ld->ld_close_fn == NULL ) {
		tcpclose( (tcpstream *)sb->sb_sd );
	} else {
		ld->ld_close_fn( sb->sb_sd );
	}
}
Beispiel #30
0
int main() {
    char buf[16];

    tcpsock ls = tcplisten(iplocal(NULL, 5555, 0), 10);
    assert(ls);

    int fd = tcpdetach(ls);
    assert(fd != -1);
    ls = tcpattach(fd, 1);
    assert(ls);
    assert(tcpport(ls) == 5555);

    go(client(5555));

    tcpsock as = tcpaccept(ls, -1);

    /* Test deadline. */
    int64_t deadline = now() + 30;
    size_t sz = tcprecv(as, buf, sizeof(buf), deadline);
    assert(sz == 0 && errno == ETIMEDOUT);
    int64_t diff = now() - deadline;
    assert(diff > -20 && diff < 20); 

    sz = tcpsend(as, "ABC", 3, -1);
    assert(sz == 3 && errno == 0);
    tcpflush(as, -1);
    assert(errno == 0);

    sz = tcprecvuntil(as, buf, sizeof(buf), "\n", 1, -1);
    assert(sz == 4);
    assert(buf[0] == '1' && buf[1] == '2' && buf[2] == '3' && buf[3] == '\n');
    sz = tcprecvuntil(as, buf, sizeof(buf), "\n", 1, -1);
    assert(sz == 3);
    assert(buf[0] == '4' && buf[1] == '5' && buf[2] == '\n');
    sz = tcprecvuntil(as, buf, 3, "\n", 1, -1);
    assert(sz == 3);
    assert(buf[0] == '6' && buf[1] == '7' && buf[2] == '8');

    tcpclose(as);
    tcpclose(ls);

    return 0;
}