@@ -42,10 +42,11 @@ unsigned short csum(unsigned short *ptr,int nbytes) {
42
42
}
43
43
44
44
int udpWrite (int s , const char * saddr , const char * daddr , short sport , short dport ,
45
- char * buf , bool chksum )
45
+ char * buf , int len , bool chksum )
46
46
{
47
47
// Datagram to represent the packet
48
- char datagram [4096 ] , source_ip [32 ] , * data , * pseudogram ;
48
+ char datagram [4096 ];
49
+ char * data , * pseudogram ;
49
50
// Zero out the packet buffer
50
51
memset (datagram , 0 , 4096 );
51
52
// IP header
@@ -60,8 +61,6 @@ int udpWrite(int s, const char* saddr, const char* daddr, short sport, short dpo
60
61
data = datagram + sizeof (struct iphdr ) + sizeof (struct udphdr );
61
62
strcpy (data , buf );
62
63
63
-
64
- strcpy (source_ip , saddr );
65
64
sin .sin_family = AF_INET ;
66
65
sin .sin_port = htons (80 );
67
66
sin .sin_addr .s_addr = inet_addr (daddr );
@@ -70,13 +69,13 @@ int udpWrite(int s, const char* saddr, const char* daddr, short sport, short dpo
70
69
iph -> ihl = 5 ;
71
70
iph -> version = 4 ;
72
71
iph -> tos = 0 ;
73
- iph -> tot_len = sizeof (struct iphdr ) + sizeof (struct udphdr ) + strlen ( data ) ;
72
+ iph -> tot_len = sizeof (struct iphdr ) + sizeof (struct udphdr ) + len ;
74
73
iph -> id = htons (10 ); //Id of this packet
75
74
iph -> frag_off = 0 ;
76
75
iph -> ttl = 255 ;
77
76
iph -> protocol = IPPROTO_UDP ;
78
77
iph -> check = 0 ; //Set to 0 before calculating checksum
79
- iph -> saddr = inet_addr ( source_ip ); //Spoof the source ip address
78
+ iph -> saddr = inet_addr ( saddr ); //Spoof the source ip address
80
79
iph -> daddr = sin .sin_addr .s_addr ;
81
80
82
81
//Compute checksum if needed
@@ -85,27 +84,27 @@ int udpWrite(int s, const char* saddr, const char* daddr, short sport, short dpo
85
84
//UDP header
86
85
udph -> uh_sport = htons (sport );
87
86
udph -> uh_dport = htons (dport );
88
- udph -> uh_ulen = htons (8 + strlen ( data )) ; //tcp header size
87
+ udph -> uh_ulen = htons (8 + len ) ; //udp header size
89
88
udph -> uh_sum = 0 ; //leave checksum 0 now, filled later by pseudo header
90
89
91
90
//Now the UDP checksum using the pseudo header
92
91
if (chksum ) {
93
- psh .source_address = inet_addr ( source_ip );
92
+ psh .source_address = inet_addr (saddr );
94
93
psh .dest_address = sin .sin_addr .s_addr ;
95
94
psh .placeholder = 0 ;
96
95
psh .protocol = IPPROTO_UDP ;
97
- psh .udp_length = htons (sizeof (struct udphdr ) + strlen ( data ) );
96
+ psh .udp_length = htons (sizeof (struct udphdr ) + len );
98
97
99
- int psize = sizeof (struct pseudo_header ) + sizeof (struct udphdr ) + strlen ( data ) ;
98
+ int psize = sizeof (struct pseudo_header ) + sizeof (struct udphdr ) + len ;
100
99
pseudogram = (char * ) malloc (psize );
101
100
102
101
memcpy (pseudogram , (char * ) & psh , sizeof (struct pseudo_header ));
103
- memcpy (pseudogram + sizeof (struct pseudo_header ) , udph , sizeof (struct udphdr ) + strlen ( data ) );
102
+ memcpy (pseudogram + sizeof (struct pseudo_header ) , udph , sizeof (struct udphdr ) + len );
104
103
udph -> uh_sum = csum ( (unsigned short * ) pseudogram , psize );
105
104
}
106
105
107
106
//Send the packet
108
- if (sendto (s , datagram , iph -> tot_len , 0 , (struct sockaddr * ) & sin , sizeof (sin )) < 0 ) {
107
+ if (sendto (s , data , len , 0 , (struct sockaddr * ) & sin , sizeof (sin )) < 0 ) {
109
108
perror ("sendto failed" );
110
109
return 1 ;
111
110
}
0 commit comments