Skip to content

Some improvements, fix compiler warnings #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ dhtest
*.o
00:*
dhscript_log.txt
compile_commands.json
chksum_test
.cache/
21 changes: 16 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# Makefile to generate dhtest

CC=gcc
#CFLAGS=-Wall -g
CC = gcc
CFLAGS ?= -Wall -Wextra

dhtest: dhtest.o functions.o
$(CC) $(LDFLAGS) dhtest.o functions.o -o dhtest
dhtest: dhtest.o functions.o chksum.o
$(CC) $(LDFLAGS) $^ -o dhtest

chksum_test: chksum.o functions_test.o dhcp_err.o
$(CC) $(LDFLAGS) $^ -o chksum_test

.PHONY: test
test: chksum_test
./chksum_test

.PHONY: debug
debug: chksum_test
gdb --args ./chksum_test

clean:
rm -f dhtest functions.o dhtest.o
rm -f dhtest chksum_test *.o
35 changes: 35 additions & 0 deletions chksum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "chksum.h"

#include <arpa/inet.h>

#include <stdint.h>
#include <stdio.h>

/*
* TCP/UDP checksum function, RFC 768
*/
u_int16_t l4_sum(u_int16_t *buff, int words, u_int32_t *srcaddr, u_int32_t *dstaddr, u_int16_t proto, u_int16_t len)
{
unsigned int last_word;

/* Checksum enhancement - Support for odd byte packets */
if((htons(len) % 2) == 1) {
last_word = *((u_int8_t *)buff + ntohs(len) - 1);
last_word = htons(last_word << 8);
} else {
/* Original checksum function */
last_word = 0;
}

uint32_t sum = 0;
for(int i = 0; i < words; i++){
sum = sum + *(buff + i);
}

sum = sum + last_word;
/* pseudo IPv4 header */
sum = sum + (*(srcaddr) & 0xffff) + (*(srcaddr) >> 16) + (*(dstaddr) & 0xffff) + (*(dstaddr) >> 16) + proto + len;
/* carry-out bits */
sum = (sum >> 16) + sum;
return ~sum;
}
8 changes: 8 additions & 0 deletions chksum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef CHKSUM_H
#define CHKSUM_H

#include<sys/types.h>

u_int16_t l4_sum(u_int16_t *buff, int words, u_int32_t *srcaddr, u_int32_t *dstaddr, u_int16_t proto, u_int16_t len);

#endif /* CHKSUM_H */
40 changes: 40 additions & 0 deletions dhcp_err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Frame (305 bytes) */
const unsigned char pkt410[271] = {
0x00, 0x44, 0x00, 0x43, 0x01, 0x0f, /* .D.C.. */
/* 0x1d, 0xff, 0x01, 0x01, 0x06, 0x00, 0x2b, 0x01, */ /* ......+. */
0x00, 0x00, 0x01, 0x01, 0x06, 0x00, 0x2b, 0x01, /* ......+. */
0xd8, 0x97, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, /* ......B. */
0x05, 0x01, 0x00, 0xcc, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, /* ......c. */
0x53, 0x63, 0x35, 0x01, 0x03, 0x32, 0x04, 0x0a, /* Sc5..2.. */
0x00, 0x72, 0x43, 0x36, 0x04, 0xac, 0x10, 0x2a, /* .rC6...* */
0x13, 0x37, 0x05, 0x01, 0x1c, 0x03, 0x0f, 0x06, /* .7...... */
0xff /* . */
};

6 changes: 6 additions & 0 deletions dhcp_err.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef DHCP_ERR_H
#define DHCP_ERR_H

extern const unsigned char pkt410[271];

#endif /* DHCP_ERR_H */
4 changes: 2 additions & 2 deletions dhtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ int main(int argc, char *argv[])
}
u_int32_t hex_length = (strlen((const char *) option55_req_list_tmp)/2);

int tmp, index = 0;
for(tmp = 0; tmp < hex_length; tmp++) {
int index = 0;
for(u_int32_t tmp = 0; tmp < hex_length; tmp++) {
sscanf(&option55_req_list_tmp[index], "%2X", (unsigned int*)&option55_req_list[tmp]);
index = index + 2;
}
Expand Down
51 changes: 12 additions & 39 deletions functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include<linux/if_ether.h>
#include<linux/if_arp.h>
#include "headers.h"
#include "chksum.h"

//Defined in dhtest.c
extern int sock_packet;
Expand Down Expand Up @@ -539,9 +540,9 @@ int set_rand_dhcp_xid()
*/
u_int16_t ipchksum(u_int16_t *buff, int words)
{
unsigned int sum, i;
unsigned int sum;
sum = 0;
for(i = 0;i < words; i++){
for(int i = 0;i < words; i++){
sum = sum + *(buff + i);
}
sum = (sum >> 16) + sum;
Expand All @@ -553,56 +554,29 @@ u_int16_t ipchksum(u_int16_t *buff, int words)
*/
u_int16_t icmpchksum(u_int16_t *buff, int words)
{
unsigned int sum, i;
unsigned int sum;
unsigned int last_word = 0;
/* Checksum enhancement for odd packets */
if((icmp_len % 2) == 1) {
last_word = *((u_int8_t *)buff + icmp_len + ICMP_H - 1);
last_word = (htons(last_word) << 8);
sum = 0;
for(i = 0;i < words; i++){
for(int i = 0;i < words; i++){
sum = sum + *(buff + i);
}
sum = sum + last_word;
sum = (sum >> 16) + sum;
return (u_int16_t)~sum;
} else {
sum = 0;
for(i = 0;i < words; i++){
for(int i = 0;i < words; i++){
sum = sum + *(buff + i);
}
sum = (sum >> 16) + sum;
return (u_int16_t)~sum;
}
}

/*
* TCP/UDP checksum function
*/
u_int16_t l4_sum(u_int16_t *buff, int words, u_int16_t *srcaddr, u_int16_t *dstaddr, u_int16_t proto, u_int16_t len)
{
unsigned int i, last_word;
uint32_t sum;

/* Checksum enhancement - Support for odd byte packets */
if((htons(len) % 2) == 1) {
last_word = *((u_int8_t *)buff + ntohs(len) - 1);
last_word = (htons(last_word) << 8);
} else {
/* Original checksum function */
last_word = 0;
}

sum = 0;
for(i = 0;i < words; i++){
sum = sum + *(buff + i);
}
sum = sum + last_word;
sum = sum + *(srcaddr) + *(srcaddr + 1) + *(dstaddr) + *(dstaddr + 1) + proto + len;
sum = (sum >> 16) + sum;
return ~sum;
}

/*
* Builds DHCP option53 on dhopt_buff
*/
Expand Down Expand Up @@ -674,7 +648,7 @@ int build_option54()
/*
* Builds DHCP option55 on dhopt_buff
*/
int build_option55()
void build_option55()
{
if (option55_req_flag == 0) {
u_int32_t msgtype = DHCP_PARAMREQUEST;
Expand All @@ -691,7 +665,6 @@ int build_option55()
memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1);
memcpy((dhopt_buff + dhopt_size + 2), msg, 5);
dhopt_size = dhopt_size + 7;
return 0;
} else if (option55_req_flag == 1) {
u_int32_t msgtype = DHCP_PARAMREQUEST;
u_int32_t msglen = option55_req_len;
Expand Down Expand Up @@ -889,7 +862,7 @@ int build_dhpacket(int pkt_type)
memcpy(dhopt_pointer, dhopt_buff, dhopt_size);

/* UDP checksum is done here */
uh->check = l4_sum((u_int16_t *) (dhcp_packet_disc + l2_hdr_size + l3_hdr_size), ((dhcp_hdr_size + dhopt_size + l4_hdr_size) / 2), (u_int16_t *)&iph->saddr, (u_int16_t *)&iph->daddr, htons(l4_proto), htons(l4_len));
uh->check = l4_sum((u_int16_t *) (dhcp_packet_disc + l2_hdr_size + l3_hdr_size), ((dhcp_hdr_size + dhopt_size + l4_hdr_size) / 2), &iph->saddr, &iph->daddr, htons(l4_proto), htons(l4_len));
}
if(pkt_type == DHCP_MSGREQUEST) {
if(vlan == 0) {
Expand Down Expand Up @@ -962,7 +935,7 @@ int build_dhpacket(int pkt_type)
memcpy(dhopt_pointer, dhopt_buff, dhopt_size);

/* UDP checksum is done here */
uh->check = l4_sum((u_int16_t *) (dhcp_packet_request + l2_hdr_size + l3_hdr_size), ((dhcp_hdr_size + dhopt_size + l4_hdr_size) / 2), (u_int16_t *)&iph->saddr, (u_int16_t *)&iph->daddr, htons(l4_proto), htons(l4_len));
uh->check = l4_sum((u_int16_t *) (dhcp_packet_request + l2_hdr_size + l3_hdr_size), ((dhcp_hdr_size + dhopt_size + l4_hdr_size) / 2), &iph->saddr, &iph->daddr, htons(l4_proto), htons(l4_len));
}
if(pkt_type == DHCP_MSGRELEASE) {
if(vlan == 0) {
Expand Down Expand Up @@ -1025,11 +998,11 @@ int build_dhpacket(int pkt_type)
dhpointer->dhcp_magic = htonl(DHCP_MAGIC);

/* DHCP option buffer is copied here to DHCP packet */
u_char *dhopt_pointer = (u_char *)(dhcp_packet_release + l2_hdr_size + l3_hdr_size + l4_hdr_size + dhcp_hdr_size);
u_char *dhopt_pointer = dhcp_packet_release + l2_hdr_size + l3_hdr_size + l4_hdr_size + dhcp_hdr_size;
memcpy(dhopt_pointer, dhopt_buff, dhopt_size);

/* UDP checksum is done here */
uh->check = l4_sum((u_int16_t *) (dhcp_packet_release + l2_hdr_size + l3_hdr_size), ((dhcp_hdr_size + dhopt_size + l4_hdr_size) / 2), (u_int16_t *)&iph->saddr, (u_int16_t *)&iph->daddr, htons(l4_proto), htons(l4_len));
uh->check = l4_sum((u_int16_t *) (dhcp_packet_release + l2_hdr_size + l3_hdr_size), ((dhcp_hdr_size + dhopt_size + l4_hdr_size) / 2), &iph->saddr, &iph->daddr, htons(l4_proto), htons(l4_len));
}

if(pkt_type == DHCP_MSGDECLINE) {
Expand Down Expand Up @@ -1096,7 +1069,7 @@ int build_dhpacket(int pkt_type)
memcpy(dhopt_pointer, dhopt_buff, dhopt_size);

/* UDP checksum is done here */
uh->check = l4_sum((u_int16_t *) (dhcp_packet_decline + l2_hdr_size + l3_hdr_size), ((dhcp_hdr_size + dhopt_size + l4_hdr_size) / 2), (u_int16_t *)&iph->saddr, (u_int16_t *)&iph->daddr, htons(l4_proto), htons(l4_len));
uh->check = l4_sum((u_int16_t *) (dhcp_packet_decline + l2_hdr_size + l3_hdr_size), ((dhcp_hdr_size + dhopt_size + l4_hdr_size) / 2), &iph->saddr, &iph->daddr, htons(l4_proto), htons(l4_len));
}

return 0;
Expand Down
14 changes: 14 additions & 0 deletions functions_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "chksum.h"
#include "dhcp_err.h"

#include <arpa/inet.h>

#include <stdio.h>

int main(void) {
uint32_t srcaddr = 0x0;
uint32_t dstaddr = 0xffffffff;

printf("0x%X\n", l4_sum((u_int16_t*)&pkt410[0], sizeof(pkt410) / 2, &srcaddr, &dstaddr, htons(17), htons(sizeof(pkt410))));
return 0;
}
7 changes: 5 additions & 2 deletions headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int recv_packet(int pkt_type); /* Receives DHCP packet on socket*/
int reset_dhopt_size(); /* Resets the dhopt_size to zero */
int set_rand_dhcp_xid(); /* Sets a random DHCP xid */
int build_option53(int msg_type); /* Option53: MSGTYPE. Builds option53*/
int build_option55(); /* Requested parameters list */
void build_option55(); /* Requested parameters list */
int build_option54(); /* Builds server identifier on DHCP request */
int build_option50(); /* Option50: Rqstd IP. Builds option50*/
int build_option51(); /* Option51: Rqstd lease time. Builds option51*/
Expand Down Expand Up @@ -153,8 +153,11 @@ struct icmp_hdr
/*
* UDP header included from netinet/udp.h
*/
#if 0
#include<netinet/udp.h>

#else
#include<linux/udp.h>
#endif

/*
* DHCP header
Expand Down