#include #include #include #include #include #include #include #define DBG 0 int parseparams(int argc, char *argv[], ulong *adres, uint *rport, uint *lport, uint *service) { char *s; uint c, d; if (argc<1) return 255; *lport=80; *service=113; switch (argc) { case 2: fprintf(stderr,"Error: missing remote port!\n"); case 1: fprintf(stderr,"Usage: ident [local port] [ident port]\n"); return 1; default: case 5: c=0; s=argv[4]; while (*s) { if (c<65536 && *s>='0' && *s<='9') c=c*10+*s-'0'; else { fprintf(stderr,"Error: wrong ident port!\n"); return 5; } s++; } *service=c; case 4: c=0; s=argv[3]; while (*s) { if (c<65536 && *s>='0' && *s<='9') c=c*10+*s-'0'; else { fprintf(stderr,"Error: wrong local port!\n"); return 4; } s++; } *lport=c; case 3: *adres=0; c=d=0; s=argv[1]; while (*s) { if (*s>='0' && *s<='9') { c=c*10+*s-'0'; if (c>255) { fprintf(stderr,"Error: wrong address!\n"); return 2; } } else if (*s=='.') { *adres=*adres*256+c; c=0; if (d++>=3) { fprintf(stderr,"Error: wrong address!\n"); return 2; } } else { fprintf(stderr,"Error: wrong address!\n"); return 2; } s++; } *adres=*adres*256+c; c=0; s=argv[2]; while (*s) { if (c<65536 && *s>='0' && *s<='9') c=c*10+*s-'0'; else { fprintf(stderr,"Error: wrong remote port!\n"); return 3; } s++; } *rport=c; } return 0; } int main(int argc, char *argv[]) { int sock; struct sockaddr_in addr; char buf[256]; int count; ulong adres; uint rport, lport, service; int err; static const int val_one=1; if (parseparams(argc,argv,&adres,&rport,&lport,&service)) return 3; if (DBG) fprintf(stderr,"addr: %lX rport: %u lport: %u service: %i\n",adres,rport,lport,service); addr.sin_family=AF_INET; addr.sin_addr.s_addr=htonl(adres); addr.sin_port=htons(service); sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); setsockopt(sock,SOL_SOCKET,SO_KEEPALIVE,&val_one,sizeof(val_one)); if (DBG) { err=errno; fprintf(stderr,"socket: %i, errno: %i\n",sock,err); } if (sock==-1) { fprintf(stderr,"Error: cannot create socket!\n"); return 4; } count=connect(sock,(struct sockaddr*)&addr,sizeof(addr)); if (DBG) { err=errno; fprintf(stderr,"connect: %i, errno: %i\n",count,err); } if (count) { fprintf(stderr,"Error: connection failed!\n"); return 5; } count=snprintf(buf,sizeof(buf),"%d , %d\n",rport,lport); if (count==-1) return 1; if (write(sock,buf,count)!=count) return 2; for (;;) { count=read(sock,buf,sizeof(buf)-1); if (count<=0) break; buf[count]=0; printf("%s",buf); fflush(stdout); } if (DBG) fprintf(stderr,"count = %d , errno = %d\n",count,errno); close(sock); return 0; }