]> git.mxchange.org Git - flightgear.git/blob - src/NetworkOLK/Tools/fgd_com.c
Code reorganization.
[flightgear.git] / src / NetworkOLK / Tools / fgd_com.c
1 /*************************************************************/
2 /* FGD_COM.C by Oliver Delise                                */
3 /* Contact info:                                             */
4 /* e-mail: delise@mail-isis.de                               */
5 /* www: http://www.isis.de/members/odelise/progs/flightgear  */
6 /*                                                           */
7 /* Version 0.1-beta                                          */
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    */
12 /* software.                                                 */
13 /*                                                           */
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...                         */
18 /*                                                           */
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.     */
27 /*                                                           */
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 /*************************************************************/
33
34 #include <stdio.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
37 #include <netinet/in.h>
38 #include <unistd.h>
39 #include <netdb.h>
40 #include <sys/time.h>
41 #include <fcntl.h>
42 #include <sys/utsname.h>
43
44
45 /* Netstuff */
46 int sock = -1;
47 int my_sock;
48 struct sockaddr_in address;
49 struct sockaddr_in my_address;
50 int result;
51 extern int errno;
52 int current_port = 0; 
53 u_short base_port = 0;
54 u_short end_port = 1024;
55 int verbose = 0;
56 struct hostent *host_info, *f_host_info;
57 struct servent *service_info;
58 struct utsname myname;
59
60 /* Program-stuff */
61 int i;
62 int fgd_len_msg = 1;
63 size_t anz;
64 char *buff;
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";
67
68
69 void fgd_init(void){
70
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");
84             exit(1);
85           } else if (verbose == 2) printf(" address valid\n");
86    
87    if ((base_port > end_port) || ((short)base_port < 0)) { 
88      fprintf(stderr,"Bad port range : start=%d end=%d !\n");
89      exit(1);
90    } else if (verbose == 2) {
91             printf("Port range: %d to %d\n",base_port,end_port);
92             }
93 }
94
95
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);
102     if (sock == -1)
103      {
104         fprintf(stderr, "Error assigning master socket: %s\n",sys_errlist[errno]);
105         exit(-1);
106      } 
107
108     address.sin_port = htons(current_port);
109     printf("address.sin_port : %d\n",htons(address.sin_port));
110
111     f_host_info = gethostbyname(src_host);
112
113 //printf ("src_host : %s", ntohs(f_host_info->h_addr));
114     
115     if (connect(sock, (struct sockaddr *)&address, sizeof(address)) == 0) {
116
117 //     write( sock, FGD_com, 1);
118
119      fgd_len_msg = (int) sizeof(f_host_info->h_addr);
120 /* send length of sender-ip */
121      write( sock, &fgd_len_msg,1);
122 /* send sender-ip */
123      write( sock, f_host_info->h_addr, fgd_len_msg);
124 /* send commando */     
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);
131    
132 /* send dummy-string, for the moment with _WHO_ to execute commando 
133    here: his ip  */   
134      write( sock, f_host_info->h_addr, fgd_len_msg);
135
136      printf(" Message : %s\n", FGD_com);
137      switch (verbose) {
138       case 0: printf("%d\n",current_port);
139               break;
140       case 1: service_info = getservbyport(htons(current_port),"tcp");
141               if (!service_info) {
142               printf("%d -> service name unknown\n",current_port);
143               } else {
144               printf("%d -> %s\n",current_port,service_info->s_name);
145               }
146               break; 
147       case 2: service_info = getservbyport(htons(current_port),"tcp");
148               if (!service_info) {
149               printf("Port %d found. Service name unknown\n",current_port);
150               } else {
151               printf("Port %d found. Service name: %s\n",current_port,service_info->s_name);
152               }
153               break; 
154      } 
155     }  else if (errno == 113) {
156          fprintf(stderr,"No route to host !\n");
157          exit(1);
158        } 
159 /*     fprintf(stderr,"Error %d connecting socket %d to port %d: %s\n",
160      errno,sock,current_port,sys_errlist[errno]); */ 
161      close(sock);
162      current_port++;
163    }
164
165   if (verbose == 2) printf("fgd_com terminated.\n");
166 }
167
168
169 int main(int argc, char **argv) { 
170
171    if (argc < 6) {
172     fprintf(stderr, usage);
173     exit(1);
174    }
175    printf("argc %d argv[5] %s\n",argc,argv[5]);
176    switch (argc) {
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]);
180              fgd_host = argv[1];
181              verbose = 2;
182              break;
183      case 5: if (!strcmp(argv[4],"-v")) 
184                verbose = 1;
185              else if (!strcmp(argv[4],"-vv"))
186                     verbose = 2;
187                else { fprintf(stderr, usage);
188                       exit(1); }
189
190      case 4: base_port = (u_short)atoi(argv[2]);
191              end_port = (u_short)atoi(argv[3]);
192              break;
193      default: fprintf(stderr, usage);
194               exit(1);
195               break;
196    }
197    fgd_init();
198    fgd_send_com( argv[5], argv[6]);
199    exit(0);
200 }