]> git.mxchange.org Git - flightgear.git/blob - src/NetworkOLK/Tools/fgd_scan.c
2000 17 Jan updates from "Oliver Delise" <delise@mail.isis.de>
[flightgear.git] / src / NetworkOLK / Tools / fgd_scan.c
1 /*************************************************************/
2 /* FGD_SCAN.C by Oliver Delise                               */
3 /* Contact info:                                             */
4 /* e-mail: delise@mail-isis.de                               */
5 /* www: http://www.isis.de/members/odelise/progs/flightgear  */
6 /*                                                           */
7 /* Version 0.1-beta                                          */
8 /* The author of this program offers no waranty at all       */
9 /* about the correct execution of this software material.    */
10 /* Furthermore, the author can NOT be held responsible for   */
11 /* any physical or moral damage caused by the use of this    */
12 /* software.                                                 */
13 /*                                                           */
14 /* This is a standalone Tool to scan for any FlightGear      */
15 /* Deamon.                                                   */
16 /* This is Open Source Software with many parts              */
17 /* shamelessly stolen from others...                         */
18 /*                                                           */
19 /* -> This program will scan for TCP port listening on a     */
20 /*    remote or local host inside the range you give to it.  */
21 /*    I offer no warranty over the accuracy though :)        */
22 /*    There are 3 verbose modes: No info, service info, and  */
23 /*    full info. No info is good of you only want the list   */
24 /*    of the ports, no more info. The best mode is Full      */
25 /*    info, as you get error information,etc. The main       */
26 /*    output is STDOUT, and ALL the errors go to STDERR.     */
27 /*                                                           */
28 /*    History: v0.1pre-alpha: May 25 1999 -> First release   */
29 /*             v0.1-alpha     Nov 08 1999                    */
30 /*             v0.1-beta      Jan 16 2000 libc5/glibc-2.0    */
31 /*                                        glibc-2.1 cleanups */
32 /*************************************************************/
33
34
35 #include <stdio.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
38 #include <netinet/in.h>
39 #include <unistd.h>
40 #include <netdb.h>
41 #include <sys/time.h>
42 #include <fcntl.h>
43 #include <sys/utsname.h>
44
45 int i;
46 int sock = -1;
47 int my_sock;
48 struct sockaddr_in address;
49 struct sockaddr_in my_address;
50 int result;
51 extern int errno;
52 int current_port  = 20000; 
53 u_short base_port = 20000;
54 u_short end_port  = 20100;
55 int verbose = 0;
56 struct hostent *host_info, *f_host_info;
57 struct servent *service_info;
58 struct utsname myname;
59
60 size_t anz;
61 char *buff;
62 char *src_host;
63
64 void port_scan( char FGD_com);
65 void fgd_scan();
66
67
68 int main(int argc, char **argv)
69
70    if (argc < 4) {
71     fprintf(stderr,"Usage: fgd_scan [host] [start port] [end port] <-v or -vv>\n");
72     exit(1);
73    }
74    printf("argc %d argv[5] %s\n",argc,argv[5]);
75    verbose = 0;
76    switch (argc) {
77 /*     case 5: base_port = (u_short)atoi(argv[2]);
78              end_port = (u_short)atoi(argv[3]);
79              verbose = 2;
80              src_host = argv[6];
81              break; */
82      case 5: if (!strcmp(argv[4],"-v")) 
83                verbose = 1;
84              else if (!strcmp(argv[4],"-vv"))
85                     verbose = 2;
86                else { fprintf(stderr,"Usage: fgd_scan [host] [start port] [end port] <-v or -vv>\n");
87                       exit(1); }
88
89      case 4: base_port = (u_short)atoi(argv[2]);
90              end_port = (u_short)atoi(argv[3]);
91              break;
92      default: fprintf(stderr,"Usage: fgd_scan [host] [start port] [end port] <-v or -vv>\n");
93               exit(1);
94               break;
95    }
96    
97    bzero((char *)&address, sizeof(address));
98    address.sin_family = AF_INET;
99 /* determinating the source/sending host */
100    if (uname(&myname) == 0) src_host = myname.nodename;
101    printf("I'm running on HOST : %s\n", src_host);
102 /* resolving the destination host, here: fgd's host */   
103    if (verbose == 2) printf("Resolving: %s ->",argv[1]);
104    if (host_info = gethostbyname(argv[1])) {
105      bcopy(host_info->h_addr, (char *)&address.sin_addr,host_info->h_length);
106      if (verbose == 2) printf(" resolved\n");
107    } else if ((address.sin_addr.s_addr = inet_addr(argv[1])) == INADDR_NONE) {
108             fprintf(stderr,"Could not get %s host entry !\n",argv[1]);
109             printf(" NOT resolved !!!\n");
110             exit(1);
111           } else if (verbose == 2) printf(" address valid\n");
112    
113    if ((base_port > end_port) || ((short)base_port < 0)) { 
114      fprintf(stderr,"Bad port range : start=%d end=%d !\n");
115      exit(1);
116    } else if (verbose == 2) {
117             printf("Port range: %d to %d\n",base_port,end_port);
118      }
119      
120    fgd_scan();
121    exit(0);
122 }
123
124 int fgd_len_msg = 1;
125
126
127 /******* HERE SCAN ROUTINE *******/
128
129 void fgd_scan() {
130    current_port = base_port;
131    printf("Scanning for fgd...\n");
132    while (current_port <= end_port) {
133      fprintf(stderr,"Trying port: %d\n",current_port); 
134      sock = socket(PF_INET, SOCK_STREAM, 0);
135      if (sock == -1) {
136         fprintf(stderr, "Error assigning master socket: %s\n",sys_errlist[errno]);
137         exit(-1);
138      } 
139      address.sin_port = htons(current_port);
140      printf("address.sin_port : %d\n",htons(address.sin_port));
141      f_host_info = gethostbyname(src_host);
142
143 /* printf ("src_host : %s", ntohs(f_host_info->h_addr)); */
144     
145     if (connect(sock, (struct sockaddr *)&address, sizeof(address)) == 0) {
146
147 /* we determine length of our ip */
148      fgd_len_msg = (int) sizeof(f_host_info->h_addr);
149 /* first we send length of ip */
150      write( sock, &fgd_len_msg,1);
151 /* then we send our ip */     
152      write( sock, f_host_info->h_addr, fgd_len_msg);
153 /* we send the command, here 0 : we ask fgd to identify itself */
154      write( sock, "0", 1);
155      printf("verbose: %d", verbose);
156
157      printf(" Inquiring FGD to identify itself\n");
158      switch (verbose) {
159       case 0: printf("%d\n",current_port);
160               break;
161       case 1: service_info = getservbyport(htons(current_port),"tcp");
162               if (!service_info) {
163               printf("%d -> service name unknown\n",current_port);
164               } else {
165               printf("%d -> %s\n",current_port,service_info->s_name);
166               }
167               break; 
168       case 2: service_info = getservbyport(htons(current_port),"tcp");
169               if (!service_info) {
170                 read( sock, &buff, 4);
171                 printf(" Got reply : %s\n", &buff);
172                 if (strncmp(&buff, "FGD", 3) == 0) {
173                   read( sock, &buff, (int) &buff[3]);
174                   printf(" FlightGear-Deamon %s detected.\n", &buff);
175                   break;
176                 }
177               printf("Port %d found. Service name unknown\n",current_port);
178               } else {
179               printf("Port %d found. Service name: %s\n",current_port,service_info->s_name);
180               }
181               break; 
182      } 
183     }  else if (errno == 113) {
184          fprintf(stderr,"No route to host !\n");
185          exit(1);
186        } 
187 /*     fprintf(stderr,"Error %d connecting socket %d to port %d: %s\n",
188      errno,sock,current_port,sys_errlist[errno]); */ 
189      close(sock);
190      current_port++;
191    }
192
193   if (verbose == 2) printf("FGD scan finished !\n");
194 }
195