#include #include #include #include #include #include #include "coc_util.h" #include "myc_err.h" #include "myc_str.h" #include #include #include #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; n=0; go=0; ires=0; iret=term_wait_fd(fd, skip?5000:1000); while (iret==1) { ERR_SI(l=recv(fd, msg, sizeof(msg)-1, 0)); if (l==0) return(0); msg[l]='\0'; if (skip==4) { ires=1; } else 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"); } else { if (j==0) { us=argv[i]; j=1; } else if (j==1) { ps=argv[i]; j=2; } else { printf("too many arguments\n"); return 0; } } } } if (j==1) { printf("missing password\n"); return(0); } instr=getenv("Instrument"); if (instr==NULL || instr[0]=='\0') { printf("Instrument is undefined\n"); 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; } 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="02lns1"; } } 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) { us="Spy"; } printf("logged 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)); ERR_I(i=readWrite(fd,4)); printf("synchronized (%d chars read)\n", i); } iret=1; buf[0]='\0'; pos=0; str_lowcase(instr,instr); sprintf(ibuf, "six[%s%s] ", instr, 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")) skip=4; 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)); if (skip==4) { printf("synchronize (%d chars read)\n", i); } 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); OnError: ErrShow("end"); return(0); }