summaryrefslogtreecommitdiff
path: root/other/3wahas/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/3wahas/network.c')
-rw-r--r--other/3wahas/network.c258
1 files changed, 258 insertions, 0 deletions
diff --git a/other/3wahas/network.c b/other/3wahas/network.c
new file mode 100644
index 0000000..0243306
--- /dev/null
+++ b/other/3wahas/network.c
@@ -0,0 +1,258 @@
1
2/* zodiac - advanced dns spoofer
3 *
4 * network primitives
5 *
6 * by scut / teso
7 *
8 * nearly all of this code wouldn't have been possible without w. richard stevens
9 * excellent network coding book. if you are interested in network coding,
10 * there is no way around it.
11 */
12
13#include <sys/types.h>
14#include <sys/ioctl.h>
15#include <sys/socket.h>
16#include <sys/time.h>
17#include <arpa/inet.h>
18#include <netdb.h>
19#include <net/if.h>
20#include <netinet/in.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include "network.h"
29
30
31int
32net_parseip (char *inp, char **ip, unsigned short int *port)
33{
34 int n;
35
36 if (inp == NULL)
37 return (0);
38 if (strchr (inp, ':') == NULL)
39 return (0);
40
41 *ip = calloc (1, 256);
42 if (*ip == NULL)
43 return (0);
44
45 n = sscanf (inp, "%[^:]:%hu", *ip, port);
46 if (n != 2)
47 return (0);
48
49 *ip = realloc (*ip, strlen (*ip) + 1);
50 if (*ip == NULL || (*port < 1 || *port > 65535))
51 return (0);
52
53 return (1);
54}
55
56
57char *
58net_getlocalip (void)
59{
60 struct sockaddr_in pf;
61 char name[255];
62
63 memset (name, '\0', sizeof (name));
64
65 if (gethostname (name, sizeof (name) - 1) == -1) {
66 return (NULL);
67 }
68
69 pf.sin_addr.s_addr = net_resolve (name);
70
71 return (strdup (inet_ntoa (pf.sin_addr)));;
72}
73
74
75void
76net_ifi_free (struct ifi_info *tf)
77{
78 struct ifi_info *ifi, *ifil;
79
80 ifil = NULL;
81 for (ifi = tf; ifi != NULL; ifi = ifi->ifi_next) {
82 if (ifil)
83 free (ifil);
84 if (ifi->ifi_addr)
85 free (ifi->ifi_addr);
86 ifil = ifi;
87 }
88 if (ifil)
89 free (ifil);
90 return;
91}
92
93
94struct ifi_info *
95net_ifi_get (int family, int doaliases)
96{
97 struct ifi_info *ifi, *ifihead, **ifipnext;
98 int sockfd, len, lastlen, flags, myflags;
99 char *ptr, *buf, lastname[IFNAMSIZ], *cptr;
100 struct ifconf ifc;
101 struct ifreq *ifr, ifrcopy;
102 struct sockaddr_in *sinptr;
103
104 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
105 if (sockfd == -1)
106 return (NULL);
107
108 lastlen = 0;
109 len = 100 * sizeof(struct ifreq);
110 for (;;) {
111 buf = malloc(len);
112 if (buf == NULL)
113 return (NULL);
114 ifc.ifc_len = len;
115 ifc.ifc_buf = buf;
116 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
117 if (errno != EINVAL || lastlen != 0)
118 return (NULL);
119 } else {
120 if (ifc.ifc_len == lastlen)
121 break;
122 lastlen = ifc.ifc_len;
123 }
124 len += 10 * sizeof(struct ifreq);
125 free (buf);
126 }
127 ifihead = NULL;
128 ifipnext = &ifihead;
129 lastname[0] = 0;
130
131 for (ptr = buf; ptr < buf + ifc.ifc_len;) {
132 ifr = (struct ifreq *) ptr;
133 if (ifr->ifr_addr.sa_family == AF_INET)
134 len = sizeof(struct sockaddr);
135 ptr += sizeof(ifr->ifr_name) + len;
136
137 if (ifr->ifr_addr.sa_family != family)
138 continue;
139 myflags = 0;
140 if ((cptr = strchr(ifr->ifr_name, ':')) != NULL)
141 *cptr = 0;
142 if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) {
143 if (doaliases == 0)
144 continue;
145 myflags = IFI_ALIAS;
146 }
147 memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
148
149 ifrcopy = *ifr;
150 if (ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy) < 0)
151 return (NULL);
152 flags = ifrcopy.ifr_flags;
153 if ((flags & IFF_UP) == 0)
154 continue;
155
156 ifi = calloc(1, sizeof(struct ifi_info));
157 if (ifi == NULL)
158 return (NULL);
159 *ifipnext = ifi;
160 ifipnext = &ifi->ifi_next;
161 ifi->ifi_flags = flags;
162 ifi->ifi_myflags = myflags;
163 memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME);
164 ifi->ifi_name[IFI_NAME - 1] = '\0';
165
166#ifdef DEBUG
167 printf("got: %s\n", ifi->ifi_name);
168#endif
169
170 switch (ifr->ifr_addr.sa_family) {
171 case AF_INET:
172 sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
173 memcpy(&ifi->ifi_saddr, &sinptr->sin_addr, sizeof(struct in_addr));
174 if (ifi->ifi_addr == NULL) {
175 ifi->ifi_addr = calloc(1, sizeof(struct sockaddr_in));
176 if (ifi->ifi_addr == NULL)
177 return (NULL);
178 memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in));
179 }
180 break;
181 default:
182 break;
183 }
184 }
185 free (buf);
186 return (ifihead);
187}
188
189
190/* partly based on resolv routine from ?
191 */
192
193unsigned long int
194net_resolve (char *host)
195{
196 long i;
197 struct hostent *he;
198
199 if (host == NULL)
200 return (htonl (INADDR_ANY));
201
202 if (strcmp (host, "*") == 0)
203 return (htonl (INADDR_ANY));
204
205 i = inet_addr (host);
206 if (i == -1) {
207 he = gethostbyname (host);
208 if (he == NULL) {
209 return (0);
210 } else {
211 return (*(unsigned long *) he->h_addr);
212 }
213 }
214 return (i);
215}
216
217
218int
219net_printipr (struct in_addr *ia, char *str, size_t len)
220{
221 unsigned char *ipp;
222
223 ipp = (unsigned char *) &ia->s_addr;
224 snprintf (str, len - 1, "%d.%d.%d.%d", ipp[3], ipp[2], ipp[1], ipp[0]);
225
226 return (0);
227}
228
229
230int
231net_printip (struct in_addr *ia, char *str, size_t len)
232{
233 unsigned char *ipp;
234
235 ipp = (unsigned char *) &ia->s_addr;
236 snprintf (str, len - 1, "%d.%d.%d.%d", ipp[0], ipp[1], ipp[2], ipp[3]);
237
238 return (0);
239}
240
241
242int
243net_printipa (struct in_addr *ia, char **str)
244{
245 unsigned char *ipp;
246
247 ipp = (unsigned char *) &ia->s_addr;
248 *str = calloc (1, 256);
249 if (*str == NULL)
250 return (1);
251
252 snprintf (*str, 255, "%3d.%3d.%3d.%3d", ipp[0], ipp[1], ipp[2], ipp[3]);
253 *str = realloc (*str, strlen (*str) + 1);
254
255 return ((*str == NULL) ? 1 : 0);
256}
257
258