File: utils.c

package info (click to toggle)
xprobe 0.0.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 496 kB
  • ctags: 105
  • sloc: sh: 1,507; ansic: 1,442; makefile: 90
file content (265 lines) | stat: -rw-r--r-- 6,793 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* $Id: utils.c,v 1.11 2001/10/14 17:58:30 fygrave Exp $ */
/*
** Copyright (C) 2001 Fyodor Yarochkin <fygrave@tigerteam.net>,
**                    Ofir Arkin       <ofir@sys-security.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** All material for nonprofit, educational use only.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "xprobe.h"

int resolve_target(char *host, struct sockaddr_in * sa) {

    struct hostent *host_serv;

    bzero(sa, sizeof(struct sockaddr_in));
    sa->sin_family = AF_INET;

    if ((sa->sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
    {
	host_serv = gethostbyname(host);
	if (host_serv == NULL)
	    return -1;
	bcopy(host_serv->h_addr, &(sa->sin_addr), host_serv->h_length);
    }
    return 0;
}

int parse_hostname(char *trghost, hstnet_t *targ) {
    struct sockaddr_in to;
    char *host;
    char *mask;

    host = strdup(trghost);
    if((mask=index(host,'/'))!=NULL) {
		*mask='\0';
		mask++;
        if ((targ->netmask = parsemask(mask)) == 0) {
            free(host);
            return -1;
        }
    } else {
        targ->netmask = 0xffffffff;
    }

    if (resolve_target(host, &to)<0) {
        herror("Can not resolve");
        free(host);
        return -1;
    }
    bcopy((void *)&to.sin_addr,
          (void *)&targ->addr, sizeof(struct in_addr));
    targ->addr &= targ->netmask; 
    free(host);
    return 0;
}

unsigned long parsemask(char *mask) {

    if (!mask) return 0;
    if (!atoi(mask) || atoi(mask) > 32) {
        fprintf(stderr,"Incorrect netmask specification: %s "
                "(1-32 allowed)\n", mask);
        return 0;
    }
    return htonl((0xffffffff<<(32-atoi(mask))));
}

/*
 * Checksum routine for Internet Protocol family headers (C Version)
 */
unsigned short
in_cksum(addr, len)
	unsigned short *addr;
	int len;
{
	register int nleft = len;
	register unsigned short *w = addr;
	register unsigned short answer;
	register int sum = 0;

	/*
	 *  Our algorithm is simple, using a 32 bit accumulator (sum),
	 *  we add sequential 16 bit words to it, and at the end, fold
	 *  back all the carry bits from the top 16 bits into the lower
	 *  16 bits.
	 */
	while (nleft > 1)  {
		sum += *w++;
		nleft -= 2;
	}

	/* mop up an odd byte, if necessary */
	if (nleft == 1)
		sum += *(u_char *)w;

	/*
	 * add back carry outs from top 16 bits to low 16 bits
	 */
	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
	sum += (sum >> 16);			/* add carry */
	answer = ~sum;				/* truncate to 16 bits */
	return (answer);
}

void tree_message(const char *format,...) {
    va_list ap;

    if (!verbose) return;
    va_start(ap, format);
    fprintf(logfn,"TREE: ");
    vfprintf(logfn, format, ap);
    fprintf(logfn,"\n");
    if (logfn != stderr) {    
        fprintf(stderr,"TREE: ");
        vfprintf(stderr, format, ap);
        fprintf(stderr,"\n");
    }
    va_end(ap);
}

void fin_message(const char *format,...) {
    va_list ap;

    va_start(ap, format);
    fprintf(logfn,"FINAL:[ ");
    vfprintf(logfn, format, ap);
    fprintf(logfn," ]\n");
    if (logfn != stderr) {
        fprintf(stderr,"FINAL:[ ");
        vfprintf(stderr, format, ap);
        fprintf(stderr," ]\n");
    }
    va_end(ap);
}

void log_message(const char *format,...) {
    va_list ap;

    va_start(ap, format);
    fprintf(logfn,"LOG: ");
    vfprintf(logfn, format, ap);
    fprintf(logfn,"\n");
    if (logfn != stderr) {
        fprintf(stderr,"LOG: ");
        vfprintf(stderr, format, ap);
        fprintf(stderr,"\n");
    }
    va_end(ap);
}

void hexdump(unsigned char *ptr, ssize_t len) {

    if (len<0 || !ptr) return;

    while(len--) 
        fprintf(logfn,"%02x%c", *ptr++ & 0xff,
        len % 16? ' ':'\n');

}

void free_res(rpack_t **rpack) {

    if (!*rpack) return;
    if ((*rpack)->orig_pkt) free((*rpack)->orig_pkt);
    if ((*rpack)->pkt)      free((*rpack)->pkt);
    free(*rpack);
    *rpack = NULL;

}

struct in_addr get_iface_addr(char *iname) {
    struct ifreq ifr;
    int sd;
    struct in_addr retval;
    struct sockaddr_in *sin;

    if((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket");
        exit(1);
    }

    strncpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));

    if (ioctl(sd, SIOCGIFADDR,(char *)&ifr) < 0) {
        perror("ioctl(SIOCGIFADDR)");
        close(sd);
        exit(1); /* interface doesn't exist or your kernel is flacky */
    }
    close(sd);
    sin = (struct sockaddr_in *) &ifr.ifr_addr;
    memcpy((void *)&retval, (void *)&(sin->sin_addr.s_addr), sizeof(retval));
    return retval;
 
}

/* udp packet checksum calculation.. length _SHOULD_ include udpheader
 * lenghth
 */
unsigned short udp_cksum_calc(struct ip *ip, struct udphdr *udp, unsigned short length) {
     ipovly_t  *ipov;
     unsigned char *buf;
     struct udphdr *ludp;
     unsigned short cksum;

     if((buf=calloc(sizeof(ipovly_t) + length, 1))== NULL) {
         perror("calloc");
         return -1;
     }
     memcpy((void *)(buf + sizeof(ipovly_t)), (void *)udp, length);
     ipov = (ipovly_t *)buf;
     ludp = (struct udphdr *)(ipov + 1);
     ipov->ih_src = ip->ip_src;
     ipov->ih_dst = ip->ip_dst;
     ipov->ih_pr  = ip->ip_p;
     ipov->ih_len = htons(length);
     ludp->uh_sum = 0;
     cksum = in_cksum((unsigned short *)buf, length);
     if (!cksum)
         cksum = 0xffff;
     return cksum;
}

void make_ippkt(struct ip *ip,
                unsigned char proto, 
                struct in_addr srcaddr,
                struct in_addr dstaddr,
                unsigned short pktlen,
                unsigned short ip_off,
                unsigned short ip_tos) {


    ip->ip_p     = proto;
    ip->ip_src   = srcaddr;
    ip->ip_dst   = dstaddr;
#if defined(__OpenBSD__)    
    ip->ip_len   = htons(pktlen);
#else    
    ip->ip_len   = pktlen;
#endif    
    ip->ip_ttl   = 250;
    ip->ip_v     = IP_VERSION;
    ip->ip_hl    = sizeof(struct ip) >> 2;
#if defined(__linux__) || defined(__OpenBSD__)    
    ip->ip_off   = htons((unsigned short) ip_off);
#else
    ip->ip_off   = (unsigned short) ip_off;
#endif    
    ip->ip_tos  = ip_tos; 
    ip->ip_id   = rand();

}