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