#include <stdio.h> #include <sys/types.h> #include <sys/file.h> #include <sys/socket.h> #include <netdb.h> #include <netinet/in.h> extern char *getenv(); int servfd; connectserver() { struct sockaddr_in sin; struct servent *sp; struct hostent *hp; char *host; char *p; short port; int slen; int expect; host = getenv("BOTHOST"); p = getenv("BOTPORT"); if(host == NULL || p == NULL) { (void)fprintf(stderr,"must set BOTHOST and BOTPORT\n"); return(1); } if((hp = gethostbyname(host)) == NULL) { (void)fprintf(stderr,"unknown host \"%s\"\n",host); return(1); } #ifdef DEBUG (void)fprintf(stderr,"<<<connecting to %s, port %s>>>\n",host,p); #endif port = htons(atoi(p)); (void)bcopy(hp->h_addr,(char *)&sin.sin_addr,hp->h_length); sin.sin_port = port; sin.sin_family = AF_INET; if((servfd = socket(AF_INET,SOCK_STREAM,0)) < 0) { perror("socket"); return(1); } if(connect(servfd,(struct sockaddr *)&sin,sizeof(sin)) < 0) { perror("connect"); return(1); } #ifdef DEBUG (void)fprintf(stderr,"<<<connected>>>\n"); #endif return(0); } loginserver() { char buf[BUFSIZ]; int nr; int errors = 0; char *robot; char *password; robot = getenv("BOTNAME"); if(robot == 0) { (void)fprintf(stderr,"must set BOTNAME in environment!\n"); return(1); } password = getenv("BOTPASS"); if(password == 0) { (void)fprintf(stderr,"must set BOTPASS in environment!\n"); return(1); } #ifdef DEBUG (void)fprintf(stderr,"<<<logging in as %s, password %s>>>\n",robot,password); #endif while(errors < 10) { /* assume first buffer read is the login header */ nr = read(servfd,buf,BUFSIZ); buf[nr - 1] = '\0'; (void)fprintf(stderr,"got:%s\n",buf); if(strncmp("Welcome to",buf,10)) return(0); (void)sprintf(buf,"connect %s %s\n",robot,password); /* so send our connect message and hope */ nr = write(servfd,buf,strlen(buf)); #ifdef DEBUG (void)fprintf(stderr,"<<<sent login message:%s>>>",buf); #endif errors++; } (void)fprintf(stderr,"too many errors. login failed\n"); return(1); }