post
poster: Thetawaves
description: full source
language: C
[download]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

//source and destination IP address
//a source and destination port
//and a message
#define SRC_ADDR "192.168.117.1"
#define SRC_PRT 5000
#define DEST_ADDR "192.168.117.129"
#define SRC_PRT 5000
typedef struct s_udp
{
    unsigned int src_no;
    unsigned int dest_no;
    unsigned char zeros;
    unsigned char protocol;
    unsigned short udp_len;
    unsigned short src_port;
    unsigned short dest_port;
    unsigned short length;
    // 1s compliment addition
    unsigned short checksum;
} udp_pkt;

int init_udp(udp_pkt * new_pkt)
{
    new_pkt->zeros = 0;
    new_pkt->protocol = 17;
}

int checksum(udp_pkt * thepkt, char * message)
{
    unsigned int sum;
    int ii = 0;
    unsigned short pudding = 0;
    char * ptr = (char *)thepkt;
    
    for (ii = 0; ii < sizeof(udp_pkt)/2; ii++)
    {
        pudding = *(ptr + 2*ii);
        sum = sum + pudding;
    }
    ptr = message;
    for (ii = 0; ii < strlen(message)/2; ii++)
    {
        pudding = *(ptr + 2*ii);
        sum = sum + pudding;
    }
    // add the carrys back on to the end.
    while (sum>>16)
        sum = (sum & 0xFFFF) + (sum >> 16);
    
    sum = ~sum;
    return (unsigned short)sum;
}

int mkpkt(udp_pkt * thepkt, unsigned long src_no, unsigned long dest_no, unsigned short src_port, unsigned short dest_port, char * message)
{
    unsigned int * tmpptr;
    init_udp(thepkt);
    thepkt->src_no = src_no;
    thepkt->dest_no  = dest_no;
    thepkt->udp_len = htons(8 + strlen(message));
    thepkt->src_port = htons(src_port);
    thepkt->dest_port = htons(dest_port);
    thepkt->length = htons(8 + strlen(message));
    thepkt->checksum = checksum(thepkt, message);
}
int main(int argc, char **argv)
{
    int ii = 0;
    char * message;
    message = "This a very long test for CS 442 which we can compare with wireshark.\n";
    char * newptr;
    udp_pkt thepkt;
    // Get the integer representation of the ip in NO
    unsigned long src_no = inet_addr(SRC_ADDR);
    unsigned long dest_no = inet_addr(DEST_ADDR);
    // make sure the message is a multiple of 2 for the checksum algo
    if (strlen(message) % 2)
    {
        newptr = malloc(strlen(message) + 1);
        // die if unable to alloc
        if (newptr == NULL)
            return -1;
        memcpy(newptr, message, strlen(message));
        message = newptr;
    }
    // make the packet with incoming port 50077 and outgoing port 60000
    mkpkt(&thepkt, src_no, dest_no,50077, 60000, message);
    char * full_pkt = malloc(sizeof(udp_pkt) + sizeof(message));
    // die if unable to alloc
    if (full_pkt == NULL)
        return -1;
    // copy the packet headers
    memcpy(full_pkt, &thepkt, sizeof(udp_pkt));
    // copy the message
    memcpy(full_pkt + sizeof(udp_pkt), (void *)message, strlen(message));
    for (ii = 0; ii < strlen(message); ii++)
    {
        printf("%c", *(((unsigned char *)full_pkt + sizeof(udp_pkt)) + ii) );
    }    
    for (ii = 0; ii < (sizeof(udp_pkt) + strlen(message)); ii++)
    {
        printf("%02x", *(((unsigned char *)full_pkt) + ii) );
        if (ii % 4 == 3)
            printf("\n");
    }
    printf("\n");
    for (ii = 0; ii < strlen(message); ii++)
    {
        printf("%c", *(((unsigned char *)full_pkt + sizeof(udp_pkt)) + ii) );
    }
    //get rid of this allocated space now that a real packet has been generated..
    if (newptr)
        free(newptr);
    free(full_pkt);
    return 0;
}