]> git.mxchange.org Git - flightgear.git/blob - src/NetworkOLK/Tools/fgd_com.c
Initial revision.
[flightgear.git] / src / NetworkOLK / Tools / fgd_com.c
1 /***********************************************************/
2 /* FGD_COM.C by Oliver Delise                              */
3 /* Contact info:                                           */
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    */ 
7 /*                                                         */
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  */
13 /* software.                                               */
14 /*                                                         */
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...                       */
19 /*                                                         */
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.   */
28 /*                                                         */
29 /*    History: v0.1pre-alpha: May 25 1999 -> First release */
30 /***********************************************************/
31
32 #include <stdio.h>
33 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <unistd.h>
37 #include <netdb.h>
38 #include <sys/time.h>
39 #include <fcntl.h>
40 #include <sys/utsname.h>
41
42 int i;
43 int sock = -1;
44 int my_sock;
45 struct sockaddr_in address;
46 struct sockaddr_in my_address;
47 int result;
48 extern char *sys_errlist[];
49 extern int errno;
50 int current_port = 0; 
51 u_short base_port = 0;
52 u_short end_port = 1024;
53 int verbose = 0;
54 struct hostent *host_info, *f_host_info;
55 struct servent *service_info;
56 struct utsname myname;
57
58 size_t anz;
59 char *buff;
60 char *src_host;
61
62 void port_scan( char *FGD_com, char *FGFS_host);
63
64 int main(int argc, char **argv)
65
66    if (argc < 6) {
67     fprintf(stderr," Usage:\n fgd_com [FGD host] [start port] [end port] [-v or -vv] [Commando] [FGFS host]\n");
68     exit(1);
69    }
70    printf("argc %d argv[5] %s\n",argc,argv[5]);
71    switch (argc) {
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]);
75              verbose = 2;
76 //             src_host = argv[6];
77              break;
78      case 5: if (!strcmp(argv[4],"-v")) 
79                verbose = 1;
80              else if (!strcmp(argv[4],"-vv"))
81                     verbose = 2;
82                else { fprintf(stderr," Usage:\n fgd_com [FGD host] [start port] [end port] <-v or -vv> [FGFS-host]\n");
83                       exit(1); }
84
85      case 4: base_port = (u_short)atoi(argv[2]);
86              end_port = (u_short)atoi(argv[3]);
87              break;
88      default: fprintf(stderr,"Usage:\n fgd_com [FGD host] [start port] [end port] <-v> [FGFS-host]\n");
89               exit(1);
90               break;
91    }
92    
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");
106             exit(1);
107           } else if (verbose == 2) printf(" address valid\n");
108    
109    if ((base_port > end_port) || ((short)base_port < 0)) { 
110      fprintf(stderr,"Bad port range : start=%d end=%d !\n");
111      exit(1);
112    } else if (verbose == 2) {
113             printf("Port range: %d to %d\n",base_port,end_port);
114      }
115    port_scan( argv[5], argv[6]);
116    exit(0);
117 }
118
119 int fgd_len_msg = 1;
120
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);
127     if (sock == -1)
128      {
129         fprintf(stderr, "Error assigning master socket: %s\n",sys_errlist[errno]);
130         exit(-1);
131      } 
132
133     address.sin_port = htons(current_port);
134     printf("address.sin_port : %d\n",htons(address.sin_port));
135
136     f_host_info = gethostbyname(src_host);
137
138 //printf ("src_host : %s", ntohs(f_host_info->h_addr));
139     
140     if (connect(sock, (struct sockaddr *)&address, sizeof(address)) == 0) {
141
142 //     write( sock, FGD_com, 1);
143
144      fgd_len_msg = (int) sizeof(f_host_info->h_addr);
145 /* send length of sender-ip */
146      write( sock, &fgd_len_msg,1);
147 /* send sender-ip */
148      write( sock, f_host_info->h_addr, fgd_len_msg);
149 /* send commando */     
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);
156    
157 /* send dummy-string, for the moment with _WHO_ to execute commando 
158    here: his ip  */   
159      write( sock, f_host_info->h_addr, fgd_len_msg);
160
161      printf(" Message : %s\n", FGD_com);
162      switch (verbose) {
163       case 0: printf("%d\n",current_port);
164               break;
165       case 1: service_info = getservbyport(htons(current_port),"tcp");
166               if (!service_info) {
167               printf("%d -> service name unknown\n",current_port);
168               } else {
169               printf("%d -> %s\n",current_port,service_info->s_name);
170               }
171               break; 
172       case 2: service_info = getservbyport(htons(current_port),"tcp");
173               if (!service_info) {
174               printf("Port %d found. Service name unknown\n",current_port);
175               } else {
176               printf("Port %d found. Service name: %s\n",current_port,service_info->s_name);
177               }
178               break; 
179      } 
180     }  else if (errno == 113) {
181          fprintf(stderr,"No route to host !\n");
182          exit(1);
183        } 
184 /*     fprintf(stderr,"Error %d connecting socket %d to port %d: %s\n",
185      errno,sock,current_port,sys_errlist[errno]); */ 
186      close(sock);
187      current_port++;
188    }
189
190   if (verbose == 2) printf("fgd_com terminated.\n");
191 }
192