Byte Ordering

Download Report

Transcript Byte Ordering

Onward with Chat!
Networking
CS 3470, Section 1
Chat v1



Handing back grading sheets
Average score = ACommon issues



Makefile
Argument checking
Hardcoded port number

Even though taking it in as a parameter!
Makefile
# This is a comment. Don’t use C-style comments
CC=gcc
CFLAGS=-Wall
all: chat
chat: chat.c
$(CC) -o chat chat.c $(CFLAGS)
clean:
rm chat
3
Argument Checking Part #1
int main(int argc, char **argv)
{
/* Are we invoking the server or client? Let's figure
it out based
* on the number of arguments. */
if(argc > 1)
client(argc, argv);
else
server();
return 0;
4
Argument Checking Part #2
int client(int argc, char **argv)
{
struct hostent *hp;
…
/* Checking parameters */
/* There had better be exactly 5, else we are outa here. */
if (argc!=5) {
print_usage();
exit(1);
}
5
Argument Checking Part #3
/* There should be a flag in the first and third positions. */
if(strcmp(argv[1], "-p")==0) {
port=atoi(argv[2]);
host=argv[4];
}
else if(strcmp(argv[3], "-p")==0) {
port=atoi(argv[4]);
host=argv[2];
}
else { /* Something is off */
print_usage();
exit(1);
}
6
Helpful Functions


atoi – converts a string to an int
strcmp or strncmp – compares two strings
7
Any other questions?
8
Onward

Next program: full chat!

Back and forth chatting


Packet formatting






Pretty formatting like on the original project specification
Version header
Src IP header
Dst IP header
Checksum field
Length of message (always 140)
Bad chat

For testing!
9
Back and Forth Chatting


Do this first!
Both send() and rcv() must be in the client
and server function’s while loops
10
Packet Formatting

Your packet should look like this

(Field sizes coming soon)
Version
Src IP
Dest IP
Data
Checksum
11
Packet Formatting





Version = 2
Src IP = IP of sender
Dest IP = IP of receiver
Data = chat message, always 140
bytes/characters long
Checksum
Version
Src IP
Dest IP
Data
Checksum
12
Packet Formatting



How do I do this?
Create a struct packet that holds all those
fields
Send the struct packet over the socket
13
Creating a Struct Packet




Assign 2 to version field
Get source and destination IP from socket,
assign to src and dest fields
Get user-typed chat message from fgets(),
use strcpy (“string copy”) to copy it into data
field
Use checksum function on page 95 of your
book to compute checksum of all previous
fields
14
Checksum


Sender creates checksum, copies it to end of
packet, and sends packet through socket
Receiver receives packet, creates checksum,
and checks to see that computed checksum
matches checksum found in packet


If matches, it shouldn’t do anything
If it doesn’t match, it should display an error to
the user.
15
Bad Chat

How can we test our checksum routines?



We can invoke a bad checksum routine on chat
by using a “–b” flag with either the client or the
server
This will invoke a second “bad checksum”
function that appends an incorrect checksum to
our packet
Computing checksum on receiver side should
always use “good” checksum function
16
Other Things

Cannot have any pointers in struct packet
before sending it over the socket

Why?
17
Other Things

Must be aware of byte ordering!
18
Byte Ordering
Increasing memory address
Address A+1
Little-endian
byte order
High-order byte
MSB
Big-endian
byte order
Address A
Low-order byte
16-bit value
Low-order byte
Address A+1
LSB
High-order byte
Address A
Increasing memory address
Implications of Byte Order



Unfortunately there is no standard between
these two byte orderings and we encounter
systems that use both formats
We refer to the byte ordering used by a given
system as host byte order
The sender and the receiver must agree on the
order in which the bytes of these multi-byte field
transmitted: specify network byte order, which
is big-endian byte ordering
Byte Order Functions
#include <netinet.h>
/* Host to network */
uint16_t htons(uint16_t host16bitvalue)
Converts a 16-bit integer from host to network byte order
uint32_t htonl(uint32_t host32bitvalue)
Converts a 32-bit integer from host to network byte order
Both return: value in network byte order
/* Network to host */
uint16_t ntohs(uint16_t net16bitvalue)
uint32_t ntohl(uint32_t net32bitvalue)
Both return: value in host byte order
When do we use hton/ntoh
functions?

Use hton the port number in struct
sockaddr_in

If we create a custom struct to hold our
headers and data


Sending our data through send() and recv()
functions
E.g., if our first struct member is a 2-byte header,
and sender/receiver have different memory
orderings, number would look very different to
each machine
22
Address Conversion Functions
#include <arpa/inet.h>
in_addr_t inet_addr(const char *strptr);
/* return 32-bit binary network byte ordered IPv4
address; INADDR_NONE if error, deprecated and replaced
by inet_aton() */
char *inet_ntoa(struct in_addr inaddr);
/* returns: pointer to dotted-decimal string */