int connecthost(const char * host, short port, int * fd) { struct sockaddr_in sain; char logbuf[64]; int fetchfd; sain.sin_family = AF_INET; sain.sin_port = htons(port); if(host == NULL && host[0] == '\0') return -1; if(hnametoaddr(host, &sain.sin_addr) == -1) return -1; if((fetchfd = initsocket(SOCK_STREAM,0)) == -1){ logerr("can not init a socket", errno); return -1; } *fd = fetchfd; sighandler_t presighandler = signal(SIGALRM, timeout); alarm(CONTIMEOUT);/* start timer */ if(connect(fetchfd, (struct sockaddr *)&sain, sizeof(sain)) == -1){ if(errno != EINTR){ sprintf(logbuf, "connect to remote host %s error", host); logerr(logbuf, errno); } return -1; } alarm(0);/* conenct success and cancel timer. */ signal(SIGALRM, presighandler); return 0; }
int main(int argc,char **argv){ int c, errflag = 0; while ((c = getopt(argc, argv, "p:h?")) != -1) switch (c) { case 'p': DefaultPath = optarg; break; case 'h': case '?': default: errflag++; break; } if (errflag > 0) { usage(argv[0]); return (1); } if (argc - optind < 1) { usage(argv[0]); exit(1); } initial_bbs(NULL); initsocket(); ctlinnbbsd(argc - optind, argv + optind); closesocket(); return 0; }
int main(void) { char *host = "irc.rizon.net"; char *port = "6667"; char *channel = "#silvestackers"; char nick[32], pass[32]; char buf[513]; int sl; printf("Select a nickname for the bot: "); fgets(nick, sizeof nick, stdin); printf("Need to auth with NickServ? Pass: "******"\n")] = '\0'; pass[strcspn(pass, "\n")] = '\0'; initsocket(host, port); raw("USER %s 0 * :KbpsIRC bot.\r\n", nick); raw("NICK %s", nick); while ((sl = read(conn, sbuf, 512)) > 0) { int i; for(i = 0; i < sl; i++) buf[i] = sbuf[i]; buf[strcspn(buf, "\r\n")] = '\0'; /* TODO: take into account split lines. */ printf(">> %s", buf); interpret(parse(buf)); } return 0; }
/*main loop, message sender, etc.pp.*/ int main(int argc,char**argv) { int c,optindex=1; struct dhcp_msg *msg; /*parse options*/ argv0=*argv; while(1){ c=getopt_long(argc,argv,shortopt,longopt,&optindex); if(c==-1)break; switch(c){ case 'p':getprefix=1;break; case 'P':getprefix=0;break; case 'a':getaddress=1;break; case 'A':getaddress=0;break; case 'd':getdns=1;break; case 'D':getdns=0;break; case 'r':retries=atoi(optarg);break; case 'l':localid=optarg;break; case 'u':setduid(optarg);break; case 'c':userapid=1;break; case 'C':userapid=0;break; case 'L':setloglevel(optarg);break; default: fprintf(stderr,"Syntax error in arguments.\n"); printhelp(); return 1; break; case 'h': printhelp(); return 0; break; } } if((optind+2)!=argc){ fprintf(stderr,"Syntax error.\n"); printhelp(); return 1; } device=argv[optind]; script=argv[optind+1]; if(DUIDLEN==0){ if(localid) setlocalid(localid); else initlocalid(); } /*init socket*/ initsocket(DHCP_CLIENTPORT,device); if(sockfd<0){ td_log(LOGERROR,"unable to allocate socket, exiting."); exit(1); } /*init my own stuff*/ inititems(); /*init SOLICIT/IREQ msg*/ if(!getaddress && !getprefix){ msg=newmessage(MSG_IREQUEST); addrecvfilter(MSG_REPLY); }else{ msg=newmessage(MSG_SOLICIT); addrecvfilter(MSG_ADVERTISE); } COMPAREMSGID=1; settargetserver(&msg->msg_peer); messageaddopt(msg,OPT_CLIENTID); if(getdns){ messageaddoptrequest(msg,OPT_DNS_SERVER); messageaddoptrequest(msg,OPT_DNS_NAME); } if(getaddress)messageaddopt(msg,OPT_IANA); if(getprefix)messageaddopt(msg,OPT_IAPD); if(userapid&&(getaddress||getprefix))messageaddopt(msg,OPT_RAPIDCOMMIT); /*start main loop*/ for(c=0;c<retries;c++){ sendmessage(msg); fd_set rfd,xfd; struct timeval tv; int sret; //wait for event FD_ZERO(&rfd); FD_ZERO(&xfd); FD_SET(sockfd,&rfd); FD_SET(sockfd,&xfd); tv.tv_sec=1; tv.tv_usec=0; sret=select(sockfd+1,&rfd,0,&xfd,&tv); //check for errors if(sret<0){ int e=errno; if(e==EAGAIN)continue; td_log(LOGERROR,"Error caught: %s\n",strerror(e)); return 1; } //check for event if(sret>0){ if(FD_ISSET(sockfd,&rfd)){ struct dhcp_msg*msg2; msg2=readmessage(); if(msg2) if(handlemessage(msg2,msg)==0)break; } if(FD_ISSET(sockfd,&xfd)){ td_log(LOGERROR,"Exception on socket caught.\n"); return 1; } }else td_log(LOGDEBUG,"timeout, iteration %i",c); } /*execute script*/ return execscript(); }