Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class A Addresses The 1st bit of a class A address is 0

Similar presentations


Presentation on theme: "Class A Addresses The 1st bit of a class A address is 0"— Presentation transcript:

1 Class A Addresses The 1st bit of a class A address is 0
The 1st byte has a value from (128.x.x.x would not be a class A) 127.x.x.x is reserved for loopback ( often used) x is used from broadcast x 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 bits for host addresses/3 bytes

2 Class B Addresses The 1st 2 bits of a class B address is 10
Allows for 214 or 16,384 network licences, hosts per network # 127.x.x.x is reserved for loopback ( often used) 126 network numbers x.x 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 bits bits for host addresses/2 bytes

3 Class C Addresses The 1st 3 bits of a class C address is 110
Allows for 221 or 2,097,152 network licences, hosts per network 127.x.x.x is reserved for loopback ( 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 bits/3 bytes bits

4 Class D Addresses The 1st 4 bits of a class B address is 1110
first byte is Reserved by the Internet Address Number Authority (IANA) for broadcast purposes Addresses from

5 Class E Addresses The 1st 5 bits of a class B address is 11110
first byte is Reserved for experimental use Addresses from

6 Sockets And Connections
(exploring Beej’s client and server program)

7 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

8 getaddrinfo(name,service,hints,result)
name: a string, ie: humber.ca service: a port# or service name, “80”, “http” hints: uses a sample addrinfo to suggest V4, V6... TCP, UDP – preferred choice of connection hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; results: starts a linked list of type addrinfo

9 struct addrinfo { int ai_flags; int ai_family; //AF_INET, AF_INET6, AF_UNIX // AF_LOCAL, AF_UNSPEC int ai_socktype; //SOCK_STREAM, SOCK_DGRAM int ai_protocol; size_t ai_addrlen; struct sockaddr *ai_addr; //different for V4, V6 char *ai_canonname; //not used? struct addinfo * ai_next; //it’s a linked list!jk }

10 A Generic Addresses struct sockaddr { sa_family_t sa_family; char sa_data[]; } struct sockaddr_in { //ipv4 address sa_family_t sa_family; //Always AF_INET in_port sin_port; //port: 16 bits struct inaddr sin_addr; //32 bit internet address } struct sockaddr_in6 { //ipv6 address* sa_family_t sa_family; //Always AF_INET6 in_port sin6_port; //port: 16 bits struct inaddr sin_addr; //32 bit internet address struct in6_addr sin6addr; //IPV6 address -128 bits. } *I’ve left out 2 fields which aren’t important. What’s relevant is that sockaddr_in and sockaddr_in6 are different sizes! How does that work???

11 if (genericAddress->sa_family==AF_INET) {
Here’s a trick to handle both Note that the first field, sa_family, in all 3 is the same! struct sockaddr *generic_address; struct sockaddr_in *inet4_address; struct sockaddr_in6 *inet6_address; if (genericAddress->sa_family==AF_INET) { inet4_address=(struct sockaddr_in *) generic_address; code that is ipV4 specific } else { inet6_address=(struct sockaddr_in6 *) generic_address; code that is ipV6 specific }

12 pointer sockaddr_in sockaddr_in6

13 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

14 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)

15 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

16 close(fd) Yes, it’s really simple!!!!

17 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

18 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

19 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

20 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

21 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

22 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 <1024 If the ip address is 0 (INADDR_ANY) then you can bind to any network card in the machine.

23 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.

24 int accept(fd, &theirAddr, length)
blocks/waits for a connection returns their addresss info and the bidirectional socket to talk to

25 send(fd,buffer,length, flags)
send a message to the client

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

27 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


Download ppt "Class A Addresses The 1st bit of a class A address is 0"

Similar presentations


Ads by Google