g_ntohs() & friends may be (are ?) macros and may (will ?) eval args more than once ...
svn path=/trunk/; revision=36272
while (sum>>16)
sum = (sum & 0xffff) + (sum >> 16);
while (sum>>16)
sum = (sum & 0xffff) + (sum >> 16);
+ sum = ~sum;
+ return g_htons(sum);
}
/* The CRC32C code is taken from draft-ietf-tsvwg-sctpcsum-01.txt.
}
/* The CRC32C code is taken from draft-ietf-tsvwg-sctpcsum-01.txt.
/* Write UDP header */
if (hdr_udp) {
/* Write UDP header */
if (hdr_udp) {
HDR_UDP.source_port = g_htons(hdr_src_port);
HDR_UDP.dest_port = g_htons(hdr_dest_port);
HDR_UDP.length = g_htons(proto_length);
HDR_UDP.source_port = g_htons(hdr_src_port);
HDR_UDP.dest_port = g_htons(hdr_dest_port);
HDR_UDP.length = g_htons(proto_length);
+ /* Note: g_ntohs()/g_htons() macro arg may be eval'd twice so calc value before invoking macro */
- u = g_ntohs(in_checksum(&pseudoh, sizeof(pseudoh))) +
- g_ntohs(in_checksum(&HDR_UDP, sizeof(HDR_UDP))) +
- g_ntohs(in_checksum(packet_buf, curr_offset));
- HDR_UDP.checksum = g_htons((u & 0xffff) + (u>>16));
- if (HDR_UDP.checksum == 0) /* differenciate between 'none' and 0 */
+ x16 = in_checksum(&pseudoh, sizeof(pseudoh));
+ u = g_ntohs(x16);
+ x16 = in_checksum(&HDR_UDP, sizeof(HDR_UDP));
+ u += g_ntohs(x16);
+ x16 = in_checksum(packet_buf, curr_offset);
+ u += g_ntohs(x16);
+ x16 = (u & 0xffff) + (u>>16);
+ HDR_UDP.checksum = g_htons(x16);
+ if (HDR_UDP.checksum == 0) /* differentiate between 'none' and 0 */
HDR_UDP.checksum = g_htons(1);
fwrite(&HDR_UDP, sizeof(HDR_UDP), 1, output_file);
HDR_UDP.checksum = g_htons(1);
fwrite(&HDR_UDP, sizeof(HDR_UDP), 1, output_file);
/* Write TCP header */
if (hdr_tcp) {
/* Write TCP header */
if (hdr_tcp) {
HDR_TCP.source_port = g_htons(hdr_src_port);
HDR_TCP.dest_port = g_htons(hdr_dest_port);
/* HDR_TCP.seq_num already correct */
HDR_TCP.window = g_htons(0x2000);
HDR_TCP.source_port = g_htons(hdr_src_port);
HDR_TCP.dest_port = g_htons(hdr_dest_port);
/* HDR_TCP.seq_num already correct */
HDR_TCP.window = g_htons(0x2000);
+ /* Note: g_ntohs()/g_htons() macro arg may be eval'd twice so calc value before invoking macro */
- u = g_ntohs(in_checksum(&pseudoh, sizeof(pseudoh))) +
- g_ntohs(in_checksum(&HDR_TCP, sizeof(HDR_TCP))) +
- g_ntohs(in_checksum(packet_buf, curr_offset));
- HDR_TCP.checksum = g_htons((u & 0xffff) + (u>>16));
- if (HDR_TCP.checksum == 0) /* differenciate between 'none' and 0 */
- HDR_TCP.checksum = g_htons(1);
+ x16 = in_checksum(&pseudoh, sizeof(pseudoh));
+ u = g_ntohs(x16);
+ x16 = in_checksum(&HDR_TCP, sizeof(HDR_TCP));
+ u += g_ntohs(x16);
+ x16 = in_checksum(packet_buf, curr_offset);
+ u += g_ntohs(x16);
+ x16 = (u & 0xffff) + (u>>16);
+ HDR_TCP.checksum = g_htons(x16);
+ if (HDR_TCP.checksum == 0) /* differentiate between 'none' and 0 */
+ HDR_TCP.checksum = g_htons(1);
fwrite(&HDR_TCP, sizeof(HDR_TCP), 1, output_file);
}
fwrite(&HDR_TCP, sizeof(HDR_TCP), 1, output_file);
}
/* Write SCTP header */
if (hdr_sctp) {
/* Write SCTP header */
if (hdr_sctp) {
HDR_SCTP.src_port = g_htons(hdr_sctp_src);
HDR_SCTP.dest_port = g_htons(hdr_sctp_dest);
HDR_SCTP.tag = g_htonl(hdr_sctp_tag);
HDR_SCTP.src_port = g_htons(hdr_sctp_src);
HDR_SCTP.dest_port = g_htons(hdr_sctp_dest);
HDR_SCTP.tag = g_htonl(hdr_sctp_tag);
HDR_SCTP.checksum = crc32c((guint8 *)&HDR_SCTP, sizeof(HDR_SCTP), ~0L);
if (hdr_data_chunk)
HDR_SCTP.checksum = crc32c((guint8 *)&HDR_DATA_CHUNK, sizeof(HDR_DATA_CHUNK), HDR_SCTP.checksum);
HDR_SCTP.checksum = crc32c((guint8 *)&HDR_SCTP, sizeof(HDR_SCTP), ~0L);
if (hdr_data_chunk)
HDR_SCTP.checksum = crc32c((guint8 *)&HDR_DATA_CHUNK, sizeof(HDR_DATA_CHUNK), HDR_SCTP.checksum);
- HDR_SCTP.checksum = g_htonl(finalize_crc32c(crc32c(packet_buf, curr_offset, HDR_SCTP.checksum)));
+ /* Note: g_ntohl() macro arg may be eval'd twice so calc value before invoking macro */
+ x32 = finalize_crc32c(crc32c(packet_buf, curr_offset, HDR_SCTP.checksum));
+ HDR_SCTP.checksum = g_htonl(x32);
fwrite(&HDR_SCTP, sizeof(HDR_SCTP), 1, output_file);
}
fwrite(&HDR_SCTP, sizeof(HDR_SCTP), 1, output_file);
}
fprintf(stderr, "Wrote packet of %lu bytes at %u\n", curr_offset, g_ntohl(HDR_TCP.seq_num));
num_packets_written ++;
}
fprintf(stderr, "Wrote packet of %lu bytes at %u\n", curr_offset, g_ntohl(HDR_TCP.seq_num));
num_packets_written ++;
}
-
- HDR_TCP.seq_num = g_htonl(g_ntohl(HDR_TCP.seq_num) + curr_offset);
+ HDR_TCP.seq_num = g_ntohl(HDR_TCP.seq_num) + curr_offset;
+ HDR_TCP.seq_num = g_htonl(HDR_TCP.seq_num);
packet_start += curr_offset;
curr_offset = 0;
packet_start += curr_offset;
curr_offset = 0;