1 /***********************************************************/
2 /* FGD_COM.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 communicate with any */
16 /* FlightGear-Deamon. */
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 /***********************************************************/
33 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <netinet/in.h>
40 #include <sys/utsname.h>
45 struct sockaddr_in address;
46 struct sockaddr_in my_address;
48 extern char *sys_errlist[];
51 u_short base_port = 0;
52 u_short end_port = 1024;
54 struct hostent *host_info, *f_host_info;
55 struct servent *service_info;
56 struct utsname myname;
62 void port_scan( char *FGD_com, char *FGFS_host);
64 int main(int argc, char **argv)
67 fprintf(stderr," Usage:\n fgd_com [FGD host] [start port] [end port] [-v or -vv] [Commando] [FGFS host]\n");
70 printf("argc %d argv[5] %s\n",argc,argv[5]);
72 case 7: printf("fgd commando : %s\n",argv[5]);
73 base_port = (u_short)atoi(argv[2]);
74 end_port = (u_short)atoi(argv[3]);
76 // src_host = argv[6];
78 case 5: if (!strcmp(argv[4],"-v"))
80 else if (!strcmp(argv[4],"-vv"))
82 else { fprintf(stderr," Usage:\n fgd_com [FGD host] [start port] [end port] <-v or -vv> [FGFS-host]\n");
85 case 4: base_port = (u_short)atoi(argv[2]);
86 end_port = (u_short)atoi(argv[3]);
88 default: fprintf(stderr,"Usage:\n fgd_com [FGD host] [start port] [end port] <-v> [FGFS-host]\n");
93 bzero((char *)&address, sizeof(address));
94 address.sin_family = AF_INET;
95 /* determinating the source/sending host */
96 if (uname(&myname) == 0) src_host = myname.nodename;
97 printf("I'm running on HOST : %s\n", src_host);
98 /* resolving the destination host, here fgd's host */
99 if (verbose == 2) printf("Resolving: %s ->",argv[1]);
100 if (host_info = gethostbyname(argv[1])) {
101 bcopy(host_info->h_addr, (char *)&address.sin_addr,host_info->h_length);
102 if (verbose == 2) printf(" resolved\n");
103 } else if ((address.sin_addr.s_addr = inet_addr(argv[1])) == INADDR_NONE) {
104 fprintf(stderr,"Could not get %s host entry !\n",argv[1]);
105 printf(" NOT resolved !!!\n");
107 } else if (verbose == 2) printf(" address valid\n");
109 if ((base_port > end_port) || ((short)base_port < 0)) {
110 fprintf(stderr,"Bad port range : start=%d end=%d !\n");
112 } else if (verbose == 2) {
113 printf("Port range: %d to %d\n",base_port,end_port);
115 port_scan( argv[5], argv[6]);
121 void port_scan( char *FGD_com, char *FGFS_host) {
122 current_port = base_port;
123 printf("Sending : %s\n", FGD_com);
124 while (current_port <= end_port) {
125 /* fprintf(stderr,"Trying port: %d\n",current_port); */
126 sock = socket(PF_INET, SOCK_STREAM, 0);
129 fprintf(stderr, "Error assigning master socket: %s\n",sys_errlist[errno]);
133 address.sin_port = htons(current_port);
134 printf("address.sin_port : %d\n",htons(address.sin_port));
136 f_host_info = gethostbyname(src_host);
138 //printf ("src_host : %s", ntohs(f_host_info->h_addr));
140 if (connect(sock, (struct sockaddr *)&address, sizeof(address)) == 0) {
142 // write( sock, FGD_com, 1);
144 fgd_len_msg = (int) sizeof(f_host_info->h_addr);
145 /* send length of sender-ip */
146 write( sock, &fgd_len_msg,1);
148 write( sock, f_host_info->h_addr, fgd_len_msg);
150 write( sock, FGD_com, 1);
151 /* send length of dummy-string, for the moment with _WHO_ to execute commando
152 here: his length of ip */
153 f_host_info = gethostbyname(FGFS_host);
154 fgd_len_msg = (int) sizeof(f_host_info->h_addr);
155 write( sock, &fgd_len_msg,1);
157 /* send dummy-string, for the moment with _WHO_ to execute commando
159 write( sock, f_host_info->h_addr, fgd_len_msg);
161 printf(" Message : %s\n", FGD_com);
163 case 0: printf("%d\n",current_port);
165 case 1: service_info = getservbyport(htons(current_port),"tcp");
167 printf("%d -> service name unknown\n",current_port);
169 printf("%d -> %s\n",current_port,service_info->s_name);
172 case 2: service_info = getservbyport(htons(current_port),"tcp");
174 printf("Port %d found. Service name unknown\n",current_port);
176 printf("Port %d found. Service name: %s\n",current_port,service_info->s_name);
180 } else if (errno == 113) {
181 fprintf(stderr,"No route to host !\n");
184 /* fprintf(stderr,"Error %d connecting socket %d to port %d: %s\n",
185 errno,sock,current_port,sys_errlist[errno]); */
190 if (verbose == 2) printf("fgd_com terminated.\n");