3 - IP and TCP, UDP
Review:
- PDU - Protocol Data Unit
- Peer Process: Having the data between source and destination for the protocol agree
- Encapsulation: The layers below don't care what's in the higher layers; it treats it like any other payload.
Program 1: Networking Data Issues and the checksum()
For program 1, you're parsing these packets and seeing these headers. But part of the assignment is doing the Internet Checksum. But first, consider the following:
#include <stdio.h>
#include <inttypes.h>
void main()
{
uint8_t anArray[100];
int i = 0;
for(i = 0; i < 100; i++)
anArray[i] = i;
printf("%s\n", anArray);
}
This outputs a newline, because it hits the null character first!
Why do we care? Well in the network we are just getting bytes, which can be 0! So using just strcpy
won't work! The lesson here is to not use str
functions unless you know it's a string. Use memcpy()
instead. (Look it up in the man
page if you don't know).
What would happen if you used %s
to print out the 6-byte MAC address of 0x00 0x02 0x2d 0x90 0x75 0x89
?
uint8_t *srcMac = &buffer[6]
printf("Source Mac is: %s\n", srcMac);
The answer:
Endianness
Say we have 2 bytes. Byte 0 and Byte 1:
- In big endian, then the big byte is 1 and the LSB is byte 0
- Little endian has the highbost bit in the MIDDLE of the number, moves to the left (towards the end of byte 0)
Instead, just use htonl, htons
and ntohl, ntohs
to convert to and from the network's endianness to the hosts (if it needs to).