hot_err_t ens_tcp_init(void *env, char * argv[], /*OUT*/ int *fd) { int result ; int sinlen ; struct hostent *hep ; char my_host[64]; struct sockaddr_in sin; trace("ens_tcp_init"); /* Get my host name in preparation for connect */ result = gethostname(my_host, 64); if (result < 0) { hot_sys_Panic("gethostname"); } /* Use my host name to get hostent in preparation for connect */ memset(&sin, 0, sizeof(sin)) ; hep = gethostbyname(my_host); if (hep == (struct hostent *)NULL) { // try localhost inet_aton("127.0.0.1",&(sin.sin_addr)); //hot_sys_Panic("gethostbyname"); } else { /* Copy hostent's addr into sin_addr. */ memcpy(&(sin.sin_addr),hep->h_addr,hep->h_length); } sin.sin_family = AF_INET; sin.sin_port = ntohs(OUTBOARD_TCP_PORT); sinlen = sizeof(sin); trace("TCP connect to %s port %d", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); fflush(stdout); /* Now call connect in a loop, waiting for accept */ { int sock ; /* Create our socket. */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { hot_sys_Panic("socket"); } result = connect(sock, (struct sockaddr *)&sin, sinlen) ; if (result == -1) { Perror("connect") ; hot_sys_Panic("connect") ; } *fd = (int)sock; } trace("TCP connected"); return HOT_OK; }
void gen_tmp_file(const char *filename) { int fd; char ch = 'a'; /*create a file, and fill it with alph table*/ if((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, FILE_MODE)) == -1) { Perror("open error"); } for(int i=0;i<26;i++) { ch = ch+i; if(write(fd, &ch, 1) != 1) { Perror("write error"); } } if(close(fd) == -1) { Perror("close error"); } }
void CcpNgCtrlEvent(int type, void *cookie) { Bund b = NULL; union { u_char buf[2048]; struct ng_mesg msg; } u; char raddr[NG_PATHSIZ]; int i, len; ng_ID_t id; /* Read message */ if ((len = NgRecvMsg(gCcpCsock, &u.msg, sizeof(u), raddr)) < 0) { Perror("CcpNgCtrlEvent: can't read message"); return; } if (sscanf(raddr, "[%x]:", &id) != 1) { Log(LG_ERR, ("CcpNgCtrlEvent: can't decode sender id: '%s'", raddr)); return; } for (i = 0; i < gNumBundles; i++) { if (gBundles[i] && !gBundles[i]->dead && gBundles[i]->ccp.decomp_node_id == id) { b = gBundles[i]; break; } } if (!b) return; /* Examine message */ switch (u.msg.header.typecookie) { #ifdef USE_NG_MPPC case NGM_MPPC_COOKIE: #endif #ifdef USE_NG_DEFLATE case NGM_DEFLATE_COOKIE: #endif #ifdef USE_NG_PRED1 case NGM_PRED1_COOKIE: #endif CcpRecvMsg(b, &u.msg, len); return; default: /* Unknown message */ Log(LG_ERR, ("CcpNgCtrlEvent: rec'd unknown ctrl message, cookie=%d cmd=%d", u.msg.header.typecookie, u.msg.header.cmd)); break; } }
void setifcap(const char *vname, int value, int s, const struct afswtch *afp) { if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) { Perror("ioctl (SIOCGIFCAP)"); exit(1); } flags = ifr.ifr_curcap; if (value < 0) { value = -value; flags &= ~value; } else flags |= value; ifr.ifr_reqcap = flags; if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0) Perror(vname); }
void do_lock(const char *filename) { int fd; char ch = 'z'; /* test read lock */ if((fd = open(filename, O_RDONLY, NULL)) < 0) { Perror("open error"); } if(read_lock(fd, 0, SEEK_SET, 1) == -1) { Perror("read_lock error"); } else { printf("Process %d set read_lock successfully\n", getpid()); } if(read(fd, &ch, 1) != 1) { Perror("read error"); } else { printf("Process %d read %c from file\n", getpid(), ch); } if(close(fd) == -1) { Perror("close error"); } /*test write lock*/ ch = 'z'; if((fd = open(filename, O_WRONLY, NULL)) < 0) { Perror("open error"); } do { if(write_lock(fd,1,SEEK_SET,1) == -1) { if(errno != EAGAIN) { Perror("write_lock error"); } } else { printf("Process %d set write_lock successfully\n", getpid()); break; } }while(1); if(write(fd, &ch, 1) != 1) { Perror("write error"); } sleep(2); if(un_lock(fd,1,SEEK_SET,1) == -1) { Perror("un_lock error"); } else { printf("Process %d unlock successfully\n", getpid()); } if(close(fd) == -1) { Perror("close error"); } }
int OpenSerialDevice(const char *label, const char *path, int baudrate) { struct termios attr; int fd; /* Open & lock serial port */ if ((fd = ExclusiveOpenDevice(label, path)) < 0) return (-1); /* Set non-blocking I/O */ if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { Perror("[%s] can't set \"%s\" to non-blocking", label, path); goto failed; } /* Set serial port raw mode, baud rate, hardware flow control, etc. */ if (tcgetattr(fd, &attr) < 0) { Perror("[%s] can't tcgetattr \"%s\"", label, path); goto failed; } cfmakeraw(&attr); attr.c_cflag &= ~(CSIZE | PARENB | PARODD); attr.c_cflag |= (CS8 | CREAD | CLOCAL | HUPCL | CCTS_OFLOW | CRTS_IFLOW); attr.c_iflag &= ~(IXANY | IMAXBEL | ISTRIP | IXON | IXOFF | BRKINT | ICRNL | INLCR); attr.c_iflag |= (IGNBRK | IGNPAR); attr.c_oflag &= ~OPOST; attr.c_lflag = 0; cfsetspeed(&attr, (speed_t)baudrate); if (tcsetattr(fd, TCSANOW, &attr) < 0) { Perror("[%s] can't tcsetattr \"%s\"", label, path); failed: ExclusiveCloseDevice(label, fd, path); return (-1); } /* OK */ return (fd); }
inline void close(){ switch(current_file){ case FILE_MODE_ERR:{ return;} case FILE_MODE_RAW:{ closeFile(file1); break;} case FILE_MODE_BZ2:{ closeFile(file2); break;} default:{ Perror("Unown type"); } } //printf("%d\n", current_file); current_file = FILE_MODE_ERR; }
void test() { pid_t pid; if((pid = fork()) < 0) { Perror("fork error"); } else if(pid == 0) { do_lock("lock.dat"); } else { sleep(2); do_lock("lock.dat"); } }
void setifflags(char *vname, int value) { if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { Perror("ioctl (SIOCGIFFLAGS)"); exit(1); } strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16); if (value < 0) { value = -value; flags &= ~value; } else flags |= value; ifr.ifr_flags = flags & 0xffff; ifr.ifr_flagshigh = flags >> 16; if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) Perror(vname); }
int TcpAcceptConnection(int sock, struct sockaddr_storage *addr, int block) { int new_sock; socklen_t size = sizeof(struct sockaddr_storage); /* Accept incoming connection */ memset(addr, 0, sizeof(*addr)); if ((new_sock = accept(sock, (struct sockaddr *)addr, &size)) < 0) { Perror("%s: accept", __FUNCTION__); return (-1); } #ifdef USE_WRAP if (Enabled(&gGlobalConf.options, GLOBAL_CONF_TCPWRAPPER)) { struct request_info req; request_init(&req, RQ_DAEMON, "mpd", RQ_FILE, new_sock, NULL); fromhost(&req); if (!hosts_access(&req)) { Log(LG_ERR, ("refused connection (tcp-wrapper) from %s", eval_client(&req))); close(new_sock); return (-1); } } #endif if (!block) { (void)fcntl(new_sock, F_SETFD, 1); if (fcntl(new_sock, F_SETFL, O_NONBLOCK) < 0) { Perror("%s: fcntl", __FUNCTION__); return (-1); } } /* Done */ return (new_sock); }
HttpCache::HttpCache(const std::string& directory) { struct stat s; d_directory = directory; int err = stat(d_directory.c_str(), &s); if(-1 == err) { if(ENOENT == errno) { mkdir(d_directory.c_str() , 0766); } else { Perror("stat", errno); } } }
/* * Given a keyboard layout number return the layout string. * We should not be doing this here, but unfortunately there * is no interface in the OpenSolaris keyboard API to perform * this mapping for us - RFE. */ static char * get_layout_name(int lnum) { FILE *stream; char buffer[MAX_LINE_SIZE]; char *result = NULL; int num; char *tmpbuf; if ((stream = fopen(KBD_LAYOUT_FILE, "r")) == 0) { Perror(KBD_LAYOUT_FILE); return (NULL); } while (fgets(buffer, MAX_LINE_SIZE, stream) != NULL) { if (buffer[0] == '#') continue; if ((result = strtok(buffer, "=")) == NULL) continue; if ((tmpbuf = strdup(result)) == NULL) { Perror("out of memory getting layout names"); (void) fclose(stream); return (NULL); } if ((result = strtok(NULL, "\n")) == NULL) continue; num = atoi(result); if (num == lnum) { break; } free(tmpbuf); } (void) fclose(stream); return (tmpbuf); }
/* * Note: doing an SIOCIGIFFLAGS scribbles on the union portion * of the ifreq structure, which may confuse other parts of ifconfig. * Make a private copy so we can avoid that. */ static void setifflags(const char *vname, int value, int s, const struct afswtch *afp) { struct ifreq my_ifr; bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq)); if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) { Perror("ioctl (SIOCGIFFLAGS)"); exit(1); } strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name)); flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16); if (value < 0) { value = -value; flags &= ~value; } else flags |= value; my_ifr.ifr_flags = flags & 0xffff; my_ifr.ifr_flagshigh = flags >> 16; if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0) Perror(vname); }
static int tcp_send(int s, void *buf_void, int len) { int result, ofs; char *buf = buf_void ; trace("tcpsend: %d 0x%x %d",s,buf,len); for (ofs=0 ; ofs < len ; ofs += result) { result = send(s, buf+ofs, len-ofs, 0); if (result == -1) { #ifdef EINTR if (errno == EINTR) continue ; #endif Perror("send"); hot_sys_Panic("lost connection to outboard") ; } } return len; }
int ExclusiveOpenDevice(const char *label, const char *pathname) { int fd, locked = FALSE; const char *ttyname = NULL; time_t startTime; /* Lock device UUCP style, if it resides in /dev */ if (!strncmp(pathname, _PATH_DEV, 5)) { int res; ttyname = pathname + 5; if ((res = uu_lock(ttyname)) != UU_LOCK_OK) { Log(LG_ERR, ("[%s] uu_lock(%s): %s", label, ttyname, uu_lockerr(res))); return (-1); } locked = TRUE; } /* Open it, but give up after so many interruptions */ for (startTime = time(NULL); (fd = open(pathname, O_RDWR, 0)) < 0 && time(NULL) < startTime + MAX_OPEN_DELAY;) if (errno != EINTR) { Perror("[%s] can't open %s", label, pathname); if (locked) uu_unlock(ttyname); return (-1); } /* Did we succeed? */ if (fd < 0) { Log(LG_ERR, ("[%s] can't open %s after %d secs", label, pathname, MAX_OPEN_DELAY)); if (locked) uu_unlock(ttyname); return (-1); } (void)fcntl(fd, F_SETFD, 1); /* Done */ return (fd); }
int IpShowRoutes(Context ctx, int ac, char *av[], void *arg) { FILE *fp; char buf[256]; char *c; if ((fp = popen(PATH_NETSTAT " -nr -f inet", "r")) == NULL) { Perror("popen"); return (CMD_ERR_OTHER); } while (fgets(buf, sizeof(buf) - 1, fp)) { if ((c = strrchr(buf, '\n'))) *c = '\r'; Printf("%s\n", buf); } pclose(fp); return (0); }
/* should return 0 on success, 1 on detected virus, -1 on error */ int check_data( struct mem_chunk *root ) { struct mem_chunk *next = root; char *buf, *p; size_t total = 0; int rc = 0; if ( no_check ) return 0; while ( next != NULL && next->b != NULL ) { total += next->size + 2; next = next->next; } debug( "total data for check: %d", total ); if ( total == 0 ) { error("check_data: zero data"); return 0; } buf = malloc( total ); if ( buf == NULL ) { Perror( "malloc"); return 0; } p = buf; next = root; while ( next != NULL && next->b != NULL ) { strncpy( p, next->b, next->size ); p += next->size; strncpy( p, "\r\n", 2); p += 2; next = next->next; } rc = dw_scan( buf, total ); free( buf ); return rc; }
int CcpsInit(void) { char name[NG_NODESIZ]; /* Create a netgraph socket node */ snprintf(name, sizeof(name), "mpd%d-cso", gPid); if (NgMkSockNode(name, &gCcpCsock, &gCcpDsock) < 0) { Perror("CcpsInit(): can't create %s node", NG_SOCKET_NODE_TYPE); return(-1); } (void) fcntl(gCcpCsock, F_SETFD, 1); (void) fcntl(gCcpDsock, F_SETFD, 1); /* Listen for happenings on our node */ EventRegister(&gCcpCtrlEvent, EVENT_READ, gCcpCsock, EVENT_RECURRING, CcpNgCtrlEvent, NULL); EventRegister(&gCcpDataEvent, EVENT_READ, gCcpDsock, EVENT_RECURRING, CcpNgDataEvent, NULL); return (0); }
FILE * OpenConfFile(const char *name, struct configfile **cf) { char pathname[MAX_FILENAME]; FILE *fp; struct configfiles **tmp; /* Build full pathname */ if (name[0] == '/') snprintf(pathname, sizeof(pathname), "%s", name); else snprintf(pathname, sizeof(pathname), "%s/%s", gConfDirectory, name); /* Open file */ if ((fp = fopen(pathname, "r")) == NULL) { Perror("%s: Can't open file '%s'", __FUNCTION__, pathname); return (NULL); } (void)fcntl(fileno(fp), F_SETFD, 1); if (cf) { tmp = &ConfigFilesIndex; while ((*tmp) && strcmp((*tmp)->filename, name)) { tmp = &((*tmp)->next); } if (!(*tmp)) { (*tmp) = Malloc(MB_CMD, sizeof(struct configfiles)); (*tmp)->filename = strcpy(Malloc(MB_CMD, strlen(name) + 1), name); (*tmp)->sections = NULL; (*tmp)->next = NULL; IndexConfFile(fp, &((*tmp)->sections)); } *cf = (*tmp)->sections; } return (fp); }
int sock_write( int s, void *buf, size_t len, int timeout ) { int rc; #ifdef HAVE_POLL struct pollfd pfd; pfd.fd = s; pfd.events = POLLOUT ; pfd.revents = 0; rc = poll( &pfd, 1, timeout ); if ( rc == -1 ) { Perror("sock_write: poll"); return -1; } if ( rc == 0 ) { errno = ETIMEDOUT; error("sock_write: timeout"); return -1; } if ( (pfd.revents & POLLOUT) == 0 ) { error("sock_write: no POLLOUT in revents"); return 0; } #else #ifdef HAVE_SELECT fd_set wrevents; struct timeval tv; if ( FD_SETSIZE <= s ) { error("descriptor %d does not fit FD_SETSIZE %d", s, FD_SETSIZE); return -1; } FD_ZERO ( &wrevents ); FD_SET ( s, &wrevents ); tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout % 1000) * 1000; while ( (rc = select( s + 1, NULL, &wrevents, NULL, &tv)) <= 0 ) { if ( rc == EINTR ) continue; if ( rc == -1 ) { Perror("select"); return -1; } if ( rc == 0 ) { errno = ETIMEDOUT; error("sock_write: timeout writing data"); return -1; } } /* if ( FD_ISSET( s, &exevents ) ) { error("sock_write: OOB data received"); return -1; } */ #endif #endif rc = send( s, buf, len, 0); return rc; }
int sock_connect( const char *addr) { struct sockaddr peer; struct sockaddr *ppeer; int s; #if HAVE_SELECT fd_set rdevents, wrevents, exevents; struct timeval tv; int flags; int rc; #endif bzero( &peer, sizeof (peer)); if ( (ppeer = parse_addr( addr )) == NULL ) { return -1; } memcpy( &peer, ppeer, sizeof(peer) ); s = socket( AF_INET, SOCK_STREAM, 0); if ( s < 0 ) { Perror("socket"); return -1; } #ifdef HAVE_SELECT if ( (flags = fcntl(s, F_GETFL, 0)) < 0) { Perror("fcntl(s, F_GETFL, 0)"); return -1; } if ( (flags = fcntl(s, F_SETFL, O_NONBLOCK)) < 0) { Perror("fcntl(s, F_SETFL, O_NONBLOCK)"); return -1; } if ( (rc = connect(s, &peer, sizeof (peer))) && errno != EINPROGRESS ) { #else if ( connect(s, &peer, sizeof (peer)) ) { #endif Perror("connect"); return -1; } #ifdef HAVE_SELECT if ( rc == 0 ) { if ( fcntl(s, F_SETFL, flags) < 0 ) { Perror("fcntl"); return -1; } debug("connected to %s", addr ); return s; } FD_ZERO ( &rdevents ); FD_SET ( s, &rdevents ); wrevents = exevents = rdevents; tv.tv_sec = 5; tv.tv_usec = 0; rc = select( s + 1, &rdevents, &wrevents, &exevents, &tv); if ( rc < 0 ) { Perror("select"); return -1; } else if ( rc == 0 ) { error("timeout connecting to %s", addr ); return -1; } else if ( isconnected(s, &rdevents, &wrevents, &exevents ) ) { if ( fcntl(s, F_SETFL, flags) < 0 ) { Perror("fcntl"); return -1; } debug("connected to %s", addr ); return s; } else { error("connection to %s failed", addr ); return -1; } #else debug("connected to %s", addr ); #endif return s; } int sock_listen( const char *addr ) { int s; const int on = 1; struct sockaddr_in local; struct sockaddr *plocal; bzero( &local, sizeof (local)); if ( (plocal = parse_addr(addr)) == NULL ) { return -1; } memcpy( &local, plocal, sizeof(local) ); s = socket( AF_INET, SOCK_STREAM, 0 ); if ( s < 0 ) { Perror("socket"); return -1; } if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) { Perror("setsockopt"); return -1; } if ( bind( s, (struct sockaddr *)&local, sizeof(local) )) { Perror("bind"); return -1; } if ( listen( s, 1 ) ) { Perror("listen"); return -1; } notice("listening on %s", addr ); return s; }
char *smtp_get( vsock_t *vsock ) { char *buf = NULL; char *eolptr = NULL; char *bufptr = NULL; char *nextptr = NULL; char lastchar = NULL; size_t len = 0; do { if ( vsock->icnt == 0 ) { if ( vsock_fill( vsock ) <= 0 ) { Perror("vsock_fill"); if ( buf != NULL) free( buf ); return NULL; } } if ( vsock->icnt == 0 ) error(""); /* XXX should truncate memory later when CRLF found ? */ if ( buf == NULL ) { debug("buf = malloc(%d)", len + vsock->icnt + 1 ); buf = malloc( len + vsock->icnt + 1); } else { debug("buf = realloc(%d)", len + vsock->icnt + 1 ); buf = realloc( buf, len + vsock->icnt + 1); } if ( buf == NULL ) { Perror("realloc"); return NULL; } bufptr = buf + len; memcpy( bufptr , vsock->iptr, vsock->icnt ); if ( *bufptr == '\n' && lastchar == '\r' ) { debug("lastchar was '\\r' and next char = '\\n'"); len += 2; vsock->icnt -= 1; vsock->iptr += 1; buf[len] = '\0'; return buf; } nextptr = bufptr; while( (eolptr = memchr( nextptr, '\r', vsock->icnt - (nextptr - bufptr) )) != NULL ) { debug("found '\\r' symbol"); if ( ((size_t)(eolptr - bufptr + 1)) < vsock->icnt ) { if ( (*(eolptr+1) == '\n') ) { debug("found '\\n' symbol"); len += eolptr - bufptr; debug("read %d chars", eolptr - bufptr + 2); vsock->icnt -= eolptr - bufptr + 2; debug("%d chars left in buffer", vsock->icnt ); buf = realloc( buf, len + 1); debug("result string have %d chars", len); buf[len] = '\0'; vsock->iptr += eolptr - bufptr + 2; debug("all ok, ret ahead"); return buf; } nextptr = eolptr + 1; } else { debug("last char is '\\r'"); eolptr = NULL; lastchar = '\r'; /* found CR, but it last char in buf*/ break; /* should check next buffer */ } } /* no CR LF found */ len += vsock->icnt; vsock->icnt = 0; } while( eolptr == NULL ); error("never should be here"); return NULL; }
int sock_read( int s, void *buf, size_t len, int timeout ) { int rc; #ifdef HAVE_POLL struct pollfd pfd; pfd.fd = s; pfd.events = POLLIN ; pfd.revents = 0; rc = poll( &pfd, 1, timeout ); if ( rc == -1 ) { Perror("sock_read: poll"); return -1; } if ( rc == 0 ) { errno = ETIMEDOUT; error("sock_read: timeout"); return -1; } if ( (pfd.revents & POLLIN) == 0 ) { error("sock_read: no POLLIN in revents"); return 0; } #else #ifdef HAVE_SELECT fd_set rdevents, exevents; struct timeval tv; if ( FD_SETSIZE <= s ) { error("descriptor %d does not fit FD_SETSIZE %d", s, FD_SETSIZE); return -1; } FD_ZERO ( &rdevents ); FD_SET ( s, &rdevents ); exevents = rdevents; tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout % 1000) * 1000; rc = select( s + 1, &rdevents, NULL, &exevents, &tv); if ( rc == -1 ) { Perror("select"); return -1; } if ( rc == 0 ) { error("sock_read: timeout reading data"); errno = ETIMEDOUT; return -1; } if ( FD_ISSET( s, &exevents ) ) { error("sock_read: OOB data received"); return -1; } #endif #endif rc = recv( s, buf, len , 0); return rc; }
int main(int argc, char* argv[]) { struct sockaddr_in addr; memset(&addr,0,sizeof(addr)); addr.sin_family = AF_INET; if(argc==1) { addr.sin_addr.s_addr = htonl(INADDR_ANY); } else { inet_pton(AF_INET, argv[1], &addr.sin_addr); } addr.sin_port = htons(12200); int listenfd = Socket(AF_INET, SOCK_STREAM, 0); Bind(listenfd, (struct sockaddr *)&addr, sizeof(addr)); struct pollfd conns[MAXFD]; for(int i=0;i<MAXFD;++i) conns[i].fd = -1; conns[0].fd = listenfd; conns[0].events = POLLIN; int conns_end = 1; Listen(listenfd,20); while(1) { int nready = Poll(conns,MAXFD,-1); if(conns[0].revents & POLLIN) { //new connection int pos; for(pos=0;pos<conns_end;++pos) { if(conns[pos].fd==-1) { break; } } if(pos==conns_end&&conns_end==MAXFD) { fprintf(stderr,"too many connections.\n"); exit(1); } if(pos==conns_end) { ++conns_end; } conns[pos].fd = Accept(listenfd,NULL,NULL); conns[pos].events = POLLIN; --nready; } for(int i=1;i<conns_end && nready>0 ;++i) { //client sockets are ready if( conns[i].fd!=-1 && (conns[i].revents & (POLLIN|POLL_ERR)) ) { int buf[MAXLINE]; int n = read(conns[i].fd, buf, MAXLINE); if(n<0) { if(errno==ECONNRESET) {//connect RESET by client Close(conns[i].fd); conns[i].fd = -1; if(i==conns_end-1) --conns_end; } else { Perror("read error"); } } if(n==0) { //connect close by client Close(conns[i].fd); conns[i].fd = -1; if(i==conns_end-1) --conns_end; } if(n>0) { //normal data Writen(conns[i].fd,buf,n); } --nready; } } } return 0; }
int TermFork (PCONINFO con, PCHILDINFO pChildInfo) { char ls, ln; // 1. Looking for unused PTY/TTY Master/Slave /* Open PTY(master) from [pqrs][5-F], in fact p-z is ok? */ /* MasterPty: pty[p-z][5-f] pty[a-e][5-f] 16*16=256 SlaveTty: tty[p-z][5-f] tty[a-e][5-f] 16*16=256 */ for (ls = 'p'; ls <= 's'; ls ++) { for (ln = 5; ln <= 0xF; ln ++) { sprintf(con->ptyName, "/dev/pty%1c%1x", ls, ln); if ((con->masterPty = open (con->ptyName, O_RDWR)) >= 0) break; } if (con->masterPty >= 0) break; } if (con->masterPty < 0) { myMessage ("can not get master pty!\n"); Perror (con->ptyName); return -1; } con->ptyName[5] = 't'; /* slave tty */ if ((con->childPid = fork()) < 0) { Perror ("fork"); return -1; } else if (con->childPid == 0) { // in child process int errfd, slavePty; FILE *errfp; struct winsize twinsz; errfd = dup (2); errfp = fdopen (errfd, "w"); /* I'm child, make me process leader */ setsid (); // close any no used fd here!! close (con->masterPty); /* Open TTY(slave) */ if ((slavePty = open (con->ptyName, O_RDWR)) < 0) PerrorExit (con->ptyName); /* Set new TTY's termio with parent's termio */ tcsetattr (slavePty, TCSANOW, (struct termios*)GetOriginalTermIO ()); /* Set new terminal window size */ twinsz.ws_row = con->rows; twinsz.ws_col = con->cols; ioctl (slavePty, TIOCSWINSZ, &twinsz); /* Set std??? to pty, dup2 (oldfd, newfd) */ dup2 (slavePty, 0); dup2 (slavePty, 1); dup2 (slavePty, 2); close (slavePty); // execute the shell ChildStart (con, errfp, pChildInfo->startupMessage, pChildInfo->startupStr, pChildInfo->execProg, pChildInfo->execArgs); } return 1; // parent process }
/* * chgrpr() - recursive chown() * * Recursively chowns the input directory then its contents. rflag must * have been set if chgrpr() is called. The input directory should not * be a sym link (this is handled in the calling routine). In * addition, the calling routine should have already added the input * directory to the search tree so we do not get into endless loops. * Note: chgrpr() doesn't need a return value as errors are reported * through the global "status" variable. */ static void chgrpr(char *dir, gid_t gid) { struct dirent *dp; DIR *dirp; struct stat st, st2; char savedir[1024]; if (getcwd(savedir, 1024) == 0) { (void) fprintf(stderr, "chgrp: "); (void) fprintf(stderr, gettext("%s\n"), savedir); exit(255); } /* * Attempt to chown the directory, however don't return if we * can't as we still may be able to chown the contents of the * directory. Note: the calling routine resets the SUID bits * on this directory so we don't have to perform an extra 'stat'. */ CHOWN(dir, -1, gid); if (chdir(dir) < 0) { status += Perror(dir); return; } if ((dirp = opendir(".")) == NULL) { status += Perror(dir); return; } for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..") == 0)) { continue; /* skip "." and ".." */ } if (lstat(dp->d_name, &st) < 0) { status += Perror(dp->d_name); continue; } if ((st.st_mode & S_IFMT) == S_IFLNK) { if (hflag || Pflag) { /* * Change the group id of the symbolic link * encountered while traversing the * directory. Don't follow the symbolic * link to any other part of the file * hierarchy. */ LCHOWN(dp->d_name, -1, gid); } else { if (stat(dp->d_name, &st2) < 0) { status += Perror(dp->d_name); continue; } /* * We know that we are to change the * group of the file referenced by the * symlink encountered while traversing * the directory. Now check to see if we * are to follow the symlink to any other * part of the file hierarchy. */ if (FOLLOW_D_LINKS) { if ((st2.st_mode & S_IFMT) == S_IFDIR) { /* * We are following symlinks so * traverse into the directory. * Add this node to the search * tree so we don't get into an * endless loop. */ int rc; if ((rc = add_tnode(&tree, st2.st_dev, st2.st_ino)) == 1) { chgrpr(dp->d_name, gid); /* * Restore SET[UG]ID * bits. */ SETUGID_PRESERVE( dp->d_name, st2.st_mode & ~S_IFMT); } else if (rc == 0) { /* already visited */ continue; } else { /* * An error occurred * while trying to add * the node to the tree. */ status += Perror( dp->d_name); continue; } } else { /* * Change the group id of the * file referenced by the * symbolic link. */ CHOWN(dp->d_name, -1, gid); } } else { /* * Change the group id of the file * referenced by the symbolic link. */ CHOWN(dp->d_name, -1, gid); if ((st2.st_mode & S_IFMT) == S_IFDIR) { /* Restore SET[UG]ID bits. */ SETUGID_PRESERVE(dp->d_name, st2.st_mode & ~S_IFMT); } } } } else if ((st.st_mode & S_IFMT) == S_IFDIR) { /* * Add this node to the search tree so we don't * get into a endless loop. */ int rc; if ((rc = add_tnode(&tree, st.st_dev, st.st_ino)) == 1) { chgrpr(dp->d_name, gid); /* Restore the SET[UG]ID bits. */ SETUGID_PRESERVE(dp->d_name, st.st_mode & ~S_IFMT); } else if (rc == 0) { /* already visited */ continue; } else { /* * An error occurred while trying * to add the node to the search tree. */ status += Perror(dp->d_name); continue; } } else { CHOWN(dp->d_name, -1, gid); } } (void) closedir(dirp); if (chdir(savedir) < 0) { (void) fprintf(stderr, "chgrp: "); (void) fprintf(stderr, gettext("can't change back to %s\n"), savedir); exit(255); } }
int main(int argc, char *argv[]) { int c; /* set the locale for only the messages system (all else is clean) */ (void) setlocale(LC_ALL, ""); #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ #endif (void) textdomain(TEXT_DOMAIN); while ((c = getopt(argc, argv, "RhfHLPs")) != EOF) switch (c) { case 'R': rflag++; break; case 'h': hflag++; break; case 'f': fflag++; break; case 'H': /* * If more than one of -H, -L, and -P * are specified, only the last option * specified determines the behavior of * chgrp. In addition, make [-H|-L] * mutually exclusive of -h. */ Lflag = Pflag = 0; Hflag++; break; case 'L': Hflag = Pflag = 0; Lflag++; break; case 'P': Hflag = Lflag = 0; Pflag++; break; case 's': sflag++; break; default: usage(); } /* * Set Pflag by default for recursive operations * if no other options were specified. */ if (rflag && !(Lflag || Hflag || Pflag || hflag)) { Pflag = 1; } /* * Check for sufficient arguments * or a usage error. */ argc -= optind; argv = &argv[optind]; if ((argc < 2) || ((Hflag || Lflag || Pflag) && !rflag) || ((Hflag || Lflag || Pflag) && hflag)) { usage(); } if (sflag) { if (sid_to_id(argv[0], B_FALSE, &gid)) { (void) fprintf(stderr, gettext( "chgrp: invalid group sid %s\n"), argv[0]); exit(2); } } else if ((gr = getgrnam(argv[0])) != NULL) { gid = gr->gr_gid; } else { if (isnumber(argv[0])) { errno = 0; /* gid is an int */ gid = (gid_t)strtoul(argv[0], NULL, 10); if (errno != 0) { if (errno == ERANGE) { (void) fprintf(stderr, gettext( "chgrp: group id is too large\n")); exit(2); } else { (void) fprintf(stderr, gettext( "chgrp: invalid group id\n")); exit(2); } } } else { (void) fprintf(stderr, "chgrp: "); (void) fprintf(stderr, gettext("unknown group: %s\n"), argv[0]); exit(2); } } for (c = 1; c < argc; c++) { tree = NULL; if (lstat(argv[c], &stbuf) < 0) { status += Perror(argv[c]); continue; } if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFLNK)) { if (hflag || Pflag) { /* * Change the group id of the symbolic link * specified on the command line. * Don't follow the symbolic link to * any other part of the file hierarchy. */ LCHOWN(argv[c], -1, gid); } else { if (stat(argv[c], &stbuf2) < 0) { status += Perror(argv[c]); continue; } /* * We know that we are to change the * group of the file referenced by the * symlink specified on the command line. * Now check to see if we are to follow * the symlink to any other part of the * file hierarchy. */ if (FOLLOW_CL_LINKS) { if ((stbuf2.st_mode & S_IFMT) == S_IFDIR) { /* * We are following symlinks so * traverse into the directory. * Add this node to the search * tree so we don't get into an * endless loop. */ if (add_tnode(&tree, stbuf2.st_dev, stbuf2.st_ino) == 1) { chgrpr(argv[c], gid); /* * Try to restore the * SET[UG]ID bits. */ SETUGID_PRESERVE( argv[c], stbuf2.st_mode & ~S_IFMT); } else { /* * Error occurred. * rc can't be 0 * as this is the first * node to be added to * the search tree. */ status += Perror( argv[c]); } } else { /* * Change the group id of the * file referenced by the * symbolic link. */ CHOWN(argv[c], -1, gid); } } else { /* * Change the group id of the file * referenced by the symbolic link. */ CHOWN(argv[c], -1, gid); if ((stbuf2.st_mode & S_IFMT) == S_IFDIR) { /* Reset the SET[UG]ID bits. */ SETUGID_PRESERVE(argv[c], stbuf2.st_mode & ~S_IFMT); } } } } else if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFDIR)) { /* * Add this node to the search tree so we don't * get into a endless loop. */ if (add_tnode(&tree, stbuf.st_dev, stbuf.st_ino) == 1) { chgrpr(argv[c], gid); /* Restore the SET[UG]ID bits. */ SETUGID_PRESERVE(argv[c], stbuf.st_mode & ~S_IFMT); } else { /* * An error occurred while trying * to add the node to the tree. * Continue on with next file * specified. Note: rc shouldn't * be 0 as this was the first node * being added to the search tree. */ status += Perror(argv[c]); } } else { if (hflag || Pflag) { LCHOWN(argv[c], -1, gid); } else { CHOWN(argv[c], -1, gid); } /* If a directory, reset the SET[UG]ID bits. */ if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { SETUGID_PRESERVE(argv[c], stbuf.st_mode & ~S_IFMT); } } } return (status); }
static void CcpLayerUp(Fsm fp) { Bund b = (Bund)fp->arg; CcpState const ccp = &b->ccp; struct ngm_connect cn; char buf[64]; /* If nothing was negotiated in either direction, close CCP */ if ((!ccp->recv || !(*ccp->recv->Negotiated)(b, COMP_DIR_RECV)) && (!ccp->xmit || !(*ccp->xmit->Negotiated)(b, COMP_DIR_XMIT))) { Log(LG_CCP, ("[%s] %s: No compression negotiated", Pref(fp), Fsm(fp))); FsmFailure(fp, FAIL_NEGOT_FAILURE); return; } /* Check for required encryption */ if (CcpCheckEncryption(b) < 0) { return; } /* Initialize each direction */ if (ccp->xmit != NULL && ccp->xmit->Init != NULL && (*ccp->xmit->Init)(b, COMP_DIR_XMIT) < 0) { Log(LG_CCP, ("[%s] %s: compression init failed", Pref(fp), Fsm(fp))); FsmFailure(fp, FAIL_NEGOT_FAILURE); /* XXX */ return; } if (ccp->recv != NULL && ccp->recv->Init != NULL && (*ccp->recv->Init)(b, COMP_DIR_RECV) < 0) { Log(LG_CCP, ("[%s] %s: decompression init failed", Pref(fp), Fsm(fp))); FsmFailure(fp, FAIL_NEGOT_FAILURE); /* XXX */ return; } if (ccp->xmit != NULL && ccp->xmit->Compress != NULL) { /* Connect a hook from the ppp node to our socket node */ snprintf(cn.path, sizeof(cn.path), "[%x]:", b->nodeID); snprintf(cn.ourhook, sizeof(cn.ourhook), "c%d", b->id); strcpy(cn.peerhook, NG_PPP_HOOK_COMPRESS); if (NgSendMsg(gCcpCsock, ".:", NGM_GENERIC_COOKIE, NGM_CONNECT, &cn, sizeof(cn)) < 0) { Perror("[%s] can't connect \"%s\"->\"%s\" and \"%s\"->\"%s\"", b->name, ".:", cn.ourhook, cn.path, cn.peerhook); } } if (ccp->recv != NULL && ccp->recv->Decompress != NULL) { /* Connect a hook from the ppp node to our socket node */ snprintf(cn.path, sizeof(cn.path), "[%x]:", b->nodeID); snprintf(cn.ourhook, sizeof(cn.ourhook), "d%d", b->id); strcpy(cn.peerhook, NG_PPP_HOOK_DECOMPRESS); if (NgSendMsg(gCcpCsock, ".:", NGM_GENERIC_COOKIE, NGM_CONNECT, &cn, sizeof(cn)) < 0) { Perror("[%s] can't connect \"%s\"->\"%s\" and \"%s\"->\"%s\"", b->name, ".:", cn.ourhook, cn.path, cn.peerhook); } } /* Report what we're doing */ Log(LG_CCP, ("[%s] CCP: Compress using: %s (%s)", b->name, !ccp->xmit ? "none" : ccp->xmit->name, (ccp->xmit && ccp->xmit->Describe) ? (*ccp->xmit->Describe)(b, COMP_DIR_XMIT, buf, sizeof(buf)) : "")); Log(LG_CCP, ("[%s] CCP: Decompress using: %s (%s)", b->name, !ccp->recv ? "none" : ccp->recv->name, (ccp->recv && ccp->recv->Describe) ? (*ccp->recv->Describe)(b, COMP_DIR_RECV, buf, sizeof(buf)) : "")); /* Update PPP node config */ b->pppConfig.bund.enableCompression = (ccp->xmit != NULL)?ccp->xmit->mode:0; b->pppConfig.bund.enableDecompression = (ccp->recv != NULL)?ccp->recv->mode:0; NgFuncSetConfig(b); /* Update interface MTU */ BundUpdateParams(b); }
int main(int argc, char *argv[]) { node_t *Gnode=NULL, *Snode=NULL, *FoundNode=NULL, *TmpNode=NULL; size_t i, niter, dim[1], tot_dim, narray; lmint_t sockfd, portno; socklen_t clilen; struct sockaddr_in cli_addr; lmchar_t *name="CFD2CSM"; lmchar_t *name1="CSM2CFD"; lmdouble_t *P, dy, *tmpfloat, *x, *y, *z, *time, sign; find_t *SFounds; opts_t opts, *Popts_1; FILE *fp; client_fce_struct_t InpPar, *PInpPar; PInpPar = &InpPar; /* * get port number */ if (argc < 3) { fprintf(stderr,"ERROR, no IPaddress and port number provided\n"); exit(1); } portno = atoi(argv[2]); /* * open socket - because we use more then just send - receive scenario * we need to open socket manualy and used Send_receive function with hostname = NULL, ie. as server * portno is then replaced by socket number */ niter = 0; while(1){ printf("\n\n-------------------------------- i = %ld\n\n", ++niter); /* * open socket */ PInpPar->channel_name = name; PInpPar->SR_MODE = 'R'; if ( (PInpPar->mode = get_exchange_channel_mode('D', 'N')) == -1) Error("wrong client mode"); Popts_1 = &opts; m3l_set_Send_receive_tcpipsocket(&Popts_1); if( (sockfd = open_connection_to_server(argv[1], portno, PInpPar, Popts_1)) < 1) Error("client_sender: Error when opening socket"); Gnode = client_receiver(sockfd, PInpPar, (opts_t *)NULL, (opts_t *)NULL); printf(" Data from Edge received\n"); // if(m3l_Cat(Gnode, "--all", "-P", "-L", "*", (char *)NULL) != 0) // Error("CatData"); if(m3l_Cat(Gnode, "--detailed", "-P", "-L", "*", (char *)NULL) != 0) Error("CatData"); if( (SFounds = m3l_Locate(Gnode, "/CFD_2_CSM/P", "/*/*", (lmchar_t *)NULL)) != NULL){ if( m3l_get_Found_number(SFounds) != 1) Error("socket_edge2out: More then one DX data set found"); /* * pointer to list of found nodes */ if( (FoundNode = m3l_get_Found_node(SFounds, 0)) == NULL) Error("socket_edge2out: Did not find 1st data pointer"); tot_dim = m3l_get_List_totdim(FoundNode); narray = tot_dim; printf(" Size of array is %ld\n", narray); if( (x = (lmdouble_t *)m3l_get_data_pointer(FoundNode)) == NULL) Error("socket_edge2out: Did not find DX data pointer"); /* * free memory allocated in m3l_Locate */ m3l_DestroyFound(&SFounds); } else { Error("socket_edge2out: P not found\n"); } SFounds = m3l_Locate(Gnode, "/CFD_2_CSM/Time", "/*/*", (lmchar_t *)NULL); FoundNode = m3l_get_Found_node(SFounds, 0); time = (lmdouble_t *)m3l_get_data_pointer(FoundNode); m3l_DestroyFound(&SFounds); printf("Time is %lf tot-dim is %ld\n", *time, tot_dim); // printf("After P\n"); // SFounds = m3l_Locate(Gnode, "/CFD_2_CSM/P1", "/*/*", (lmchar_t *)NULL); // FoundNode = m3l_get_Found_node(SFounds, 0); // y = (lmdouble_t *)m3l_get_data_pointer(FoundNode); // m3l_DestroyFound(&SFounds); // printf("After P1\n"); // SFounds = m3l_Locate(Gnode, "/CFD_2_CSM/P2", "/*/*", (lmchar_t *)NULL); // FoundNode = m3l_get_Found_node(SFounds, 0); // z = (lmdouble_t *)m3l_get_data_pointer(FoundNode); // m3l_DestroyFound(&SFounds); // printf("After P2 %d\n", tot_dim); // fp = fopen("COORDINATES","w"); // printf("writing \n"); // // for (i=0; i<tot_dim; i++){ // // printf("%d %lf %lf %lf\n", i, x[i],y[i], z[i]); // fprintf(fp, "%lf %lf %lf\n", x[i],y[i], z[i]); // // } // // fclose(fp); // printf("closing \n"); /* * close socket */ if( close(sockfd) == -1) Perror("close"); // printf(" ENTER \n"); // getchar(); // sleep(10); /* * open socket for sending data back */ PInpPar->channel_name = name1; PInpPar->SR_MODE = 'S'; if ( (PInpPar->mode = get_exchange_channel_mode('D', 'N')) == -1) Error("wrong client mode"); Popts_1 = &opts; m3l_set_Send_receive_tcpipsocket(&Popts_1); if( (sockfd = open_connection_to_server(argv[1], portno, PInpPar, Popts_1)) < 1) Error("client_sender: Error when opening socket"); if( (Snode = m3l_Mklist("CSM_2_CFD", "DIR", 0, 0, (node_t **)NULL, (const char *)NULL, (const char *)NULL, (char *)NULL)) == 0) Perror("m3l_Mklist"); dim[0] = narray; /* * add iteration number */ if( (TmpNode = m3l_Mklist("DX", "D", 1, dim, &Snode, "/CSM_2_CFD", "./", (char *)NULL)) == 0) Error("m3l_Mklist"); tmpfloat = (lmdouble_t *)m3l_get_data_pointer(TmpNode); for (i=0; i<tot_dim; i++) tmpfloat[i] = 0; if( (TmpNode = m3l_Mklist("DY", "D", 1, dim, &Snode, "/CSM_2_CFD", "./", (char *)NULL)) == 0) Error("m3l_Mklist"); tmpfloat = (lmdouble_t *)m3l_get_data_pointer(TmpNode); for (i=0; i<tot_dim; i++) tmpfloat[i] = 0; if( (TmpNode = m3l_Mklist("DZ", "D", 1, dim, &Snode, "/CSM_2_CFD", "./", (char *)NULL)) == 0) Error("m3l_Mklist"); tmpfloat = (lmdouble_t *)m3l_get_data_pointer(TmpNode); printf(" Largest deformation is %lf\n", 0.1*sin(*time*2*3.1415926*5)); for (i=0; i<tot_dim; i++){ // tmpfloat[i] = 0.005*x[i]; // tmpfloat[i] = *time*5*0.005*(1-cos(x[i]*3.1415926)); if(abs(x[i]) > 0){ sign = x[i]/abs(x[i]); } else sign = 0; tmpfloat[i] = 0.1*sin(*time*2*3.1415926*5)*(1-cos(x[i]*3.1415926/2.)); } client_sender(Snode, sockfd, PInpPar, (opts_t *)NULL, (opts_t *)NULL); if(m3l_Umount(&Gnode) != 1) Perror("m3l_Umount"); if(m3l_Umount(&Snode) != 1) Perror("m3l_Umount");/* * close socket */ if( close(sockfd) == -1) Perror("close"); } return 0; }
static int ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp) { const struct afswtch *afp, *nafp; const struct cmd *p; struct callback *cb; int s; strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); afp = uafp != NULL ? uafp : af_getbyname("inet"); top: ifr.ifr_addr.sa_family = afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ? AF_LOCAL : afp->af_af; if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 && (uafp != NULL || errno != EPROTONOSUPPORT || (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)) err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family); while (argc > 0) { p = cmd_lookup(*argv, iscreate); if (iscreate && p == NULL) { /* * Push the clone create callback so the new * device is created and can be used for any * remaining arguments. */ cb = callbacks; if (cb == NULL) errx(1, "internal error, no callback"); callbacks = cb->cb_next; cb->cb_func(s, cb->cb_arg); iscreate = 0; /* * Handle any address family spec that * immediately follows and potentially * recreate the socket. */ nafp = af_getbyname(*argv); if (nafp != NULL) { argc--, argv++; if (nafp != afp) { close(s); afp = nafp; goto top; } } /* * Look for a normal parameter. */ continue; } if (p == NULL) { /* * Not a recognized command, choose between setting * the interface address and the dst address. */ p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd); } if (p->c_u.c_func || p->c_u.c_func2) { if (p->c_parameter == NEXTARG) { if (argv[1] == NULL) errx(1, "'%s' requires argument", p->c_name); p->c_u.c_func(argv[1], 0, s, afp); argc--, argv++; } else if (p->c_parameter == OPTARG) { p->c_u.c_func(argv[1], 0, s, afp); if (argv[1] != NULL) argc--, argv++; } else if (p->c_parameter == NEXTARG2) { if (argc < 3) errx(1, "'%s' requires 2 arguments", p->c_name); p->c_u.c_func2(argv[1], argv[2], s, afp); argc -= 2, argv += 2; } else p->c_u.c_func(*argv, p->c_parameter, s, afp); } argc--, argv++; } /* * Do any post argument processing required by the address family. */ if (afp->af_postproc != NULL) afp->af_postproc(s, afp); /* * Do deferred callbacks registered while processing * command-line arguments. */ for (cb = callbacks; cb != NULL; cb = cb->cb_next) cb->cb_func(s, cb->cb_arg); /* * Do deferred operations. */ if (clearaddr) { if (afp->af_ridreq == NULL || afp->af_difaddr == 0) { warnx("interface %s cannot change %s addresses!", name, afp->af_name); clearaddr = 0; } } if (clearaddr) { int ret; strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name); ret = ioctl(s, afp->af_difaddr, afp->af_ridreq); if (ret < 0) { if (errno == EADDRNOTAVAIL && (doalias >= 0)) { /* means no previous address for interface */ } else Perror("ioctl (SIOCDIFADDR)"); } } if (newaddr) { if (afp->af_addreq == NULL || afp->af_aifaddr == 0) { warnx("interface %s cannot change %s addresses!", name, afp->af_name); newaddr = 0; } } if (newaddr && (setaddr || setmask)) { strncpy(afp->af_addreq, name, sizeof ifr.ifr_name); if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) Perror("ioctl (SIOCAIFADDR)"); } close(s); return(0); }