ipv6 programming
Upcoming SlideShare
Loading in...5
×
 

ipv6 programming

on

  • 835 views

 

Statistics

Views

Total Views
835
Views on SlideShare
835
Embed Views
0

Actions

Likes
1
Downloads
65
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

ipv6 programming ipv6 programming Presentation Transcript

  • IPv6 Programming Eric 2012/06/29
  • outlineecho clientecho serverDNS conversionsrefine our app to support ipv6 and ipv4special case
  • ipv4 echo clientint sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);struct sockaddr_in serv_addr;memset((unsigned char *)&serv_addr, 0x00, sizeof(serv_addr));serv_addr.sin_family = AF_INET;serv_addr.sin_port = htons(9999);inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);connect(sock, (struct sockaddr *)&serv_addr,sizeof(serv_addr));
  • ipv6 echo clientint sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);struct sockaddr_in6 serv_addr;memset((unsigned char *)&serv_addr, 0x00, sizeof(serv_addr));serv_addr.sin6_family = AF_INET6;serv_addr.sin6_port = htons(9999);inet_pton(AF_INET6, "::1", &serv_addr.sin6_addr);connect(sock, (struct sockaddr *)&serv_addr,sizeof(serv_addr));
  • comparison
  • ipv4 echo serverint sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);struct sockaddr_in serv_addr;memset((unsigned char *)&serv_addr, 0x00, sizeof(serv_addr));serv_addr.sin_family = AF_INET;serv_addr.sin_port = htons(9999);serv_addr.sin_addr.s_addr = INADDR_ANY;struct sockaddr_in client_addr;bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));listen(sock, 20);socklen_t len = sizeof(client_addr);int conn = accept(sock, (struct sockaddr *)&client_addr,&len);
  • ipv6 echo serverint sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);struct sockaddr_in6 serv_addr;memset((unsigned char *)&serv_addr, 0x00, sizeof(serv_addr));serv_addr.sin6_family = AF_INET6;serv_addr.sin6_port = htons(9999);serv_addr.sin6_addr = in6addr_any;const int on = 1;setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));struct sockaddr_in6 client_addr;bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));listen(sock, 20);socklen_t len = sizeof(client_addr);int conn = accept(sock, (struct sockaddr *)&client_addr,&len);
  • ipv4 & ipv6 echo server let’s see echoServer2.c set up 2 socket for ipv4 and ipv6 independently. select is key point.
  • DNS conversionsplease notice following functions: gethostbyname gethostbyaddr gethostbyname2 inet_aton, inet_ntoalet’s see hostent.c
  • hostent structure
  • addrinfo structure
  • refine our app tosupport ipv6 and ipv4grep -re “gethostby” *grep -re “inet_ntoa” *grep -re “inet_aton” *grep -re “AF_INET” *grep -re “sockaddr_in” *
  • special caseif our environment support ipv4 and ipv6, so server is simple ipv4 -> ipv4 socket ipv6 -> ipv6 socket const int on = 1; setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
  • special caseif our environment support ipv4 and ipv6, so client must consider priority of DNS conversions and retry connection.
  • special casebut...... if our environment is ipv4 only, we should use hints.ai_family = AF_INET on getaddrinfo. relatively, env is ipv6 only, please enable hints.ai_family = AF_INET6 on getaddrinfo.
  • Thanks a lot.