Presentation is loading. Please wait.

Presentation is loading. Please wait.

TDC561 Network Programming Camelia Zlatea, PhD Week 9: IPv6 IPv6 Client-Server Programming.

Similar presentations


Presentation on theme: "TDC561 Network Programming Camelia Zlatea, PhD Week 9: IPv6 IPv6 Client-Server Programming."— Presentation transcript:

1 TDC561 Network Programming Camelia Zlatea, PhD Email: czlatea@cs.depaul.educzlatea@cs.depaul.edu Week 9: IPv6 IPv6 Client-Server Programming

2 Page 2 Network Programming (TDC561) Winter 2003 References  W. Richard Stevens, Network Programming : Networking API: Sockets and XTI, Volume 1, 2nd edition, 1998 (ISBN 0- 13-490012-X) –Chap. 7, 11, 19, 21, 22

3 Page 3 Network Programming (TDC561) Winter 2003 Motivation for IPv6 Total Datagram Length Datagram ID Checksum 32 bit source address 32 bit destination address 4 bytes (32 bits) Header Length Flags Fragment Offset Ver TOS TTLProtocol IP Options Payload (data portion) Basic Header IPv4 Header Format IPv4 - Variable Header, 32-bit addresses 4 for IPv4

4 Page 4 Network Programming (TDC561) Winter 2003 Motivation for IPv6 Payload Length 128 bit source address 4 bytes (32 bits) Next Header Hop Limit Ver Payload (includes optional headers & data portion) Basic Header Traffic Class Flow Label 128 bit destination address IPv6 Header Format IPv6 - Fixed Header, 128-bit addresses, Flow label 6 for IPv6

5 Page 5 Network Programming (TDC561) Winter 2003 Motivation for IPv6  IPv6 supports very large address space –No shortage of IPv4 addresses within organizations  Showcase a variety of applications –Support for hand-held devices –IPv6 adopted as standard for 3G mobile networks  Support for IPv6 in Win2000 –Microsoft has technical preview of IPv6 with APIs and SDKs available for Win2000

6 Page 6 Network Programming (TDC561) Winter 2003 IPv6 Headers  Simpler header - faster processing by routers. –No optional fields - fixed size (40 bytes) –No fragmentation fields. –No checksum  Support for multiple headers –more flexible than simple “protocol” field.

7 Page 7 Network Programming (TDC561) Winter 2003 IPv6 Header Fields  VERS: 6 (IP version number)  Priority(Traffic Class): will be used in congestion control  Flow Label: experimental - sender can label a sequence of packets as being in the same flow.  Payload Length: number of bytes in everything following the 40 byte header, or 0 for a Jumbogram.  Next Header is similar to the IPv4 “protocol” field - indicates what type of header follows the IPv6 header.  Hop Limit is similar to the IPv4 TTL field (but now it really means hops, not time).

8 Page 8 Network Programming (TDC561) Winter 2003 Extension Headers  Routing Header - source routing  Fragmentation Header - supports fragmentation of IPv6 datagrams.  Authentication Header  Encapsulating Security Payload Header

9 Page 9 Network Programming (TDC561) Winter 2003 IPv6 availability  Generally available with (new) versions of most operating systems. –BSD, Linux, Solaris 8  An option with Windows 2000/NT  Most routers can support IPV6

10 Page 10 Network Programming (TDC561) Winter 2003 IPv6 Design Issues  Overcome IPv4 scaling problem –lack of address space.  Flexible transition mechanism.  New routing capabilities.  Quality of service.  Security.  Ability to add features in the future.

11 Page 11 Network Programming (TDC561) Winter 2003 IPv6 Addresses  128 bits - written as eight 16-bit hex numbers. 5f1b:df00:ce3e:e200:0020:0800:2078:e3e3  High order bits determine the type of address.

12 Page 12 Network Programming (TDC561) Winter 2003 Motivation for IPv6  Structured IPv6 header formats lead to design of fast and efficient routers –All headers (including optional headers) are 40 bytes long »In IPv4, header length varies from 20 to 60 bytes –Intermediate routers cannot fragment packet »Fragmentation is possible only at the source »Reduces routing overhead – No checksum performed on the header »Optional IPSEC header authentication can be used  Efficient address management in IPv6  Relaxed schedules for Router upgrades

13 Page 13 Network Programming (TDC561) Winter 2003 Motivation for IPv6  IPv6 Address assignment designed to support address aggregation –Smaller routing tables, easier to manage, more scalable, faster and efficient routers  Support for multicast addresses –Various standard multicast addresses are pre-defined »Useful for dynamically changing information on a given subset of routers/servers e.g., Lease time for addresses assigned by DHCP servers can be easily modified with a controlled multicast sent to all-DHCP servers multicast address (useful for renumbering IP addresses)

14 Page 14 Network Programming (TDC561) Winter 2003 IPv6 Unicast Address Types  Link-local unicast addresses –Automatically assigned and used for localized communication –Pertinent only to routers and end-hosts on the local subnet  Site-local unicast address –Similar to IPv4 private addresses –Has relevance only within the given site »Site could refer to a given enterprise or a given geographic location »Packets bearing this address are not routed outside the scope of the site. continued...

15 Page 15 Network Programming (TDC561) Winter 2003 IPv6 Unicast Address Types  Aggregation of global unicast address –Unique address across all IP networks –Assigned by the Internet Service Provider »ISP gets the bundle of addresses from Regional Registries for Internet Numbers »Helps in address aggregation and smaller Internet routing tables »IPv4 addresses are not dependent on the ISP. Results in large Internet routing tables.  IP address aggregation is crucial for scaling

16 Page 16 Network Programming (TDC561) Winter 2003 IPv6 - Aggregate Global Unicast Address 001TLA IDNLA IDSLA IDInterface ID 313321664 TLA: top-level aggregation NLA: next-level SLA: site-level Interface ID is (typically) based on hardware MAC address

17 Page 17 Network Programming (TDC561) Winter 2003 IPv4-Mapped IPv6 Address  IPv4-Mapped addresses allow a host that support both IPv4 and IPv6 to communicate with a host that supports only IPv4.  The IPv6 address is based completely on the IPv4 address.  80 bits of 0s followed by 16 bits of ones, followed by a 32 bit IPv4 Address: 80 bits32 bits16 bits 0000... 0000IPv4 AddressFFFF

18 Page 18 Network Programming (TDC561) Winter 2003 Works with DNS  An IPv6 application asks DNS for the address of a host, but the host only has an IPv4 address.  DNS creates the IPv4-Mapped IPv6 address automatically.  Kernel understands this is a special address and really uses IPv4 communication.

19 Page 19 Network Programming (TDC561) Winter 2003 IPv4-Compatible IPv6 Address  An IPv4 compatible address allows a host supporting IPv6 to talk IPv6 even if the local router(s) don’t talk IPv6.  IPv4 compatible addresses tell endpoint software to create a tunnel by encapsulating the IPv6 packet in an IPv4 packet.  80 bits of 0s followed by 16 bits of 0s, followed by a 32 bit IPv4 Address: 0000... 0000IPv4 Address0000 80 bits32 bits16 bits

20 Page 20 Network Programming (TDC561) Winter 2003 Tunneling IPv6 Host IPv6 Host IPv4 Routers IPv6 Datagram IPv4 Datagram  done automatically by kernel when IPv4-Compatible IPv6 addresses are used

21 Page 21 Network Programming (TDC561) Winter 2003 Dual Server  Server that handle both IPv4 and IPv6.  The work is handled by the O.S. (which contains protocol stacks for both v4 and v6): –automatic creation of IPv6 address from an IPv4 client (IPv4- mapped IPv6 address).

22 Page 22 Network Programming (TDC561) Winter 2003 IPv4 client IPv4 client TCP IPv4 Datalink IPv6 client IPv6 client TCP IPv6 Datalink IPv6 server IPv6 server TCP Datalink IPv4 IPv6 IPv4-mapped IPv6 address IPv4-mapped IPv6 address Dual Server

23 Page 23 Network Programming (TDC561) Winter 2003 IPv6 Clients  If an IPv6 client specifies an IPv4 address for the server, the kernel detects and talks IPv4 to the server.  DNS support for IPv6 addresses can make everything work. –getaddrinfo() returns an IPv4 mapped IPv6 address for hosts that only support IPv4.

24 Page 24 Network Programming (TDC561) Winter 2003 IPv6 - IPv4 Programming  The kernel does the work, we can assume we are talking IPv6 to everyone!  In case we really want to know, there are some macros that determine the type of an IPv6 address. –We can find out if we are talking to an IPv4 client or server by checking whether the address is an IPv4 mapped address.

25 Page 25 Network Programming (TDC561) Winter 2003 IPv6 Sockets programming  New address family: AF_INET6  New address data type: in6_addr  New address structure: sockaddr_in6  IPv6-based client/server applications

26 Page 26 Network Programming (TDC561) Winter 2003 in6_addr struct in6_addr { uint8_t s6_addr[16]; };

27 Page 27 Network Programming (TDC561) Winter 2003 sockaddr_in6 struct sockaddr_in6 { uint8_tsin6_len; sa_family_tsin6_family; in_port_tsin6_port; uint32_tsin6_flowinfo; struct in6_addrsin6_addr; };

28 Page 28 Network Programming (TDC561) Winter 2003 getaddrinfo()  used both to look up hostnames, and to look up service names (port numbers)  does not use perror() to report errors, but its own function gai_strerror(). #include int getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res); IPv4IPv6 gethostbyname() getaddrinfo() getservicebyname()

29 Page 29 Network Programming (TDC561) Winter 2003 getnameinfo()  Extracting info about a connection is placed in getnameinfo()  Corresponding to getpeername/getsockname. #include int getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags); IPv4IPv6 getservbyport() getnameinfo() getservbyname() getpeername()

30 Page 30 Network Programming (TDC561) Winter 2003 Operating Systems - IPv6 Support  Some operating systems do not conform to the standard interfaces.  OpenBSD does not map IPv4 addresses to IPv6 ranges.  Windows winsock, –reported issues in supporting inet_ptoa. –alternative: getaddrinfo can be used to get around this (more expensive).  Standard way to find out interface configuration information about the ipv6 addresses –ifconfig/ipconfig. (SunOS, HP-UX, Linux versions,..) –Example: % ifconfig le0 inet6 le0: flags=2000841

31 Page 31 Network Programming (TDC561) Winter 2003 Example: Mixing IPv4 and IPv6 // Server.c #include #define bufsize 20 #define queuesize 5 #define true 1 #define false 0 main () { struct sockaddr_in6 cin; char buffer[bufsize]; int sd, sd_client, addrlen, yes=1,err; struct addrinfo query,*response,*ap; bzero(&query,sizeof(struct addrinfo)); query.ai_flags = AI_PASSIVE; query.ai_family = AF_UNSPEC; query.ai_socktype = SOCK_STREAM; if ((err = getaddrinfo(NULL,"servicename",&query,&response)) != 0) { printf("%s",gai_strerror(err)); return; }

32 Page 32 Network Programming (TDC561) Winter 2003 Mixing IPv4 and IPv6 sd = -1; for (ap = response ; ap != NULL; ap=ap->ai_next) { if ((sd = socket(ap->ai_family,ap->ai_socktype,ap->ai_protocol)) == -1) { continue; } if (bind(sd,ap->ai_addr,ap->ai_addrlen) == 0) { break; // success } perror("bind"); close(sd); sd = -1; } freeaddrinfo(response); if (sd < 0) { printf("Couldn't open bind an open socket\n"); exit(1); } if (listen(sd,queuesize) == -1) { perror("listen"); exit(1); }

33 Page 33 Network Programming (TDC561) Winter 2003 Mixing IPv4 and IPv6 while (true) { char hostname[100],service[100]; bzero(hostname,100); bzero(service,100); if ((sd_client = accept(sd,(struct sockaddr *)&cin,&addrlen)) == -1) { perror("accept"); exit(1); } if (getnameinfo((struct sockaddr *)&cin,addrlen,hostname,100,service,100,0) != 0) { perror("getnameinfo"); } printf("Connection received from host (%s) on remote port (%s)\n",hostname,service); bzero(buffer,20); if (recv(sd_client,buffer,sizeof(buffer),0) == -1) { perror("recv"); exit(1); }

34 Page 34 Network Programming (TDC561) Winter 2003 Mixing IPv4 and IPv6 // doWork(buffer) here if (send(sd_client,buffer,strlen(buffer),0) == -1) { perror("send"); exit(1); } close (sd_client); } close (sd); printf("Server closing down...\n"); }

35 Page 35 Network Programming (TDC561) Winter 2003 Mixing IPv4 and IPv6 // Client.c char *sockaddr_ntop(struct sockaddr *sa); main (argc,argv) int argc; char *argv[]; { struct sockaddr_in6 cin,cin2; struct addrinfo req, *ans, *ap; char buffer[bufsize],ip6str[INET6_ADDRSTRLEN]; int sd,err,connected = false; bzero(&req,sizeof(struct addrinfo)); req.ai_family = AF_UNSPEC; req.ai_socktype = SOCK_STREAM; // This port name has to exist, otherwise getaddrinfo fails if ((err = getaddrinfo(HOST,"servicename",&req,&ans)) != 0) { printf("Lookup error: %s\n",gai_strerror(err)); exit(0); } #include #define HOST “hawk.depaul.edu" #define bufsize 20 #define false 0 #define true 1

36 Page 36 Network Programming (TDC561) Winter 2003 Mixing IPv4 and IPv6 for (ap = ans; ap != NULL; ap = ap->ai_next) { printf("Trying to connect to %s = %s\n",HOST,sockaddr_ntop(ap->ai_addr)); if ((sd = socket(ap->ai_family,ap->ai_socktype,ap->ai_protocol)) == -1) { perror("socket"); exit(1); } if (connect(sd, ap->ai_addr, ap->ai_addrlen) >= 0) { connected = true; break; } freeaddrinfo(ans); if (connected) { sprintf(buffer,"%s + %s",argv[1],argv[3]); if (send(sd,buffer,strlen(buffer),0) == -1) { perror ("send"); exit(1); }

37 Page 37 Network Programming (TDC561) Winter 2003 Mixing IPv4 and IPv6 bzero(buffer,bufsize); if (recv(sd,buffer,bufsize,0) == -1) { perror("recv"); exit (1); } printf ("Server responded with %s\n",buffer); close (sd); } char *sockaddr_ntop(struct sockaddr *sa)

38 Page 38 Network Programming (TDC561) Winter 2003 Mixing IPv4 and IPv6 char *sockaddr_ntop(struct sockaddr *sa) { void *addr; static char addrbuf[INET6_ADDRSTRLEN]; switch (sa->sa_family) { case AF_INET: addr = &((struct sockaddr_in *)sa)->sin_addr; break; case AF_INET6: addr = &((struct sockaddr_in6 *)sa)->sin6_addr; break; default: /* terminate the process abnormally */ abort(); } inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf)); / * The inet_ntop() routine converts a numeric address into a string suitable for presentation. Can manipulate both IPv4 and IPv6 addresses */ return addrbuf; }


Download ppt "TDC561 Network Programming Camelia Zlatea, PhD Week 9: IPv6 IPv6 Client-Server Programming."

Similar presentations


Ads by Google