1 /***********************************************************/
2 /* FGD_SCAN.C by Oliver Delise */
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 */
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 */
15 /* This is a standalone Tool to scan for any FlightGear */
17 /* This is Open Source Software with many parts */
18 /* shamelessly stolen from others... */
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. */
29 /* History: v0.1pre-alpha: May 25 1999 -> First release */
30 /***********************************************************/
34 #include <sys/socket.h>
35 #include <sys/types.h>
36 #include <netinet/in.h>
41 #include <sys/utsname.h>
46 struct sockaddr_in address;
47 struct sockaddr_in my_address;
49 extern char *sys_errlist[];
51 int current_port = 20000;
52 u_short base_port = 20000;
53 u_short end_port = 20100;
55 struct hostent *host_info, *f_host_info;
56 struct servent *service_info;
57 struct utsname myname;
63 void port_scan( char FGD_com);
67 int main(int argc, char **argv)
70 fprintf(stderr,"Usage: fgd_scan [host] [start port] [end port] <-v or -vv>\n");
73 printf("argc %d argv[5] %s\n",argc,argv[5]);
76 /* case 5: base_port = (u_short)atoi(argv[2]);
77 end_port = (u_short)atoi(argv[3]);
81 case 5: if (!strcmp(argv[4],"-v"))
83 else if (!strcmp(argv[4],"-vv"))
85 else { fprintf(stderr,"Usage: fgd_scan [host] [start port] [end port] <-v or -vv>\n");
88 case 4: base_port = (u_short)atoi(argv[2]);
89 end_port = (u_short)atoi(argv[3]);
91 default: fprintf(stderr,"Usage: fgd_scan [host] [start port] [end port] <-v or -vv>\n");
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");
110 } else if (verbose == 2) printf(" address valid\n");
112 if ((base_port > end_port) || ((short)base_port < 0)) {
113 fprintf(stderr,"Bad port range : start=%d end=%d !\n");
115 } else if (verbose == 2) {
116 printf("Port range: %d to %d\n",base_port,end_port);
126 /******* HERE SCAN ROUTINE *******/
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);
135 fprintf(stderr, "Error assigning master socket: %s\n",sys_errlist[errno]);
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);
142 /* printf ("src_host : %s", ntohs(f_host_info->h_addr)); */
144 if (connect(sock, (struct sockaddr *)&address, sizeof(address)) == 0) {
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);
156 printf(" Inquiring FGD to identify itself\n");
158 case 0: printf("%d\n",current_port);
160 case 1: service_info = getservbyport(htons(current_port),"tcp");
162 printf("%d -> service name unknown\n",current_port);
164 printf("%d -> %s\n",current_port,service_info->s_name);
167 case 2: service_info = getservbyport(htons(current_port),"tcp");
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");
175 printf("Port %d found. Service name unknown\n",current_port);
177 printf("Port %d found. Service name: %s\n",current_port,service_info->s_name);
181 } else if (errno == 113) {
182 fprintf(stderr,"No route to host !\n");
185 /* fprintf(stderr,"Error %d connecting socket %d to port %d: %s\n",
186 errno,sock,current_port,sys_errlist[errno]); */
191 if (verbose == 2) printf("FGD scan finished !\n");