IP Addresses - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

IP Addresses

Description:

This is what makes routing feasible. 2. IP Addresses ... An IP address that has a host ID of all 0s is called a network address and ... – PowerPoint PPT presentation

Number of Views:193
Avg rating:3.0/5.0
Slides: 41
Provided by: csta3
Category:
Tags: address | addresses | ip | is | my | what

less

Transcript and Presenter's Notes

Title: IP Addresses


1
IP Addresses
  • IP is a network layer - it must be capable of
    providing communication between hosts on
    different kinds of networks (different data-link
    implementations).
  • The address must include information about what
    network the receiving host is on. This is what
    makes routing feasible.

2
IP Addresses
  • IP addresses are logical addresses (not physical)
  • 32 bits.
  • Includes a network ID and a host ID.
  • Every host must have a unique IP address.
  • IP addresses are assigned by a central authority
    (American Registry for Internet Numbers for North
    America).

IPv4 (version 4)
3
The four formats of IP Addresses
Class
A
B
C
D
4
  • Class A
  • 128 possible network IDs
  • over 4 million host IDs per network ID

5
Network and Host IDs
  • A Network ID is assigned to an organization by a
    global authority.
  • Host IDs are assigned locally by a system
    administrator.
  • Both the Network ID and the Host ID are used for
    routing.

6
IP Addresses
  • IP Addresses are usually shown in dotted decimal
    notation
  • 1.2.3.4 00000001 00000010 00000011
    00000100
  • cs.rpi.edu is 128.213.1.1
  • 10000000 11010101 00000001 00000001

CS has a class B network
7
Host and Network Addresses
  • A single network interface is assigned a single
    IP address called the host address.
  • A host may have multiple interfaces, and
    therefore multiple host addresses.
  • Hosts that share a network all have the same IP
    network address (the network ID).

8
IP Broadcast and Network Addresses
  • An IP broadcast addresses has a host ID of all
    1s.
  • IP broadcasting is not necessarily a true
    broadcast, it relies on the underlying hardware
    technology.
  • An IP address that has a host ID of all 0s is
    called a network address and refers to an entire
    network.

9
Subnet Addresses
  • An organization can subdivide its host address
    space into groups called subnets.
  • The subnet ID is generally used to group hosts
    based on the physical network topology.

10
UDP Sockets Programming
  • Creating UDP sockets.
  • Client
  • Server
  • Sending data.
  • Receiving data.
  • Connected Mode.

11
Creating a UDP socket
  • int socket(int family,int type,int proto)
  • int sock
  • sock socket( PF_INET, SOCK_DGRAM,
  • IPPROTO_UDP)
  • if (socklt0) / ERROR /

12
Binding to well known address(typically done by
server only)
  • int mysock
  • struct sockaddr_in myaddr
  • mysock socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP)
  • myaddr.sin_family AF_INET
  • myaddr.sin_port htons( PORT )
  • myaddr.sin_addr htonl( INADDR_ANY )
  • bind(mysock, myaddr, sizeof(myaddr))

13
Sending UDP Datagrams
  • ssize_t sendto( int sockfd,
  • void buff,
  • size_t nbytes,
  • int flags,
  • const struct sockaddr to,
  • socklen_t addrlen)
  • sockfd is a UDP socket
  • buff is the address of the data (nbytes long)
  • to is the address of a sockaddr containing the
    destination address.
  • Return value is the number of bytes sent, or -1
    on error.

14
sendto()
  • You can send 0 bytes of data!
  • Some possible errors
  • EBADF, ENOTSOCK bad socket descriptor
  • EFAULT bad buffer address
  • EMSGSIZE message too large
  • ENOBUFS system buffers are full

15
More sendto()
  • The return value of sendto() indicates how much
    data was accepted by the O.S. for sending as a
    datagram - not how much data made it to the
    destination.
  • There is no error condition that indicates that
    the destination did not get the data!!!

16
Receiving UDP Datagrams
  • ssize_t recvfrom( int sockfd,
  • void buff,
  • size_t nbytes,
  • int flags,
  • struct sockaddr from,
  • socklen_t fromaddrlen)
  • sockfd is a UDP socket
  • buff is the address of a buffer (nbytes long)
  • from is the address of a sockaddr.
  • Return value is the number of bytes received and
    put into buff, or -1 on error.

17
recvfrom()
  • If buff is not large enough, any extra data is
    lost forever...
  • You can receive 0 bytes of data!
  • The sockaddr at from is filled in with the
    address of the sender.
  • You should set fromaddrlen before calling.
  • If from and fromaddrlen are NULL we dont find
    out who sent the data.

18
More recvfrom()
  • Same errors as sendto, but also
  • EINTR System call interrupted by signal.
  • Unless you do something special - recvfrom
    doesnt return until there is a datagram
    available.

19
Typical UDP Server code
  • Create UDP socket and bind to well known address.
  • Call recvfrom() to get a request, noting the
    address of the client.
  • Process request and send reply back with sendto().

20
Server Code Example
  • 1 include ltarpa/inet.hgt
  • 2 include ltnetinet/in.hgt
  • 3 include ltstdio.hgt
  • 4 include ltsys/types.hgt
  • 5 include ltsys/socket.hgt
  • 6 include ltunistd.hgt
  • 7
  • 8 define BUFLEN 512
  • 9 define NPACK 10
  • 10 define PORT 9930
  • 18 int main(void)
  • 19
  • 20 struct sockaddr_in si_me, si_other
  • 21 int s, i, slensizeof(si_other)
  • 22 char bufBUFLEN
  • 24 if ((ssocket(AF_INET, SOCK_DGRAM,
    IPPROTO_UDP))-1)
  • 25 diep("socket")
  • 27 memset((char ) si_me, sizeof(si_me),
    0)
  • 28 si_me.sin_family AF_INET
  • 29 si_me.sin_port htons(PORT)
  • 30 si_me.sin_addr.s_addr
    htonl(INADDR_ANY)
  • 31 if (bind(s, si_me, sizeof(si_me))-1)
  • 32 diep("bind")
  • 33
  • 34 for (i0 iltNPACK i)
  • 35 if (recvfrom(s, buf, BUFLEN, 0,
    si_other, slen)-1)
  • 36 diep("recvfrom()")
  • 37 printf("Received packet from
    sd\nData s\n\n",
  • 38 inet_ntoa(si_other.sin_addr),
    ntohs(si_other.sin_port), buf)
  • 39
  • 40
  • 41 close(s)
  • 42 return 0
  • 43

21
Typical UDP client code
  • Create UDP socket.
  • Create sockaddr with address of server.
  • Call sendto(), sending request to the server. No
    call to bind() is necessary!
  • Possibly call recvfrom() (if we need a reply).

22
Client Code Example
  • 1 define SRV_IP "999.999.999.999"
  • 3
  • 4 int main(void)
  • 5
  • 6 struct sockaddr_in si_other
  • 7 int s, i, slensizeof(si_other)
  • 8 char bufBUFLEN
  • 9
  • 10 if ((ssocket(AF_INET, SOCK_DGRAM,
    IPPROTO_UDP))-1)
  • 11 diep("socket")
  • 12
  • 13 memset((char ) si_other,
    sizeof(si_other), 0)
  • 14 si_other.sin_family AF_INET
  • 15 si_other.sin_port htons(PORT)
  • 16 if (inet_aton(SRV_IP,
    si_other.sin_addr)0)
  • 17 fprintf(stderr, "inet_aton()
    failed\n")
  • 18 exit(1)
  • 19
  • 21 for (i0 iltNPACK i)
  • 22 printf("Sending packet d\n", i)
  • 23 sprintf(buf, "This is packet d\n",
    i)
  • 24 if (sendto(s, buf, BUFLEN, 0,
    si_other, slen)-1)
  • 25 diep("sendto()")
  • 26
  • 27
  • 28 close(s)
  • 29 return 0
  • 30

23
Timeout when calling recvfrom()
  • It might be nice to have each call to recvfrom()
    return after a specified period of time even if
    there is no incoming datagram.
  • We can do this by using SIGALRM and wrapping each
    call to recvfrom() with a call to alarm()

24
recvfrom()and alarm()
  • signal(SIGALRM, sig_alrm)
  • alarm(max_time_to_wait)
  • if (recvfrom()lt0)
  • if (errnoEINTR)
  • / timed out /
  • else
  • / some other error /
  • else
  • / no error or time out
  • - turn off alarm /
  • alarm(0)

There are some other (better) ways to do this -
check out section 13.2
25
Connected mode
  • A UDP socket can be used in a call to connect().
  • This simply tells the O.S. the address of the
    peer.
  • No handshake is made to establish that the peer
    exists.
  • No data of any kind is sent on the network as a
    result of calling connect() on a UDP socket.

26
Connected UDP
  • Once a UDP socket is connected
  • can use sendto() with a null dest. address
  • can use write() and send()
  • can use read() and recv()
  • only datagrams from the peer will be returned.

27
Asynchronous Errors
  • What happens if a client sends data to a server
    that is not running?
  • ICMP port unreachable error is generated by
    receiving host and sent to sending host.
  • The ICMP error may reach the sending host after
    sendto() has already returned!
  • The next call dealing with the socket could
    return the error.

28
Back to UDP connect()
  • Connect() is typically used with UDP when
    communication is with a single peer only.
  • Many UDP clients use connect().
  • Some servers (TFTP).
  • It is possible to disconnect and connect the same
    socket to a new peer.

29
TCP Sockets Programming
  • Creating a passive mode (server) socket.
  • Establishing an application-level connection.
  • send/receive data.
  • Terminating a connection.

30
Creating a TCP socket
  • int socket(int family,int type,int proto)
  • int sock
  • sock socket( PF_INET, SOCK_STREAM,
  • 0)
  • if (socklt0) / ERROR /

31
Binding to well known address
  • int mysock
  • struct sockaddr_in myaddr
  • mysock socket(PF_INET,SOCK_STREAM,0)
  • myaddr.sin_family AF_INET
  • myaddr.sin_port htons( 80 )
  • myaddr.sin_addr htonl( INADDR_ANY )
  • bind(mysock, myaddr, sizeof(myaddr))

32
Establishing a passive mode TCP socket
  • Passive mode
  • Address already determined.
  • Tell the kernel to accept incoming connection
    requests directed at the socket address.
  • Tell the kernel to queue incoming connections for
    us.

33
Listen()
  • int listen( int sockfd, int backlog)
  • sockfd is the TCP socket (already bound to an
    address)
  • backlog is the number of incoming connections the
    kernel should be able to keep track of.
  • Listen() returns -1 on error (otherwise 0).

34
Accept()
  • int accept( int sockfd,
  • struct sockaddr cliaddr,
  • socklen_t addrlen)
  • sockfd is the passive mode TCP socket.
  • Cliaddr is a pointer to allocated space.
  • Addrlen is a value-result argument, must be set
    to the size of cliaddr, will be set on return to
    be the number of bytes in cliaddr set by the call
    to accept.

35
accept() return value
  • Accept() returns a new socket descriptor (small
    positive integer) or -1 on error.
  • After accept returns a new socket descriptor I/O
    can be done using the read() and write() system
    calls.
  • read() and write() operate a little differently
    on sockets (vs. file operation)!

36
Terminating a TCP connection
  • Either end of the connection can call the close()
    system call.
  • If the other end had closed the connection and
    there is no buffered data, reading from a TCP
    socket returns EOF (0 bytes).

37
Client Code
  • TCP clients can call connect() which
  • takes care of establishing an endpoint address
    for the client socket (we dont need to call
    bind).
  • Attempts to establish a connection to the
    specified server.

38
connect()
  • int connect( int sockfd,
  • const struct sockaddr server,
  • socklen_t addrlen)
  • sockfd is an already created TCP socket.
  • server contains the address of the server (IP
    Address and TCP port number)
  • connect() returns 0 if OK, -1 on error

39
Reading from a TCP socket
  • int read( int fd, char buf, int max)
  • By default read() will block until data is
    available
  • reading from a TCP socket may return less than
    max bytes (whatever is available).
  • You must be prepared to read data 1 byte at a
    time!

40
Writing to a TCP socket
  • int write( int fd, char buf, int num)
  • write might not be able to write all num bytes
    (on a nonblocking socket).
Write a Comment
User Comments (0)
About PowerShow.com