1 /*************************************************************/
2 /* FGD_COM.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 communicate with any */
15 /* FlightGear-Deamon. */
16 /* This is Open Source Software with many parts */
17 /* shamelessly stolen from others... */
19 /* -> This program will use a 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 /* glibc2.1 cleanups */
32 /*************************************************************/
35 #include <sys/socket.h>
36 #include <sys/types.h>
37 #include <netinet/in.h>
42 #include <sys/utsname.h>
48 struct sockaddr_in address;
49 struct sockaddr_in my_address;
53 u_short base_port = 0;
54 u_short end_port = 1024;
56 struct hostent *host_info, *f_host_info;
57 struct servent *service_info;
58 struct utsname myname;
65 char *src_host, *fgd_host, fgfs_host;
66 char *usage = "Usage:\n fgd_com [FGD host] [start port] [end port] [-v or -vv] [Commando] [FGFS host]\n";
71 bzero((char *)&address, sizeof(address));
72 address.sin_family = AF_INET;
73 /* determinating the source/sending host */
74 if (uname(&myname) == 0) src_host = myname.nodename;
75 printf("I'm running on HOST : %s\n", src_host);
76 /* resolving the destination host, here fgd's host */
77 if (verbose == 2) printf("Resolving: %s ->", fgd_host);
78 if (host_info = gethostbyname( fgd_host)) {
79 bcopy(host_info->h_addr, (char *)&address.sin_addr,host_info->h_length);
80 if (verbose == 2) printf(" resolved\n");
81 } else if ((address.sin_addr.s_addr = inet_addr( fgd_host)) == INADDR_NONE) {
82 fprintf(stderr,"Could not get %s host entry !\n", fgd_host);
83 printf(" NOT resolved !!!\n");
85 } else if (verbose == 2) printf(" address valid\n");
87 if ((base_port > end_port) || ((short)base_port < 0)) {
88 fprintf(stderr,"Bad port range : start=%d end=%d !\n");
90 } else if (verbose == 2) {
91 printf("Port range: %d to %d\n",base_port,end_port);
96 void fgd_send_com( char *FGD_com, char *FGFS_host) {
97 current_port = base_port;
98 printf("Sending : %s\n", FGD_com);
99 while (current_port <= end_port) {
100 /* fprintf(stderr,"Trying port: %d\n",current_port); */
101 sock = socket(PF_INET, SOCK_STREAM, 0);
104 fprintf(stderr, "Error assigning master socket: %s\n",sys_errlist[errno]);
108 address.sin_port = htons(current_port);
109 printf("address.sin_port : %d\n",htons(address.sin_port));
111 f_host_info = gethostbyname(src_host);
113 //printf ("src_host : %s", ntohs(f_host_info->h_addr));
115 if (connect(sock, (struct sockaddr *)&address, sizeof(address)) == 0) {
117 // write( sock, FGD_com, 1);
119 fgd_len_msg = (int) sizeof(f_host_info->h_addr);
120 /* send length of sender-ip */
121 write( sock, &fgd_len_msg,1);
123 write( sock, f_host_info->h_addr, fgd_len_msg);
125 write( sock, FGD_com, 1);
126 /* send length of dummy-string, for the moment with _WHO_ to execute commando
127 here: his length of ip */
128 f_host_info = gethostbyname(FGFS_host);
129 fgd_len_msg = (int) sizeof(f_host_info->h_addr);
130 write( sock, &fgd_len_msg,1);
132 /* send dummy-string, for the moment with _WHO_ to execute commando
134 write( sock, f_host_info->h_addr, fgd_len_msg);
136 printf(" Message : %s\n", FGD_com);
138 case 0: printf("%d\n",current_port);
140 case 1: service_info = getservbyport(htons(current_port),"tcp");
142 printf("%d -> service name unknown\n",current_port);
144 printf("%d -> %s\n",current_port,service_info->s_name);
147 case 2: service_info = getservbyport(htons(current_port),"tcp");
149 printf("Port %d found. Service name unknown\n",current_port);
151 printf("Port %d found. Service name: %s\n",current_port,service_info->s_name);
155 } else if (errno == 113) {
156 fprintf(stderr,"No route to host !\n");
159 /* fprintf(stderr,"Error %d connecting socket %d to port %d: %s\n",
160 errno,sock,current_port,sys_errlist[errno]); */
165 if (verbose == 2) printf("fgd_com terminated.\n");
169 int main(int argc, char **argv) {
172 fprintf(stderr, usage);
175 printf("argc %d argv[5] %s\n",argc,argv[5]);
177 case 7: printf("fgd commando : %s\n",argv[5]);
178 base_port = (u_short)atoi(argv[2]);
179 end_port = (u_short)atoi(argv[3]);
183 case 5: if (!strcmp(argv[4],"-v"))
185 else if (!strcmp(argv[4],"-vv"))
187 else { fprintf(stderr, usage);
190 case 4: base_port = (u_short)atoi(argv[2]);
191 end_port = (u_short)atoi(argv[3]);
193 default: fprintf(stderr, usage);
198 fgd_send_com( argv[5], argv[6]);