1 /*************************************************************/
2 /* FGD_SCAN.C by Oliver Delise */
4 /* e-mail: delise@mail-isis.de */
5 /* www: http://www.isis.de/members/odelise/progs/flightgear */
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 */
14 /* This is a standalone Tool to scan for any FlightGear */
16 /* This is Open Source Software with many parts */
17 /* shamelessly stolen from others... */
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. */
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 /*************************************************************/
36 #include <sys/socket.h>
37 #include <sys/types.h>
38 #include <netinet/in.h>
43 #include <sys/utsname.h>
48 struct sockaddr_in address;
49 struct sockaddr_in my_address;
52 int current_port = 20000;
53 u_short base_port = 20000;
54 u_short end_port = 20100;
56 struct hostent *host_info, *f_host_info;
57 struct servent *service_info;
58 struct utsname myname;
64 void port_scan( char FGD_com);
68 int main(int argc, char **argv)
71 fprintf(stderr,"Usage: fgd_scan [host] [start port] [end port] <-v or -vv>\n");
74 printf("argc %d argv[5] %s\n",argc,argv[5]);
77 /* case 5: base_port = (u_short)atoi(argv[2]);
78 end_port = (u_short)atoi(argv[3]);
82 case 5: if (!strcmp(argv[4],"-v"))
84 else if (!strcmp(argv[4],"-vv"))
86 else { fprintf(stderr,"Usage: fgd_scan [host] [start port] [end port] <-v or -vv>\n");
89 case 4: base_port = (u_short)atoi(argv[2]);
90 end_port = (u_short)atoi(argv[3]);
92 default: fprintf(stderr,"Usage: fgd_scan [host] [start port] [end port] <-v or -vv>\n");
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");
111 } else if (verbose == 2) printf(" address valid\n");
113 if ((base_port > end_port) || ((short)base_port < 0)) {
114 fprintf(stderr,"Bad port range : start=%d end=%d !\n");
116 } else if (verbose == 2) {
117 printf("Port range: %d to %d\n",base_port,end_port);
127 /******* HERE SCAN ROUTINE *******/
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);
136 fprintf(stderr, "Error assigning master socket: %s\n",sys_errlist[errno]);
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);
143 /* printf ("src_host : %s", ntohs(f_host_info->h_addr)); */
145 if (connect(sock, (struct sockaddr *)&address, sizeof(address)) == 0) {
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);
157 printf(" Inquiring FGD to identify itself\n");
159 case 0: printf("%d\n",current_port);
161 case 1: service_info = getservbyport(htons(current_port),"tcp");
163 printf("%d -> service name unknown\n",current_port);
165 printf("%d -> %s\n",current_port,service_info->s_name);
168 case 2: service_info = getservbyport(htons(current_port),"tcp");
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);
177 printf("Port %d found. Service name unknown\n",current_port);
179 printf("Port %d found. Service name: %s\n",current_port,service_info->s_name);
183 } else if (errno == 113) {
184 fprintf(stderr,"No route to host !\n");
187 /* fprintf(stderr,"Error %d connecting socket %d to port %d: %s\n",
188 errno,sock,current_port,sys_errlist[errno]); */
193 if (verbose == 2) printf("FGD scan finished !\n");