File: packets.c

package info (click to toggle)
irpas 0.10-4.1
  • links: PTS
  • area: non-free
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 412 kB
  • ctags: 973
  • sloc: ansic: 7,385; makefile: 145
file content (403 lines) | stat: -rw-r--r-- 9,971 bytes parent folder | download | duplicates (2)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* IRPAS project - packet library
 *
 * FX <fx@phenoelit.de>
 * Phenoelit (http://www.phenoelit.de)
 * (c) 2k
 *
 * $Id: packets.c,v 1.2 2001/06/16 18:17:31 fx Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <arpa/inet.h>

#include <protocols.h>
#include <packets.h>


packet_ifconfig_t	packet_ifconfig;


/* pings the host, waits 'timeout' seconds and reports to stdout 
 * depending on the level of 'verbose'
 *
 * returns 0 on OK, -1 on error, 1 on timeout */
int     icmp_ping(struct in_addr *t,int timeout,int verbose) {
#define RESP_ONE	1
#define RESP_MORE	2
    int				sfd;
    struct sockaddr_in  	sin,fromaddr;
    u_char			*tpacket;
    icmp_ping_t			*pingh;
    int				psize;
    u_int16_t			pident;
    int				rc,addrsize,respond=0;
    unsigned long		start_t;
    struct timespec		sleeper = { 0, 10 };


    if ((sfd=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))<0) {
	perror("socket()");
	return(-1);
    }

    rc=1;
    if (setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,(void *)&rc,sizeof(int))!=0) { 
	perror("setsockopt"); 
	return (-1); 
    }

    psize=sizeof(icmp_ping_t);
    tpacket=(u_char *)smalloc(sizeof(icmp_ping_t)
	    +3 /* for my checksum function, which sometimes 
		  steps over the mark */
	    );

    pident=1+(u_int16_t) (65535.0*rand()/(RAND_MAX+1.0));
    /* make up the icmp header */
    pingh=(icmp_ping_t *)tpacket;
    pingh->icmp.type=ICMP_ECHO;
    pingh->icmp.code=0;
    pingh->echo.identifier=htons(pident);
    pingh->icmp.checksum=chksum((u_char *)pingh,psize);

    memset(&sin,0,sizeof(struct sockaddr_in));
    sin.sin_family=AF_INET;
    sin.sin_port=htons(0);
    memcpy(&(sin.sin_addr),t,sizeof(sin.sin_addr));

    if (sendto(sfd,tpacket,psize,0,
                (struct sockaddr *) &sin,
                sizeof(struct sockaddr_in)) <0) {
        perror("sendto()");
        return(-1);
    }

    rc= O_NONBLOCK | fcntl(sfd, F_GETFL);
    fcntl(sfd,F_SETFL,rc);

    memset(&fromaddr,0,sizeof(struct sockaddr_in));
    addrsize=sizeof(struct sockaddr_in);
    start_t=(unsigned long)time(NULL);
    memset(tpacket,0,psize);

    while (start_t+timeout>=time(NULL)) {
	if ((rc=recvfrom(sfd,(u_char *)tpacket,psize,0,
		    (struct sockaddr *)&fromaddr,
		    &addrsize))>=0) {
	    pingh=(icmp_ping_t *)(tpacket+sizeof(iphdr_t));

	    if (pingh->icmp.type==ICMP_ECHOREPLY) {

		if ( /* address has to be the same */
			(!memcmp(&(fromaddr.sin_addr),
			    &(sin.sin_addr),sizeof(sin.sin_addr)))
			&&
			(ntohs(pingh->echo.identifier)==pident)
			){
		    /* normal response */
		    respond=RESP_ONE;
		    if (verbose>1) 
			printf("%s respond ... good\n",
				inet_ntoa(fromaddr.sin_addr));
		    break;

		} else if (
			/** SECTION that handles bcast and network addrs **/

			/* address is different - possibly bcast*/
			(memcmp(&(fromaddr.sin_addr),
			    &(sin.sin_addr),sizeof(sin.sin_addr)))
			&&
			(ntohs(pingh->echo.identifier)==pident)
			) {
		    if (verbose) {
			fprintf(stderr,"%s respond instead of ",
				inet_ntoa(fromaddr.sin_addr));
			fprintf(stderr,"%s (broadcast or network address?)\n",
				inet_ntoa(sin.sin_addr));
		    }
		    if (respond==0) respond=RESP_ONE; else respond=RESP_MORE;
		}
		/* it may be an ECHO REPLY, but it's not our's */
	    }
	    /* it's not an ECHO REPLY, therefore it is not OK */
	}

	nanosleep(&sleeper,NULL);
    }

    if (!respond) {
	if (verbose) {
	    printf("%s not responding to ICMP Echo request\n",
		    inet_ntoa(*t));
	}
	free(tpacket);
	close(sfd);
	return 1;
    } else if (respond==RESP_MORE) {
	if (verbose) {
	    printf("%s is broadcast or network\n",
		    inet_ntoa(*t));
	}
	free(tpacket);
	close(sfd);
	return 1;
    } else {
	if (verbose) {
	    printf("%s is alive\n",
		    inet_ntoa(*t));
	}
	free(tpacket);
	close(sfd);
	return 0;
    }

    /* not reached */
    return 0;
}


void	makenonblock(int s) {
    int		rc=1;

    rc= O_NONBLOCK | fcntl(s, F_GETFL);
    fcntl(s,F_SETFL,rc);
}

int	makebcast(int s) {
    int		rc=1;

    if (setsockopt(s,SOL_SOCKET,SO_BROADCAST,(void *)&rc,sizeof(int))!=0) { 
	perror("setsockopt"); 
	return (-1); 
    }
    return(0);
}


/* opens the raw socket,
 * RETURNS 0 on success or -1 on error */
int     init_socket_eth(char *device) {
    int			sfd;
    struct ifreq        ifr;

    if ((sfd=socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL)))<0) {
	perror("socket()");
	return (-1);
    }

    /* get HW addr */
    memset(&ifr,0,sizeof(ifr));
    strncpy(ifr.ifr_name, device, sizeof (ifr.ifr_name));
    if (ioctl(sfd, SIOCGIFHWADDR, &ifr) < 0 ) {
        perror("ioctl()");
        return (-1);
    }
    memcpy(&(packet_ifconfig.eth),&ifr.ifr_hwaddr.sa_data,ETH_ALEN);

    /* grab the IP address */
    memset(&ifr,0,sizeof(ifr));
    strncpy(ifr.ifr_name, device, sizeof (ifr.ifr_name));
    if (ioctl(sfd, SIOCGIFADDR, &ifr) < 0 ) {
	perror("ioctl()");
	return (-1);
    }
    memcpy(&(packet_ifconfig.ip.s_addr),
	    &(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr,
	    IP_ADDR_LEN);

    /* get MTU for size */
    memset(&ifr,0,sizeof(ifr));
    strncpy(ifr.ifr_name, device, sizeof (ifr.ifr_name));
    if (ioctl(sfd, SIOCGIFMTU, &ifr) < 0 ) {
        perror("ioctl()");
        return (-1);
    }
    packet_ifconfig.mtu=ifr.ifr_mtu;

    /* get broadcast addr for size */
    memset(&ifr,0,sizeof(ifr));
    strncpy(ifr.ifr_name, device, sizeof (ifr.ifr_name));
    if (ioctl(sfd, SIOCGIFBRDADDR, &ifr) < 0 ) {
        perror("ioctl()");
        return (-1);
    }
    memcpy(&(packet_ifconfig.bcast.s_addr),
	    &(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr,
	    IP_ADDR_LEN);

    return sfd;
}


/* creates the socket
 * Returns the socket or -1 on error */
int     init_socket_IP4(char *device, int broadcast) {
    int                 sfd;
    struct ifreq        ifr;
    int                 t=1;

    if ((sfd=socket(AF_INET,SOCK_RAW,IPPROTO_RAW))<0) {
        perror("socket()");
        return(-1);
    }

    /* make a broadcast enabled socket if desired */
    if (broadcast) {
	if (setsockopt(
		    sfd,SOL_SOCKET,SO_BROADCAST,
		    (void *)&t,sizeof(int)) != 0) {
	    perror("setsockopt");
	    return (-1);
	}
    }

    /* get HW addr */
    memset(&ifr,0,sizeof(ifr));
    strncpy(ifr.ifr_name, device, sizeof (ifr.ifr_name));
    if (ioctl(sfd, SIOCGIFHWADDR, &ifr) < 0 ) {
        perror("ioctl()");
        return (-1);
    }
    memcpy(&(packet_ifconfig.eth),&ifr.ifr_hwaddr.sa_data,ETH_ALEN);

    /* grab the IP address */
    memset(&ifr,0,sizeof(ifr));
    strncpy(ifr.ifr_name, device, sizeof (ifr.ifr_name));
    if (ioctl(sfd, SIOCGIFADDR, &ifr) < 0 ) {
	perror("ioctl()");
	return (-1);
    }
    memcpy(&(packet_ifconfig.ip.s_addr),
	    &(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr,
	    IP_ADDR_LEN);

    /* get MTU for size */
    memset(&ifr,0,sizeof(ifr));
    strncpy(ifr.ifr_name, device, sizeof (ifr.ifr_name));
    if (ioctl(sfd, SIOCGIFMTU, &ifr) < 0 ) {
        perror("ioctl()");
        return (-1);
    }
    packet_ifconfig.mtu=ifr.ifr_mtu;

    /* get broadcast addr for size */
    memset(&ifr,0,sizeof(ifr));
    strncpy(ifr.ifr_name, device, sizeof (ifr.ifr_name));
    if (ioctl(sfd, SIOCGIFBRDADDR, &ifr) < 0 ) {
        perror("ioctl()");
        return (-1);
    }
    memcpy(&(packet_ifconfig.bcast.s_addr),
	    &(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr,
	    IP_ADDR_LEN);

    return sfd;
}



/* sends out a raw IP packet 
 * returns 0 on success or -1 on error */
int     sendpack_IP4(int sfd, u_char *packet,int plength) {
    struct sockaddr_in  sin;
    iphdr_t             *iph;

    iph=(iphdr_t *)packet;

    memset(&sin,0,sizeof(struct sockaddr_in));
    sin.sin_family=AF_INET;
    sin.sin_port=htons(0);
    memcpy(&(sin.sin_addr),&(iph->daddr),sizeof(sin.sin_addr));

    if (sendto(sfd,packet,plength,0,
                (struct sockaddr *) &sin,
                sizeof(struct sockaddr_in)) <=0) {
        perror("sendto()");
        return(-1);
    }

    return 0;
}


/* send's the ethernet frame,
 * RETURNS the number of octets send or -1 on error */
int     sendpack_eth(char *device, int atsock, 
		u_char *frame, int frame_length) {
    struct sockaddr     sa;
    int                 sendBytes;

    memset(&sa,0,sizeof(sa));
    strncpy(sa.sa_data,device,sizeof(sa.sa_data));

    sendBytes=sendto(atsock,frame,frame_length,0,&sa,sizeof(sa));
    if (sendBytes<0) {
        perror("send_ethernet_frame(): sendto");
        return (-1);
    } else if (sendBytes<frame_length) {
#ifdef __DEBUG__
        fprintf(stderr,"send_ethernet_frame(): "
                "WARNING: short send %d out off %d\n",sendBytes,frame_length);
#endif /* __DEBUG__ */
    }

    return sendBytes;
}


/* returns an initialized pointer to a memory area 
 * or hard-exits on failure */
void    *smalloc(size_t size) {
    void        *p;

    if ((p=malloc(size))==NULL) {
        fprintf(stderr,"smalloc(): malloc failed\n");
        exit (-2);
    }
    memset(p,0,size);
    return p;
}


/* returns the checksum 
 * WARNING: if left over bytes are present, the memory after *data has to
 * contain 0x00 series and should be part of the buffer
 * -> make the buffer for data at least count+1 bytes long ! */
u_int16_t chksum(u_char *data, unsigned long count) {
    u_int32_t           sum = 0;
    u_int16_t           *wrd;

    wrd=(u_int16_t *)data;
    while( count > 1 )  {
        sum = sum + *wrd;
        wrd++;
        count -= 2;
    }

    /*  Add left-over byte, if any */
    if( count > 0 ) {
#ifdef __DEBUG__
            printf("Left over byte: %04X\n",((*wrd & 0xFF)<<8));
#endif /* __DEBUG__ */
        sum = sum + ((*wrd &0xFF)<<8);
    }

    /*  Fold 32-bit sum to 16 bits */
    while (sum>>16) {
        sum = (sum & 0xffff) + (sum >> 16);
    }

    return (~sum);
}