ipv6 socket programming

27
IPv6 Socket Programming 2004.10.6 Joonbok Lee KAIST

Upload: rafa-sanchez

Post on 25-Oct-2014

36 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Ipv6 Socket Programming

IPv6 Socket Programming

2004.10.6Joonbok Lee

KAIST

Page 2: Ipv6 Socket Programming

Contents

1. Socket 2. IPv63. IPv6 Socket Programming

Page 3: Ipv6 Socket Programming

1. Socket

Page 4: Ipv6 Socket Programming

1.1 What is a socket?• Socket

– The combination of an IP address and a port number. (RFC 793 ,original TCP specification)

– The name of the Berkeley-derived application programming interfaces (APIs) for applications using TCP/IP protocols.

– Two types• Stream socket : reliable two-way connected communication

streams• Datagram socket

• Socket pair– Specify the two end points that uniquely identifies each

TCP connection in an internet.– 4-tuple: (client IP address, client port number, server IP

address, server port number)

Page 5: Ipv6 Socket Programming

1.2 Client-server applications

• Implementation of a protocol standard defined in an RFC. (FTP, HTTP, SMTP…)– Conform to the rules dictated by the RFC.– Should use the port number associated with the protocol.

• Proprietary client-server application.– A single developer( or team) creates both client and server

program.– The developer has complete control.– Must be careful not to use one of the well-known port

number defined in the RFCs.

* well-known port number : managed by the Internet Assigned Numbers Authority(IANA)

Page 6: Ipv6 Socket Programming

1.3 Socket Programming with TCP

Figure 2.6-1: Processes communicating through TCP sockets

The application developer has the ability to fix a few TCP parameters, such as maximum buffer and maximum segment sizes.

Page 7: Ipv6 Socket Programming

1.4 Sockets

Figure 2.6-2: Client socket, welcoming socket and connection socket

Page 8: Ipv6 Socket Programming

1.5 Sockets for server and client

• Server– Welcoming socket

• Welcomes some initial contact from a client.– Connection socket

• It is created at initial contact of client.• New socket that is dedicated to the particular client.

• Client– Client socket

• Initiate a TCP connection to the server by creating a socket object. (Three-way handshake)

• Specify the address of the server process, namely, the IP address of the server and the port number of the process.

Page 9: Ipv6 Socket Programming

1.6 Socket Programming- TCP

client server

socket( )bind( )connect( )

socket( )bind( )listen( )

accept( )send( )

recv( )

close( ) close( )

recv( )send( )

TCP conn. request

TCP ACK

Page 10: Ipv6 Socket Programming

2. IPv6

Page 11: Ipv6 Socket Programming

2.1 IPv4 and IPv6

• IPv4– RFC 791, which was published in 1981– 32bit address space: 4,294,967,296 (232) addresses

• IPv6– 128bit address space: 3.4 x 1038

• 67billion billion addresses per cm2 of the planet space.– Hierarchical address architecture– Security (built-in IPsec)– Autoconfiguration– IETF IPv6 Working Group

Page 12: Ipv6 Socket Programming

2.2 IPv6 Address

• RFC 2373• x:x:x:x:x:x:x:x (x is 16bit)

ex) 3ffe:8041:2:1:bc16:6ee6:26ab:7ba8• ‘::’ indicates multiple groups of 16 bits of zeros.

ex) 3ffe:8041:0:0:0:0:0:3 3ffe:8041::3• IPv6 Address Structure: Prefix + Interface ID

ex) 3ffe:8041:2:1:bc16:6ee6:26ab:7ba8/64

Page 13: Ipv6 Socket Programming

2.3 Exampleeth0 Link encap:Ethernet HWaddr 00:04:76:2F:CC:34 inet addr:134.75.85.132 Bcast:134.75.255.255

Mask:255.255.255.224 inet6 addr: 2001:320:1a10:3:204:76ff:fe2f:cc34/64 Scope:Global inet6 addr: fe80::204:76ff:fe2f:cc34/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1595908 errors:0 dropped:0 overruns:0 frame:0 TX packets:24722783 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1091245010 (1040.6 Mb) TX bytes:1288208803 (1228.5

Mb) Interrupt:177

Page 14: Ipv6 Socket Programming

3. IPv6 Socket Programming

Page 15: Ipv6 Socket Programming

3.1 Goal

• Develope IPv6-aware (or IPv6-enable) application.

• Application Type– IPv4-only: not able to handle IPv6 addresses– IPv6-aware: can communicate with nodes that do not

have IPv4 addresses– IPv6-enable: in addition to being IPv6-aware, takes

advantage of some IPv6 specific features such as flow labels

– IPv6-required: requires some IPv6 specific feature and therefore cannot operate over IPv4

Page 16: Ipv6 Socket Programming

3.2 Changes socket API for IPv6 Support

• New socket address structure to carry IPv6 address

• New address conversion functions and several new socket options

• RFC 3493

Page 17: Ipv6 Socket Programming

3.3 Typical IPv6 Code sequence

• Same with IPv4– Server

• Socket – open a socket• Bind – bind our local address to the socket• Listen – tell that we are listening to a port• Accept – wait for connection• Read and/or write if TCP • Recvfrom and/or sendto if UDP

– Client• Socket – open a socket• Connect – connect to the server• Read and/or write if TCP• Recvfrom and/or sendto if UDP

Page 18: Ipv6 Socket Programming

3.4 What need to be changed

• Socket Interface– New protocol family name: PF_INET6

• Address data structures– Net address family name: AF_INET6– in6_addr structure – sockaddr_in6 structure

• Name-to-address translation functions– inet_pton, inet_ntop

• Address conversion functions– getnameinfo– getaddrinfo

Page 19: Ipv6 Socket Programming
Page 20: Ipv6 Socket Programming

3.5 IPv6-enable application example

int main(int argc, char **argv){

char Buffer[BUFFER_SIZE]; char *Server = DEFAULT_SERVER; /* “143.248.226.231” “2001:320:1a10:3:204:76ff:fe2f:cc34” “cosmos.kaist.ac.kr” */ int Family = DEFAULT_FAMILY; /* AF_INET, AF_INET6, AF_UNSPEC */ int SocketType = DEFAULT_SOCKTYPE; /* SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, … */ char *Port = DEFAULT_PORT; int i, RetVal, AddrLen, AmountToSend; struct addrinfo Hints, *AddrInfo, *AI; socket ConnSocket; struct sockaddr_storage Addr;

Client

Page 21: Ipv6 Socket Programming

memset(&Hints, 0, sizeof(Hints));Hints.ai_family = AF_UNSPEC; /* AF_INET: IPv4 address, AF_INET6: IPv6 address, AF_UNSPEC: IPv4 and IPv6 address */Hints.ai_socktype = SOCK_STREAM; /* SOCK_STREAM: TCP, SOCK_DGRAM: UDP */RetVal = getaddrinfo(Server, Port, &Hints, &AddrInfo);if (RetVal != 0) { fprintf(stderr, "Cannot resolve address [%s] and port [%s], error %d: %s\n", Server, Port,

RetVal, gai_strerror(RetVal)); return -1;}

for (AI = AddrInfo; AI != NULL; AI = AI->ai_next) { // Open a socket with the correct address family for this address. // if( AI->ai_family == AF_INET6 ){ ConnSocket = socket(AI->ai_family, AI->ai_socktype, AI-> ai_protocol); if (ConnSocket == INVALID_SOCKET) continue;

if (connect(ConnSocket, AI->ai_addr, AI->ai_addrlen) != SOCKET_ERROR) break; // }

}

freeaddrinfo(Addrinfo);

Page 22: Ipv6 Socket Programming

strcpy(Buffer, “Hello, world”);AmmountToSend = strlen(Buffer);RetVal = send(ConnSocket, Buffer, AmmountToSend, 0);RetVal = recv(ConnSocket, Buffer, Buffer, BUFFER_SIZE, 0);

closesocket(ConnSocket);

}

Page 23: Ipv6 Socket Programming

#define MAXSOCK 2struct addrinfo hints, *res;struct sockaddr_storage from;int error, sockfd[MAXSOCK], nsock=0, connsock, fromlen;memset(&hints, 0, sizeof(hints));hints.ai_flags = AI_PASSIVE;hints.ai_family = AF_UNSPEC;hints.ai_socktype = SOCK_STREAM;

error = getaddrinfo(NULL, SERVICE, &hints, &res);if (error != 0) {/* handle getaddrinfo error */}

for (aip=res; aip && nsock < MAXSOCK; aip=aip->ai_next) { sockfd[nsock] = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol); if (sockfd[nsock] < 0) { /* handle socket error */ }

Server

Page 24: Ipv6 Socket Programming

else { int on = 1; /* optional: works better if dual-binding to wildcard address */ if (aip->ai_family == AF_INET6) { setsockopt(sockfd[nsock], IPPROTO_IPV6, IPV6_V6ONLY, (char *)&on,

sizeof(on)); /* errors are ignored */ } if (bind(sockfd[nsock], aip->ai_addr, aip->ai_addrlen) < 0 ) { /* handle bind error */ close(sockfd[nsock]); continue; } if (listen(sockfd[nsock], SOMAXCONN) < 0) { /* handle listen errors */ close(sockfd[nsock]); continue; }}

nsock++;}freeaddrinfo(res);/* check that we were able to obtain the sockets */

Page 25: Ipv6 Socket Programming

/*select socket and close other socket*/

fromlen = sizeof(from); connsock = accept(selectedsock, &from, fromlen); /* handle accept error */

ammountread = recv(connsock, buffer, sizeof(buffer), 0); /* handle recv error */

send(connsock, buffer, ammountread, 0); /* handle send error */

closesocket(connsock);

}

Page 26: Ipv6 Socket Programming

3.6 Concurrent server

• Servers need to handle a new connection request while processing previous requests.– Most TCP servers are designed to be concurrent.

• When a new connection request arrives at a server, the server accepts and invokes a new process to handle the new client.

Page 27: Ipv6 Socket Programming

Appendix IPv6 Support

• Linux: Kernel 2.4.x – http://cosmos.kaist.ac.kr/~shlee/ipv6/doc/

ipv6linux.txt

• Windows XP, windows 2000, windows 2003– http://www.microsoft.com/windowsserver2003/

technologies/ipv6/default.mspx