Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from 1-126....

24
Class A Addresses • The 1 st bit of a class A address is 0 • The 1 st byte has a value from 1-126. (128.x.x.x would not be a class A) • 127.x.x.x is reserved for loopback (127.0.0.1 often used) • x.255.255.255 is used from broadcast • x. 0.0.0 is used as a ‘default’ router • This allows 16,277,214 (2 24 -2) hosts per class A network. • Only a few class A licenses are not shared. • covers 50% of the entire address space 0 7 bits/1 st byte 24 bits for host addresses/3 bytes

Transcript of Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from 1-126....

Class A Addresses• The 1st bit of a class A address is 0• The 1st byte has a value from 1-126. (128.x.x.x would not be a class A)• 127.x.x.x is reserved for loopback (127.0.0.1 often used)• x.255.255.255 is used from broadcast• x. 0.0.0 is used as a ‘default’ router• This allows 16,277,214 (224 -2) hosts per class A network.• Only a few class A licenses are not shared. • covers 50% of the entire address space

0 7 bits/1st byte 24 bits for host addresses/3 bytes

Class B Addresses• The 1st 2 bits of a class B address is 10• Allows for 214 or 16,384 network licences, 216-2 hosts per network #• 127.x.x.x is reserved for loopback (127.0.0.1 often used)• 126 network numbers• x.x.255.255 is used for broadcast• x. x.0.0 is used as a ‘default’ router• This allows 16,384 (214 -2) hosts per class A network• Represents 25% of all available IP addresses• 1st byte is from 128 to 191

10 14 bits 16 bits for host addresses/2 bytes

Class C Addresses• The 1st 3 bits of a class C address is 110• Allows for 221 or 2,097,152 network licences, 254 hosts per network • 127.x.x.x is reserved for loopback (127.0.0.1 often used)• 126 network numbers• x.x.x.255 is used for broadcast• x. x.x.0 is used as a ‘default’ router• Represents 12.5% of all available IP addresses• first byte is 192-223

110 21 bits/3 bytes 8 bits

Class D Addresses

• The 1st 4 bits of a class B address is 1110• first byte is 192-223• Reserved by the Internet Address Number

Authority (IANA) for broadcast purposes

1110 Addresses from 224.0.0.0 - 239.255.255.255

Class E Addresses

• The 1st 5 bits of a class B address is 11110• first byte is 240-255• Reserved for experimental use

1110 Addresses from 224.0.0.0 - 239.255.255.255

Sockets And Connections

(exploring Beej’s client and server program)

Writing a Client in C

• gettaddrinfo(...) – name+port -> IP address• inet_ntop(...) - only needed to display address• socket(...) - create an fd for the socket• connect(...) - open the socket • recv(...) - receive bytes from the socket • close(...) or shutdown(..) – close the socekt

getaddrinfo(name,service,hints,result)

• name: a string, ie: humber.c• service: a port# or service name, “80”, “http”• hints: uses the addrinfo to suggest V4, V6... • results: a linked list of addrinfo s

}

int ai_flags;int ai_family; //AF_INET, AF_INET6, AF_UNIX // AF_UNSPE int ai_socktype; //SOCK_STREAM, SOCK_DGRAM int ai_protocol; size_t ai_addrlen;struct sockaddr *ai_addr; //different for V4, V6char *ai_canonname; //not used?struct addinfo * ai_next; //it’s a linked list!jk

struct addrinfo {

int socket(family,socketType,protocol)

• family: AF_INET, AF_INET6, AF_UNIX• socketType: SOCK_STREAM, SOCK_DGRAM,

SOCK_RAW, ...• protocol - set to zero. Most socket types have

only one protocol so we can ignore this

connect(fd,ipAddr,addrLength)

• The fd is the local end of the socket• the ipAddr is either V4 or V6 – a string• addrLength is the length of the string

Fails if the service is not available.Not used with SOCK_DGRAM (connectionless)

recv(fd,buf,bufsize,flags)

• exactly like read – except for the flags• unless socket is non-blocking it waits for input• we don’t use flags in the 1st example– MSG_DONTWAIT– MSG_ERRQUEUE– MSG_OOB– MSG_PEEK

close(fd)

Yes, it’s really simple!!!!

shutdown(fd, how)

• ‘how’ is either SHUT_RD, SHUT_WR, SHUT_RDWR

• SHUT_WR used to signal an EOF to the other end• If we have multiple threads SHUT_RDWR

decrements a count leaving the socket open

Writing a Server in C• gettaddrinfo(...) - info about this machine• socket(...) create a socket• setsockopt(...) - define properties of the socket• bind(...)• listen(...) - wait for a connection – this blocks• freeaddrinfo() - return the linked list’s memory• sigaction (...) - reap any dead child processes• accept(...) - accept the connection• inet_ntop(...) - not req’d: display the connection• fork a child to carry on the conversation

– send(...) (can also receive)– close(....) - close the connection

getaddrinfo(NULL,service,&hints,&result)

• NULL - not needed• service - port or service name as a string• hints – field .ai_flags is AI_PASSIVE – use this host– ai_socktype - SOCK_STREAM– ai_family - AF_UNSPEC - whatever. AF_INET, AF_INET6

Returns: a list of IP addresses, usually 1 or an error code – service may be in use

int socket(family,socketType,protocol)

• family: AF_INET, AF_INET6, AF_UNIX• socketType: SOCK_STREAM, SOCK_DGRAM,

SOCK_RAW, ...• protocol - set to zero. Most socket types have

only one protocol so we can ignore this

setsockopt(fd,level,optname,&value,len)

• Used to set socket characteristics• fd – the socket id from call to socket()• level – always SOL_SOCKET - it corresponds to the

protocol, and we don’t have any• optname: SO_REUSEADDR - set this to 1/true

other options will come up later. For a list of options, see: man 7 socket

• value - settings for options may be different data structures – pass an address

• len

bind(fd,addressInfo,length)

• fd – the socket we just opened• addressInfo - AF_type, port – we have to and will match

the info in the call to socket• length – size

bind generates a name for the socket in the proc directory. In the client program, the call to socket only connects to an existing socket.

ONLY THE SUPERUSER can use a port <1024If the ip address is 0 (INADDR_ANY) then you can bind toany network card in the machine.

listen(fd,nItems)

Sets up a queue for the socket of nItems.

If a server is busy handling a connection it might miss the next request for one, even if we fork quickly.

However if the average interarrival time is too high, no size may be big enough. This is to handle surges in requests, ie: when a class logs into a service all at once.

int accept(fd, &theirAddr, length)

• blocks/waits for a connection• returns their addresss info and the

bidirectional socket to talk to

send(fd,buffer,length, flags)

• send a message to the client

close(fd)

• Yes, it’s that simple• Can also use shutdown(fd,how)

Other Management Tasks

• memset(&memoryBlock,val, nBytes)• freeaddrinfo(serverInfo)• sigemptyset(signalset) – clears all signal

handlers• sigaction(SIGHCHILD, handler,lastUse) -

advanced version of signal(signum,handler) - it can be ignored.

• Use ps and kill -9 to terminate rogue children