Ipv6 Socket Programming
Ipv6 Socket Programming
Contents
1. Socket 2. IPv6 3. IPv6 Socket Programming
1. 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)
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)
The application developer has the ability to fix a few TCP parameters, such as maximum buffer and maximum segment sizes.
1.4 Sockets
Connection socket
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.
server
socket( ) bind( ) listen( ) TCP conn. request TCP ACK
accept( )
recv( )
recv( ) close( )
send( ) close( )
2. IPv6
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
2.3 Example
eth0 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
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
Client
Socket open a socket Connect connect to the server Read and/or write if TCP Recvfrom and/or sendto if UDP
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);
strcpy(Buffer, Hello, world); AmmountToSend = strlen(Buffer); RetVal = send(ConnSocket, Buffer, AmmountToSend, 0); RetVal = recv(ConnSocket, Buffer, Buffer, BUFFER_SIZE, 0); closesocket(ConnSocket); }
Server
#define MAXSOCK 2 struct 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;
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++;
/*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); }
When a new connection request arrives at a server, the server accepts and invokes a new process to handle the new client.