"identd" client, by Tomas Bures <ghort@atrey.karlin.mff.cuni.cz>
authorshort <>
Mon, 9 Jul 2001 22:48:40 +0000 (22:48 +0000)
committershort <>
Mon, 9 Jul 2001 22:48:40 +0000 (22:48 +0000)
src/ghortident.c [new file with mode: 0644]

diff --git a/src/ghortident.c b/src/ghortident.c
new file mode 100644 (file)
index 0000000..d25ff0c
--- /dev/null
@@ -0,0 +1,172 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <netinet/in.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#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 <xxx.xxx.xxx.xxx> <remote port> [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;
+}
+