Multicast programming foundation under TCP / IP
Jesse Song
http://blog.9cbs.net/garibaldi/
Time Modify Content Modifier Version 2004-9-22 Jesse V0.9
This article discusses the multicast programming foundation based on TCP / IP. All examples can be running next to WINDOWS.
This article does not involve the implementation of the multicast protocol, just discusses the environment and API functions required for multicast programming. The author's discussion will be discussed separately about the implementation of multicast implementation and needs.
Multicast description
MULTICAST is a communication method for one-to-way in the network, and completes a Server's efficient data transfer in the network through packet replication. The metaphor for its image is similar to the transmission of the radio station and the TV station. The radio or TV station sends their program to a particular channel, and the recipient can select the channel according to their own preferences to listen to the program.
The communication method of traditional networks is not suitable in many occasions. If it is forcibly picked in a unicast mode, it will also affect the message transmitted on the network, and it will affect the running efficiency of the Server end, such as online games, web video conferencing. Wait. Since these occasions need to be the same data, if there is n recipients, then Server needs to send this data to the recipient, so when the recipient increases, Server The message sent by the end also needs to increase. The Server terminal will definitely have an extreme value. When the recipient is to a certain extent, it is only a problem that the network traffic is increased.
The multicast has used the place in this case. The Server side needs to do a message to the close-up group. When the network's multiple recipients are interested in this message, they can freely copy this message. In this case, regardless of how many people adds to the receiver, Server needs to do just send a message, and the network is transmitted is just a message.
Unicast legend:
Multicast legend:
Multicast
Multicast address
The 32-bit IP address is divided into five types of A, B, C, D, E, the first three are common in our daily lives, and allocated by an authority. And the Date address we need multicast addresses, the first three addresses we can think of a point, and a D-class multicast address can be considered a channel. This can be a bit for the understanding of the multicast.
About the legend of the IP address is free, please refer to relevant information about the relevant IP classification, just remember that the multicast address is from 224.0.0-239.255.255.255. The opening of 224 is the start address of the reservation address, 239. These addresses cannot be used casually, and they are also authoritative, similar to the TCP, UDP retention port.
Common multicast reservation address
Address use introduction 224.0.0.1
All hosts in this network segment (All Hosts)
[RFC 1112, JBP]
224.0.0.2
All Multicast Routers
[Jbp]
224.0.0.5
Transfer OSPF protocol (OSPF Routers)
[RFC 1583, JXM1]
224.0.0.6
Ospf designated Routers
[RFC 1583, JXM1]
224.0.0.9
Rip2 Routers
[RFC 1723, SM11]
224.0.0.12
DHCP Server / Relay Agent
[RFC 1884]
224.0.0.13all Pim Routers
[Farinacci]
Experiment: Ping 224.0.0.1 can be performed on the host to see how many hosts have respond. J
IP multicast address to the conversion of link layer addresses
The valid bit of the IP multicast address is 28 bits, is it possible to make this 28 bits to the link layer address, such as Ethernet, FDDI, token Ring (we are common for Ethernet address, is on most computers The address of the network card of the RJ45 interface). The Ethernet address is 48 digits. The IANA organization assigns the address of 00- 00-5E to the address for multicast, the maximum two digits of the Ethernet multicast address must be 01, and it will become 01- 00-5E The Ethernet is used for multicast addresses to 01- 00 - 5E - 00 - 00 - 00 to 01 - 00 - 5E - ff - ff - ff.
That is to have a 24-bit address, the 24-bit maximum bit must be 1, only 23 bits left (l I don't know why you want to trouble).
The problem occurs, the 28-bit effective bit of the IP multicast address is moving to the 23-bit L of the Ethernet, and now the solution is only 23 after the IP multicast address, that is, IP has 5 not too moving. So the case where multiple IP multicast addresses (up to 32) have appeared an Ethernet address. It seems to be a bit running
Multicast host required environment
If you want to run the multicast program on a host, this host supports the IGMP protocol. This agreement is used to join a multicast group, send a command to the router to the router, etc. The current version of the IGMP protocol is V3 (RFC3376), and now there is a very few UNIX implementation, WindowxP is implemented. Most hosts, the version implemented by the router is V2 (RFC2236), and the operation of the V1 version (RFC1112) can be implemented from V2. The difference between V2 and V1 is to combine the 4-bit version fields in the V1 version with 4-bit operation type fields into an 8-bit operation type, and the 8-bit fields not used in V1 are called the maximum response time in V2. The IGMP structure of W-write V2 is:
Struct IGMP
{
U_CHAR IGMP_TYPE;
U_CHAR IGMP_CODE;
U_CHAR IGMP_CKSUM;
Struct in_addr igmp_addr;
}
I remember that the second field was written in "TCP / IP Details 2", IGMP_CODE in "TCP / IP Details 2", I just saw it this afternoon, I don't know why, I don't know why you have such a name, look at it tomorrow. :) You can see IGMP packets (8 bytes). It may be the simplest protocol structure (what, UDP is simple? Think about UDP).
Network environment required for multicast
If the host wants to get multicast packets, adjacent routers must also support IGMP. If you want to get multicast packets on the Internet, the routers encountered in this path to Server must support IGMP, and the router must also support the source discovery. Agreement, such as MSDP, PIM_DM, PIM_SM, etc.
Multicast level
Level 0 does not support IP multicast
Level 1 only supports data for multicast groups to send data without receiving multicast groups, multicast multicast support
The understanding of these three levels should be on the Socket. After setting up a socket, you can set it to see what it needs. The typical network programs that exist now are Level0 because they do not support multicast, such as the socket used to connect to the web server to get the web page, should belong to Level0.
A concert live network live broadcast, because of the multicast, the server wants to send packets to a multicast group because he does not need to get the recipient's packet, so you can build a socket only to send data to a specific multicast group. Yes, this Socket should be Level 1
An example of a web conference, because there are many people participating in the conference, everyone needs to receive a message of other people, so I have established a socket, first join this socket to a multicast group, so that it can receive multicast group Data, then it can also use this socket to send its own state to the multicast group you join. This Socket should be Level 2 J
Multicast programming associated socket structure and functions
Int Setsockopt (Socket S, Int Level, Int Optname, Const Char Far * OptVal, Int Optlen);
INT GetSockopt (Socket S, INT Level, Int Optname, Char Far * Optval, Int Far * Optlen);
Level must be ipproto_ip. Don't ask why. What is the two functions doing in the group? Get the system's settings for multicast (such as TTL), join a multicast group, leave one of these two functions, which you have to ask? Don't look at L
Optname is a field that plays the most important role in multicast, which is related to multicast.
Value setsockopt getSockOpt IP_Multicast_loop support support IP_MULTICAST_TTL
Support IP_MULTICAST_IF support support IP_ADD_MEMBERSHIP support does not support
IP_DROP_MEMBERSHIP support does not support
IP_MULTICAST_LOOP
When the recipient is added to a multicast group, send data to this multicast group, this field setting is
No allows you to return to itself.
2. IP_MULTICAST_TTL
By default, multicast packet TTL is set to 1, which means that this message is transmitted online.
It is only possible to transfer in your own network. When you want to send it out, the router will reduce TTL 1
It turned 0, this message has been discard. example:
Char TTL;
TTL = 2;
Setsockopt (s, ipproto_ip, ip_multicast_ttl, (char *) TTL, SIZEOF (TTL));
3. IP_MULTICAST_IF
Send a local interface used in multicast reports, by default, the first address of the local interface.
Last
4. IP_ADD_MEMBERSHIP
This option and the following option are not much more broadcast, which is used to join a multicast group, an example:
Struct ip_mreq ipmr;
IPMR.IMR_INTERFACE.S_ADDR = HTONL (INADDR_Any); IPMR.IMR_MULTIADDR.S_ADDR = INET_ADDR ("234.5.6.7");
Setsockopt (s, ipproto_ip, ip_addr_membership, (char *) & IPMR, SIZEOF (IPMR));
5. IP_DROP_MEMBERSHIP
Used to leave a multicast group, use the method to use IP_ADDR_MEMBERSHIP.
Struct ip_mreq ipmr;
Int Len;
Setsockopt (s, ipproto_ip, ip_drop_membership, (char *) & ipmr, & len;
Send data to a specific multicast group
Sending data to the multicast group is the easiest, more often supports UDP? I don't know where to write L, the next version is changed, and the sending data is not needed to join this multicast group. It is just a simple set of destination addresses to make a multicast address, such as:
Socket S;
SockAddr_in Multiaddr;
s = socket (....);
/ * No need to bind * /
/ * Add data to 234.5.6.7 multicast group * /
.
Multiaddr.sin_addr.s_addr = inet_addr ("234.5.6.7");
.
/ * You can send BUF * /
Sendto (S, BUF, LEN, 0, & MULTIADDR, & LEN);
Receive data from a multicast group
Socket S;
SockAddr_in Multiaddr;
s = socket (....);
The purpose of / * bind is to specify a specific port of a local interface. This should be very well understood * /
Bind (s, ... ..);
/ * Add this Socket to a multicast group, such as add to 234.5.6.7 * /
Struct ip_mreq recote;
INT LEN = SizeOf (remote);
Remote.imr_multiaddr.s_addr = INET_ADDR ("234.5.6.7");
Remote.imr_interface.s_addr = HTONL (INADDR_Any);
/ * In this sentence, it has already been added to the multicast group * /
Setsockopt (S, Ipproto_IP, IP_ADD_MEMBERSHIP, & Remote, LEN);
/ * You can receive this multicast group data * /
Recvfrom (s, buf, maxlen, ...);
/ * Leave this multicast group * /
Setsockopt (s, ipproto_ip, ip_drop_membership, & remote, & len);
Example of multicast program
I don't know why the level of multicast is defined as three levels. The author only does itself with the first level to discuss.
Level 3 receives data from multicast groups without issuing data to the multicast group, in fact, this is an example of Level 2
Just receive data, just like the listener of the online conference.
The following is just two examples, and the reader can raise these two examples. The compilation environment should be added to the following macros, which can be placed on the beginning of the file, or put it in a separate H file.
#define reporterr (STR) /
{/
Printf ("% s% d / n", __file__, __line__); /
Printf ("% s% d / n", str, getLastError ()); /
}
#define wsareporterr (STR) /
{/
Printf ("% s% d / n", __file__, __line__); /
Printf ("% s% d / n", str, wsagetlasterror ()); /
}
#define MC_ADDR "234.5.6.7"
#define MC_Port 10000
#define maxlen 256
A Level 1 example
Just send data to multicast groups, just like a network TV's Server
#include
#include
#include
Void main (int Argc, char ** argv)
{
Socket S;
SockAddr_in mcaddr;
Int nmclen;
Int n;
CHAR BUF [Maxlen];
Wsadata wsadata;
Int Nerr;
IF (WSAStartup (0x0202, & wsadata)! = 0)
{
"" WSAStartup (..) ");
Return;
}
s = Socket (AF_INET, SOCK_DGRAM, 0);
IF (s == invalid_socket)
{
WSareporterr ("socket (...");
WSACLEANUP ();
Return;
}
McAddr.sin_Family = AF_INET;
McAddr.sin_addr.s_addr = inet_addr (mc_addr);
McAddr.sin_Port = HTONS (MC_PORT);
NMClen = SizeOf (MCADDR);
n = 0;
While (1)
{
Sleep (1000);
Sprintf (BUF, "this send to multicast group% s. SEQ:% D", MC_ADDR, N );
Nerr = SENDTO (S, BUF, Strlen (BUF), 0, (Struct SockAddr *) & mcAddr, NMCLEN;
IF (Nerr == Socket_ERROR)
{
Washporterr ("Sendto (...));
Break;
}
Printf ("% S / N", BUF);
}
WSACLEANUP ();
}
A Level 3 example
#include
#include
#include
Void main (int Argc, char ** argv)
{
Socket S;
SockAddr_in mcaddr;
Int nmclen;
CHAR BUF [Maxlen];
Struct ip_mreq ipmr;
SockAddr_in Localaddr;
Wsadata wsadata;
Int Nerr;
IF (WSAStartup (0x0202, & wsadata)! = 0)
{
"" WsaStartup (..) "); RETURN;
}
s = Socket (AF_INET, SOCK_DGRAM, 0);
IF (s == invalid_socket)
{
WSareporterr ("socket (...");
WSACLEANUP ();
Return;
}
/ * * /
MEMSET (& localaddr, 0, sizeof (localaddr));
Localaddr.sin_family = af_INet;
Localaddr.sin_addr.s_addr = HTONL (INADDR_Any) / * inet_addr (mc_addr) * /;
Localaddr.sin_Port = htons (mc_port);
Nerr = Bind (S, Struct SockAddr *) & localaddr, sizeof (localaddr));
IF (Nerr == Socket_ERROR)
{
WSareporterr ("Bind (...));
WSACLEANUP ();
Return;
}
/ * JOIN MULTICAST GROUP * /
IPMR.IMR_MULTIADDR.S_ADDR = INET_ADDR (MC_ADDR);
IPMR.IMR_Interface.s_addr = htonl (inaddr_any);
Nerr = setsockopt (s, ipproto_ip, ip_add_membership, (char *) & IPMR, SIZEOF (IPMR));
IF (Nerr == Socket_ERROR)
{
WSareporterr ("setsockopt (. Ip_add_membership ..)");
WSACLEANUP ();
Return;
}
McAddr.sin_Family = AF_INET;
McAddr.sin_addr.s_addr = inet_addr (mc_addr);
McAddr.sin_Port = HTONS (MC_PORT);
NMClen = SizeOf (MCADDR);
While (1)
{
Sleep (1000);
Nerr = Recvfrom (s, buf, maxlen, 0, (struct sockaddr *) & mcaddr, & nmclen;
IF (Nerr == Socket_ERROR)
{
CONTINUE;
}
Printf ("% S / N", BUF);
}
WSACLEANUP ();
}
bibliography
l Multicast over TCP / IP HOWTO
Juan-manriano de geeNeche jmseyas@dit.upm.es
l ftp://ftp-eng.cisco.com/ipmulticast/
Cisco Co.
l TCP / IP Detailed Volume 1 Volume 2
W. Richard Stevens, Translator: Fan Jianhua, etc.
l Windows Network Programming (2nd Edition)
Anthony Jones, Jim Ohlund, Translator: Yang Heqing