Skip to content

Commit c126845

Browse files
committed
Simplify in_cksum() and squelch one more warning.
sn_packets.c:66:32: warning: passing argument 1 of 'in_cksum' from incompatible pointer type [-Wincompatible-pointer-types] Packet data comes as a sequence of bytes rather than 16-bit words, so take is as bytes and reconstruct the words as necessary. Also replace host byte order pointer type casting with explicit network byte order in the words. Lose a few variables as well. Drop "register" because it no longer is necessary with modern compilers and it certainly has nothing to do with endianness. This function may have other problems, which should be easier to see and to fix now. Use unsigned char in a few places to match the new prototype.
1 parent b99fbdd commit c126845

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

src/sn_generation.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ sp_help_ip->TTL = 69;
211211
sp_help_ip->protocol = proto;
212212
sp_help_ip->source = sp->source;
213213
sp_help_ip->destination = sp->dest;
214-
sp_help_ip->checksum=in_cksum((unsigned short *) (sp->buffer),
214+
sp_help_ip->checksum=in_cksum(sp->buffer,
215215
SP_IP_HEAD_BASE+sp->IP_optlen);
216216
#ifdef DEBUG
217217
printf("IP header fixed...\n");
@@ -220,7 +220,7 @@ sp_help_ip->checksum=in_cksum((unsigned short *) (sp->buffer),
220220

221221
static void sp_fix_TCP_packet (struct sp_data_exchange *sp)
222222
{
223-
char sp_pseudo_ip_construct[MTU];
223+
unsigned char sp_pseudo_ip_construct[MTU];
224224
struct TCP_header *sp_help_tcp;
225225
struct pseudo_IP_header *sp_help_pseudo;
226226
int i;
@@ -245,7 +245,7 @@ sp_help_pseudo->protocol = 6;
245245
sp_help_pseudo->TCP_UDP_len = htons(sp->datalen+SP_TCP_HEAD_BASE+sp->TCP_optlen);
246246

247247
memcpy(sp_pseudo_ip_construct+12, sp_help_tcp, sp->TCP_optlen+sp->datalen+SP_TCP_HEAD_BASE);
248-
sp_help_tcp->checksum=in_cksum((unsigned short *) sp_pseudo_ip_construct,
248+
sp_help_tcp->checksum=in_cksum(sp_pseudo_ip_construct,
249249
sp->datalen+12+SP_TCP_HEAD_BASE+sp->TCP_optlen);
250250
#ifdef DEBUG
251251
printf("TCP header fixed...\n");
@@ -260,7 +260,7 @@ static void transmit_TCP (int sp_fd, char *sp_data,
260260
_32_bit sp_seq, _32_bit sp_ack,
261261
unsigned short sp_flags)
262262
{
263-
char sp_buffer[1500];
263+
unsigned char sp_buffer[1500];
264264
struct sp_data_exchange sp_struct;
265265

266266
bzero(sp_buffer,1500);
@@ -295,7 +295,7 @@ sp_send_packet(&sp_struct, 6);
295295

296296
static void sp_fix_UDP_packet (struct sp_data_exchange *sp)
297297
{
298-
char sp_pseudo_ip_construct[MTU];
298+
unsigned char sp_pseudo_ip_construct[MTU];
299299
struct UDP_header *sp_help_udp;
300300
struct pseudo_IP_header *sp_help_pseudo;
301301
int i;
@@ -317,7 +317,7 @@ sp_help_pseudo->protocol = 17;
317317
sp_help_pseudo->TCP_UDP_len = htons(sp->datalen+SP_UDP_HEAD_BASE);
318318

319319
memcpy(sp_pseudo_ip_construct+12, sp_help_udp, sp->datalen+SP_UDP_HEAD_BASE);
320-
sp_help_udp->checksum=in_cksum((unsigned short *) sp_pseudo_ip_construct,
320+
sp_help_udp->checksum=in_cksum(sp_pseudo_ip_construct,
321321
sp->datalen+12+SP_UDP_HEAD_BASE);
322322
#ifdef DEBUG
323323
printf("UDP header fixed...\n");
@@ -329,7 +329,7 @@ static void transmit_UDP (int sp_fd, char *sp_data,
329329
_32_bit sp_source, unsigned short sp_source_port,
330330
_32_bit sp_dest, unsigned short sp_dest_port)
331331
{
332-
char sp_buffer[1500];
332+
unsigned char sp_buffer[1500];
333333
struct sp_data_exchange sp_struct;
334334
bzero(sp_buffer,1500);
335335

src/sn_packets.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,23 @@ extern int PROTO_HEAD;
1111
extern char NO_CHKSUM;
1212

1313
/* This routine stolen from ping.c */
14-
unsigned short in_cksum(unsigned short *addr,int len)
14+
unsigned short in_cksum(const unsigned char *addr,int nleft)
1515
{
16-
register int nleft = len; /* leave this alone.. my opinion is that the */
17-
register unsigned short *w = addr;
18-
/* register is needed to make it work for both */
19-
register int sum = 0; /* BIG and LITTLE endian machines */
20-
unsigned short answer = 0;
21-
/* but then again, who am I to make such statement */
16+
int sum = 0;
2217

2318
while (nleft > 1)
2419
{
25-
sum += *w++;
20+
sum += *addr++ << 8;
21+
sum += *addr++;
2622
nleft -= 2;
2723
}
2824
if (nleft == 1)
2925
{
30-
*(unsigned char *)(&answer) = *(unsigned char *)w ;
31-
sum += answer;
26+
sum += *addr++;
3227
}
3328
sum = (sum >> 16) + (sum & 0xffff);
3429
sum += (sum >> 16);
35-
answer = ~sum;
36-
return(answer);
30+
return(~sum);
3731
}
3832

3933
int unwrap_packet (unsigned char *sp, struct unwrap *unwrapped)

src/sn_packets.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#ifndef _SN_PACKETS_H_
44
#define _SN_PACKETS_H_
55

6-
extern unsigned short in_cksum(unsigned short *,int);
6+
extern unsigned short in_cksum(const unsigned char *,int);
77
extern int unwrap_packet (const unsigned char *, struct unwrap *);
88

99
#endif

src/sn_structs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ struct sp_data_exchange {
8686
_32_bit dest; unsigned short dest_port;
8787
_32_bit seq, ack;
8888
unsigned short flags;
89-
char *buffer; /* work buffer */
89+
unsigned char *buffer; /* work buffer */
9090
int IP_optlen; /* IP options length in bytes */
9191
int TCP_optlen; /* TCP options length in bytes */
9292
};

0 commit comments

Comments
 (0)