/*Sleep on given read socket until msec or readable*/ int skt_select1(SOCKET fd,int msec) { int sec=msec/1000; fd_set rfds; struct timeval tmo, *tmp=&tmo; int secLeft=sec; int begin=0, nreadable; if (!skt_inited) skt_init(); FD_ZERO(&rfds); FD_SET(fd, &rfds); if (msec>0) begin = time(0); else /* msec zero-- disable timeout */ tmp=NULL; do { tmo.tv_sec=secLeft; tmo.tv_usec = (msec-1000*sec)*1000; skt_ignore_SIGPIPE=1; nreadable = select(1+fd, &rfds, NULL, NULL, tmp); skt_ignore_SIGPIPE=0; if (nreadable < 0) { if (skt_should_retry()) continue; else return skt_abort(93200,"Fatal error in select"); } if (nreadable >0) return 1; /*We gotta good socket*/ } while(msec>0 && ((secLeft = sec - (time(0) - begin))>0)); return 0;/*Timed out*/ }
struct sockaddr_in skt_build_addr(skt_ip_t IP,int port) { struct sockaddr_in ret={0}; if (!skt_inited) skt_init(); /* this works for datagram, server, and connect, too! */ ret.sin_family=AF_INET; ret.sin_port = htons((short)port); memcpy(&ret.sin_addr,&IP,sizeof(IP)); return ret; }
skt_ip_t skt_my_ip(void) { char hostname[1000]; if (!skt_inited) skt_init(); if (gethostname(hostname, 999)==0) return skt_lookup_ip(hostname); return _skt_invalid_ip; }
skt_ip_t skt_lookup_invalid(const char *name) { skt_ip_t ret=_skt_invalid_ip; if (!skt_inited) skt_init(); /*First try to parse the name as dotted decimal*/ if (skt_parse_dotted(name,&ret)) return ret; else {/*Try a DNS lookup*/ struct hostent *h = gethostbyname(name); if (h==0) return _skt_invalid_ip; memcpy(&ret,h->h_addr_list[0],h->h_length); return ret; } }
/*Make a new Ccs Server socket, on the given port. Returns the actual port and IP address. */ void CcsServer_new(skt_ip_t *ret_ip,int *use_port,const char *authFile) { char ip_str[200]; skt_ip_t ip; unsigned int port=0;if (use_port!=NULL) port=*use_port; CCS_AUTH_new(&ccs_clientlist); security=CcsSecMan_default(authFile); skt_init(); ip=skt_my_ip(); ccs_server_fd=skt_server(&port); printf("ccs: %s\nccs: Server IP = %s, Server port = %u $\n", CMK_CCS_VERSION, skt_print_ip(ip_str,ip), port); fflush(stdout); if (ret_ip!=NULL) *ret_ip=ip; if (use_port!=NULL) *use_port=port; }