1 /* The New routine for getting ips and interfaces for /hostname
2 Phear MHacker ;)
3
4 */
5
6 /* added rev dns support
7 Phear sideshow ;)
8
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <netdb.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #if defined(sun)
24 #include <sys/sockio.h>
25 #else
26 #include <sys/sysctl.h>
27 #endif
28 #include <sys/time.h>
29 #include <net/if.h>
30 #include <netinet/in.h>
31 #if !defined(linux)
32 #include <netinet/in_var.h>
33 #endif
34 #include <netdb.h>
35
36 void check_inter (char *, char **);
37 void usage (char **);
38
main(int argc,char ** argv)39 int main (int argc, char **argv)
40 {
41 int s;
42 char *buffer;
43 struct ifconf ifc;
44 char name[100];
45 struct ifreq *ifptr, *end;
46 struct ifreq ifr;
47 int ifflags, selectflag = -1;
48 int oldbufsize, bufsize = sizeof (struct ifreq);
49 if(!argv[1])
50 {
51 usage(argv);
52 exit(0);
53 }
54 printf ("This program should print out valid hosts on all network devices\n");
55 s = socket (AF_INET, SOCK_DGRAM, 0);
56 if (s < 0)
57 {
58 perror ("ifconfig: socket");
59 exit (1);
60 }
61 buffer = malloc (bufsize);
62 ifc.ifc_len = bufsize;
63 do
64 {
65 oldbufsize = ifc.ifc_len;
66 bufsize += 1 + sizeof (struct ifreq);
67 buffer = realloc ((void *) buffer, bufsize);
68 ifc.ifc_len = bufsize;
69 ifc.ifc_buf = buffer;
70 if (ioctl (s, SIOCGIFCONF, (char *) &ifc) < 0)
71 {
72 perror ("ifconfig (SIOCGIFCONF)");
73 exit (1);
74 }
75 }
76 while (ifc.ifc_len > oldbufsize);
77 ifflags = ifc.ifc_req->ifr_flags;
78 end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
79 ifptr = ifc.ifc_req;
80 while (ifptr < end)
81 {
82 sprintf (ifr.ifr_name, "%s", ifptr->ifr_name);
83 sprintf (name, "%s", ifptr->ifr_name);
84 close (s);
85 check_inter (name, argv);
86 ifptr++;
87 }
88 return 0;
89 }
90
91 void
check_inter(char * interface,char ** argv)92 check_inter (char *interface, char **argv)
93 {
94 struct in_addr i;
95 struct hostent *he;
96 struct ifreq ifr;
97 char rhost[256], fhost[30];
98 int fd;
99 char *ip;
100 register int flags;
101 ip = malloc (sizeof (ip));
102 if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
103 return;
104 strcpy (ifr.ifr_name, interface);
105
106 if (ioctl (fd, SIOCGIFADDR, &ifr) < 0)
107 {
108 close (fd);
109 return;
110 }
111 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
112 {
113 close (fd);
114 return;
115 }
116 ip = (char *) inet_ntoa (((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr);
117 if (ifr.ifr_flags & IFF_UP)
118 {
119 i.s_addr = inet_addr (ip);
120 he = gethostbyaddr ((char *) &i, sizeof (struct in_addr), AF_INET);
121 bzero(rhost, sizeof(rhost));
122 if (he != NULL)
123 {
124 strncpy (rhost, he->h_name, 255);
125 he = gethostbyname (rhost);
126 if (he != NULL)
127 sprintf (fhost, "%u.%u.%u.%u", he->h_addr[0] & 0xff, he->h_addr[1] & 0xff, he->h_addr[2] & 0xff, he->h_addr[3] & 0xff);
128
129 }
130 if (rhost)
131 {
132 if (strcasecmp (ip, fhost) == 0)
133 {
134 if ((strcmp ("-m", argv[1]) == 0) || (strcmp ("-a", argv[1]) == 0))
135 {
136 printf ("Interface %s: %s %s (matching)\n", interface, ip, rhost);
137 bzero (rhost, sizeof (rhost));
138 bzero (fhost, sizeof (fhost));
139 }
140 }
141 else if ((strcmp("-r", argv[1])==0) || (strcmp("-a", argv[1]) == 0))
142 {
143 printf ("Interface %s: %s %s\n", interface, ip, rhost);
144 bzero (rhost, sizeof (rhost));
145 }
146 }
147 else if ((strcmp("-i", argv[1]) ==0) || (strcmp("-a", argv[1]) == 0))
148 printf ("Interface %s: %s\n", interface, ip);
149 }
150 close (fd);
151 }
152
usage(char ** argv)153 void usage(char **argv)
154 {
155 printf("\nVirtual Host checking system by Warren Rees\n");
156 printf("Support for reverse dns and dns matching by Matt Watson\n\n");
157 printf("Usage: %s [-amri]\n\n", argv[0]);
158 printf("\t-a : Show all ips\n");
159 printf("\t-m : Show only ips & hostname with matching forward and reverse dns\n");
160 printf("\t-r : Show only ips that have reverse dns but no forward dns\n");
161 printf("\t-i : Show only non-resolving ips no forward or reverse dns\n\n\n");
162 }
163
164