Annotation of embedaddon/hping2/sendip.c, revision 1.1.1.1
1.1 misho 1: /*
2: * $smu-mark$
3: * $name: sendip.c$
4: * $author: Salvatore Sanfilippo <antirez@invece.org>$
5: * $copyright: Copyright (C) 1999 by Salvatore Sanfilippo$
6: * $license: This software is under GPL version 2 of license$
7: * $date: Fri Nov 5 11:55:49 MET 1999$
8: * $rev: 8$
9: */
10:
11: /* $Id: sendip.c,v 1.7 2003/08/01 13:28:07 njombart Exp $ */
12:
13: #include <stdio.h>
14: #include <sys/types.h>
15: #include <sys/socket.h>
16: #include <string.h>
17: #include <errno.h>
18: #include <stdlib.h>
19: #include <unistd.h>
20:
21: #include "hping2.h"
22: #include "globals.h"
23:
24: void send_ip (char* src, char *dst, char *data, unsigned int datalen,
25: int more_fragments, unsigned short fragoff, char *options,
26: char optlen)
27: {
28: char *packet;
29: int result,
30: packetsize;
31: struct myiphdr *ip;
32:
33: packetsize = IPHDR_SIZE + optlen + datalen;
34: if ( (packet = malloc(packetsize)) == NULL) {
35: perror("[send_ip] malloc()");
36: return;
37: }
38:
39: memset(packet, 0, packetsize);
40: ip = (struct myiphdr*) packet;
41:
42: /* copy src and dst address */
43: memcpy(&ip->saddr, src, sizeof(ip->saddr));
44: memcpy(&ip->daddr, dst, sizeof(ip->daddr));
45:
46: /* build ip header */
47: ip->version = 4;
48: ip->ihl = (IPHDR_SIZE + optlen + 3) >> 2;
49: ip->tos = ip_tos;
50:
51: #if defined OSTYPE_FREEBSD || defined OSTYPE_NETBSD || defined OSTYPE_BSDI
52: /* FreeBSD */
53: /* NetBSD */
54: ip->tot_len = packetsize;
55: #else
56: /* Linux */
57: /* OpenBSD */
58: ip->tot_len = htons(packetsize);
59: #endif
60:
61: if (!opt_fragment)
62: {
63: ip->id = (src_id == -1) ?
64: htons((unsigned short) rand()) :
65: htons((unsigned short) src_id);
66: }
67: else /* if you need fragmentation id must not be randomic */
68: {
69: /* FIXME: when frag. enabled sendip_handler shold inc. ip->id */
70: /* for every frame sent */
71: ip->id = (src_id == -1) ?
72: htons(getpid() & 255) :
73: htons((unsigned short) src_id);
74: }
75:
76: #if defined OSTYPE_FREEBSD || defined OSTYPE_NETBSD | defined OSTYPE_BSDI
77: /* FreeBSD */
78: /* NetBSD */
79: ip->frag_off |= more_fragments;
80: ip->frag_off |= fragoff >> 3;
81: #else
82: /* Linux */
83: /* OpenBSD */
84: ip->frag_off |= htons(more_fragments);
85: ip->frag_off |= htons(fragoff >> 3); /* shift three flags bit */
86: #endif
87:
88: ip->ttl = src_ttl;
89: if (opt_rawipmode) ip->protocol = raw_ip_protocol;
90: else if (opt_icmpmode) ip->protocol = 1; /* icmp */
91: else if (opt_udpmode) ip->protocol = 17; /* udp */
92: else ip->protocol = 6; /* tcp */
93: ip->check = 0; /* always computed by the kernel */
94:
95: /* copies options */
96: if (options != NULL)
97: memcpy(packet+IPHDR_SIZE, options, optlen);
98:
99: /* copies data */
100: memcpy(packet + IPHDR_SIZE + optlen, data, datalen);
101:
102: if (opt_debug == TRUE)
103: {
104: unsigned int i;
105:
106: for (i=0; i<packetsize; i++)
107: printf("%.2X ", packet[i]&255);
108: printf("\n");
109: }
110: result = sendto(sockraw, packet, packetsize, 0,
111: (struct sockaddr*)&remote, sizeof(remote));
112:
113: if (result == -1 && errno != EINTR && !opt_rand_dest && !opt_rand_source) {
114: perror("[send_ip] sendto");
115: if (close(sockraw) == -1)
116: perror("[ipsender] close(sockraw)");
117: #if (!defined OSTYPE_LINUX) || (defined FORCE_LIBPCAP)
118: if (close_pcap() == -1)
119: printf("[ipsender] close_pcap failed\n");
120: #else
121: if (close_sockpacket(sockpacket) == -1)
122: perror("[ipsender] close(sockpacket)");
123: #endif /* ! OSTYPE_LINUX || FORCE_LIBPCAP */
124: exit(1);
125: }
126:
127: free(packet);
128:
129: /* inc packet id for safe protocol */
130: if (opt_safe && !eof_reached)
131: src_id++;
132: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>