#include #include #include #include #include #include #include #include #include #include "coc_util.h" #include "myc_err.h" #include "myc_str.h" #include "term.h" int CocCreateSockAdr( struct sockaddr_in *sockaddrPtr, /* Socket address */ const char *host, /* Host. NULL implies INADDR_ANY */ int port) /* Port number */ { struct hostent *hostent; /* Host database entry */ struct in_addr addr; /* For 64/32 bit madness */ (void) memset((char *) sockaddrPtr, '\0', sizeof(struct sockaddr_in)); sockaddrPtr->sin_family = AF_INET; sockaddrPtr->sin_port = htons((unsigned short) (port & 0xFFFF)); if (host == NULL || host[0]=='\0') { addr.s_addr = INADDR_ANY; } else { hostent = gethostbyname(host); if (hostent != NULL) { memcpy((char *) &addr, (char *) hostent->h_addr_list[0], (size_t) hostent->h_length); } else { addr.s_addr = inet_addr(host); if (addr.s_addr == (unsigned long)-1) { return -1; /* error */ } } } /* * There is a rumor that this assignment may require care on * some 64 bit machines. */ sockaddrPtr->sin_addr.s_addr = addr.s_addr; return 0; } int readWrite(int fd, int skip) { char msg[128]; int l, i, go, iret, ires, n; /* * read from fd and write to stdout until a key pressed, * a timeout occured (1 or 5 sec.), or a message * received. * skip=0: timeout 1 sec (normal behaviour) * skip>0: timeout 10 sec. * skip>1: swallow text * skip=1,2: return 0 if SICS not found in message text * skip=3: return 0 if not acknowledged */ n=0; go=0; ires=0; iret=term_wait_fd(fd, skip?10000:1000); while (iret==1) { ERR_SI(l=recv(fd, msg, sizeof(msg)-1, 0)); /* if (l==0) return(0); */ msg[l]='\0'; if (skip==3) { if (NULL!=strstr(msg, "Acknowledged")) ires=1; } else if (skip==1 || skip==2) { if (NULL!=strstr(msg, "SICS")) ires=1; } for (i=0; i=32) { printf("argument too long\n"); Usage(); return 0; } else { if (j==0) { us=argv[i]; j=1; } else if (j==1) { ps=argv[i]; j=2; } else { printf("too many arguments\n"); Usage(); return 0; } } } } if (j==1) { printf("missing password\n"); Usage(); return 0; } instr=getenv("Instrument"); if (instr==NULL || instr[0]=='\0') { printf("Instrument is undefined\n"); Usage(); return 0; } ERR_I(CocCreateSockAdr(&sadr, "0", port)); ERR_SI(fd=socket(AF_INET, SOCK_STREAM, 0)); term_reg_socket(fd); ERR_SI(connect(fd, (struct sockaddr *)&sadr, sizeof(sadr))); strcpy(buf, "sicslogin Spy 007\r\n"); ERR_SI(send(fd, buf, strlen(buf), 0)); ERR_I(i=readWrite(fd,skip)); if (!i) { printf("rejected\n"); return 0; } if (skip==2) printf("reading welcome message ...\n"); if (j==0) { rights=getenv("six_rights"); if (rights!=NULL && 0==strcmp(rights, instr)) { us="lnsmanager"; ps="lnsSICSlns"; } else { sprintf(ibuf, "%suser", instr); str_lowcase(ibuf, ibuf); us=ibuf; ps="03lns1"; } } sprintf(buf, "config Rights %s %s\r\n", us, ps); ERR_SI(send(fd, buf, strlen(buf), 0)); ERR_I(i=readWrite(fd,3)); if (!i && 0 != strcmp(instr, "TASP")) { printf("password guess failed, logged in as Spy\n\n"); Usage(); us="Spy"; } printf("\rlogged in to SICS as %s on %s%s\n", us, instr, sim); /* if (*sim!='\0') { strcpy(buf, "sync\r\n"); ERR_SI(send(fd, buf, strlen(buf), 0)); printf("\rsynchronize ...\n"); ERR_I(i=readWrite(fd,1)); } */ iret=1; buf[0]='\0'; pos=0; str_lowcase(ilow,instr); sprintf(ibuf, "six[%s%s] ", ilow, sim); while (1) { FD_ZERO(&mask); FD_SET(fd, &mask); iret=term_get_line(buf, sizeof(buf)-2, &pos, ibuf, &mask); if (iret==STDIN_FILENO) { /* input line terminated */ str_lowcase(lbuf, buf); if (0==strcmp(lbuf,"quit")) break; if (0==strcmp(lbuf,"exit")) break; if (0==strcmp(lbuf,"stop")) { strcpy(buf, "INT1712 3"); } else if (0==strcmp(buf, "")) { strcpy(buf, "status"); } fputs("\n", stdout); skip=0; if (0==strcmp(lbuf,"sync")) { printf("\rsynchronize ...\n"); skip=1; } l=strlen(buf); /* printf("sent> %s[%d chars]\n", buf, l); */ buf[l]=13; buf[l+1]=10; ERR_SI(send(fd, buf, l+2, 0)); ERR_I(i=readWrite(fd,skip)); buf[0]='\0'; pos=0; } else if (iret<0) { /* ?? */ printf("\nwhat?\n"); exit(1); } else { /* socket iret ready to read */ assert(fd==iret); ERR_I(readWrite(fd,0)); } } fputs("\n", stdout); return 0; Usage: return 0; OnError: ErrShow("end"); return 0; }