d25ff0c1d7011681354e45bc6acadd76283f226f
[nethome.git] / src / ghortident.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <netinet/in.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8
9 #define DBG 0
10
11 int parseparams(int argc, char *argv[], ulong *adres, uint *rport, uint *lport, uint *service)
12 {
13         char *s;
14         uint c, d;
15         
16         if (argc<1) return 255;
17         
18         *lport=80;
19         *service=113;
20         
21         switch (argc)
22         {
23                 case 2:
24                         fprintf(stderr,"Error: missing remote port!\n");
25                 case 1:
26                         fprintf(stderr,"Usage: ident <xxx.xxx.xxx.xxx> <remote port> [local port] [ident port]\n");
27                         return 1;
28                 default:
29                 case 5:
30                         c=0;
31                         s=argv[4];
32                         while (*s)
33                         {
34                                 if (c<65536 && *s>='0' && *s<='9') c=c*10+*s-'0';
35                                 else
36                                 {
37                                         fprintf(stderr,"Error: wrong ident port!\n");
38                                         return 5;
39                                 }
40                                 s++;
41                         }
42                         *service=c;
43                 case 4:
44                         c=0;
45                         s=argv[3];
46                         while (*s)
47                         {
48                                 if (c<65536 && *s>='0' && *s<='9') c=c*10+*s-'0';
49                                 else
50                                 {
51                                         fprintf(stderr,"Error: wrong local port!\n");
52                                         return 4;
53                                 }
54                                 s++;
55                         }
56                         *lport=c;
57                 case 3:
58                         *adres=0;
59                         c=d=0;
60                         s=argv[1];
61                         while (*s)
62                         {       
63                                 if (*s>='0' && *s<='9') 
64                                 {
65                                         c=c*10+*s-'0';
66                                         if (c>255)
67                                         {
68                                                 fprintf(stderr,"Error: wrong address!\n");
69                                                 return 2;
70                                         }
71                                 }
72                                 else
73                                         if (*s=='.')
74                                         {
75                                                 *adres=*adres*256+c;
76                                                 c=0;
77                                                 if (d++>=3)
78                                                 {
79                                                         fprintf(stderr,"Error: wrong address!\n");
80                                                         return 2;
81                                                 }
82                                         }
83                                         else 
84                                         {
85                                                 fprintf(stderr,"Error: wrong address!\n"); 
86                                                 return 2;
87                                         }
88                                 s++;
89                         }
90                         *adres=*adres*256+c;
91                         
92                         c=0;
93                         s=argv[2];
94                         while (*s)
95                         {
96                                 if (c<65536 && *s>='0' && *s<='9') c=c*10+*s-'0';
97                                 else
98                                 {
99                                         fprintf(stderr,"Error: wrong remote port!\n");
100                                         return 3;
101                                 }
102                                 s++;
103                         }
104                         *rport=c;
105         }
106         return 0;
107 }
108
109 int main(int argc, char *argv[])
110 {
111         int sock;
112         struct sockaddr_in addr;
113         char buf[256];
114         int count;
115         ulong adres;
116         uint rport, lport, service;
117         int err;
118         static const int val_one=1;
119
120         if (parseparams(argc,argv,&adres,&rport,&lport,&service)) return 3;
121
122         if (DBG) fprintf(stderr,"addr: %lX  rport: %u  lport: %u  service: %i\n",adres,rport,lport,service);
123
124         addr.sin_family=AF_INET;
125         addr.sin_addr.s_addr=htonl(adres);
126         addr.sin_port=htons(service);
127
128         sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
129         setsockopt(sock,SOL_SOCKET,SO_KEEPALIVE,&val_one,sizeof(val_one));
130
131         if (DBG) 
132         {
133                 err=errno;
134                 fprintf(stderr,"socket: %i, errno: %i\n",sock,err);
135         }
136         if (sock==-1) 
137         {
138                 fprintf(stderr,"Error: cannot create socket!\n");
139                 return 4;
140         }
141
142         count=connect(sock,(struct sockaddr*)&addr,sizeof(addr));
143         if (DBG)
144         {
145                 err=errno;
146                 fprintf(stderr,"connect: %i, errno: %i\n",count,err);   
147         }
148
149         if (count)
150         {
151                 fprintf(stderr,"Error: connection failed!\n");
152                 return 5;
153         }
154
155         count=snprintf(buf,sizeof(buf),"%d , %d\n",rport,lport);
156         if (count==-1) return 1;
157         if (write(sock,buf,count)!=count) return 2;
158         
159         for (;;)
160         {
161                 count=read(sock,buf,sizeof(buf)-1);
162                 if (count<=0) break;
163                 buf[count]=0;
164                 printf("%s",buf);
165                 fflush(stdout);
166         }
167
168         if (DBG) fprintf(stderr,"count = %d , errno = %d\n",count,errno);
169         close(sock);
170         return 0;
171 }
172