4 * "ipx driver" for IPv4 multicasting
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
27 #include <sys/socket.h>
29 #define closesocket(x) close(x)
33 #include "ipx_mcast4.h"
39 //#define IPX_MCAST4_DEBUG
41 extern unsigned char ipx_MyAddress[10];
43 #define UDP_BASEPORT 28342
44 #define PORTSHIFT_TOLERANCE 0x100
45 #define MAX_PACKETSIZE 8192
47 /* OUR port. Can be changed by "@X[+=]..." argument (X is the shift value)
49 static int baseport=UDP_BASEPORT;
51 static struct in_addr game_addr; // The game's multicast address
53 #define MSGHDR "IPX_mcast4: "
55 #ifdef IPX_MCAST4_DEBUG
56 static void msg(const char *fmt, ...)
60 fputs(MSGHDR, stdout);
70 #define FAIL(...) do{ nm_messagebox("Error", 1, "Ok", __VA_ARGS__); return -1; } while (0)
72 #ifdef IPX_MCAST4_DEBUG
73 /* Dump raw form of IP address/port by fancy output to user
75 static void dumpraddr(unsigned char *a)
79 printf("[%u.%u.%u.%u]", a[0], a[1], a[2], a[3]);
80 port=(signed short)ntohs(*(unsigned short *)(a+4));
81 if (port) printf(":%+d",port);
84 /* Like dumpraddr() but for structure "sockaddr_in"
86 static void dumpaddr(struct sockaddr_in *sin)
89 unsigned char qhbuf[8];
91 memcpy(qhbuf + 0, &sin->sin_addr, 4);
92 ports = htons(((short)ntohs(sin->sin_port)) - UDP_BASEPORT);
93 memcpy(qhbuf + 4, &ports, 2);
98 // The multicast address for Descent 2 game announcements.
99 // TODO: Pick a better address for this
100 #define DESCENT2_ANNOUNCE_ADDR inet_addr("239.255.1.2")
102 /* Open the socket and subscribe to the multicast session */
103 static int ipx_mcast4_OpenSocket(ipx_socket_t *sk, int port)
107 struct sockaddr_in sin;
110 if((sk->fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
113 FAIL("socket() creation failed on port %d: %m", port);
117 sin.sin_family = AF_INET;
118 sin.sin_addr.s_addr = htonl(INADDR_ANY);
119 sin.sin_port = htons(baseport);
120 if (bind(sk->fd, (struct sockaddr *)&sin, sizeof(sin)))
122 if (closesocket(sk->fd))
123 msg("closesocket() failed during error recovery: %m");
125 FAIL("bind() to UDP port %d failed: %m", baseport);
128 // Set the TTL so the packets can get out of the local network.
129 if (setsockopt(sk->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const void *)&ttl, sizeof(ttl)) < 0)
130 FAIL("setsockopt() failed to set TTL to 128");
132 // Disable multicast loopback
134 if (setsockopt(sk->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const void *)&loop, sizeof(loop)) < 0)
135 FAIL("setsockopt() failed to disable multicast loopback: %m");
137 // Subscribe to the game announcement address
138 memset(&mreq, 0, sizeof(mreq));
139 mreq.imr_multiaddr.s_addr = DESCENT2_ANNOUNCE_ADDR;
140 mreq.imr_interface.s_addr = INADDR_ANY;
141 if (setsockopt(sk->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0)
142 FAIL("setsockopt() failed to subscribe to the game announcement multicast group");
144 // We're not subscribed to a game address yet
145 game_addr.s_addr = 0;
151 static void ipx_mcast4_CloseSocket(ipx_socket_t *sk)
153 if(closesocket(sk->fd) < 0)
158 static int ipx_mcast4_SendPacket(ipx_socket_t *sk, IPXPacket_t *IPXHeader, u_char *data, int dataLen)
160 struct sockaddr_in toaddr;
163 msg("SendPacket enter, dataLen=%d", dataLen);
165 if(dataLen < 0 || dataLen > MAX_PACKETSIZE)
168 toaddr.sin_family = AF_INET;
169 memcpy(&toaddr.sin_addr, IPXHeader->Destination.Node + 0, 4);
170 //toaddr.sin_port = htons(((short)ntohs(*(unsigned short *)(IPXHeader->Destination.Node + 4))) + UDP_BASEPORT);
171 // For now, just use the same port for everything
172 toaddr.sin_port = htons(UDP_BASEPORT);
174 // If it's the broadcast address, then we want to send it to the
175 // GAME ANNOUNCEMENT address.
176 // Data to be sent to the GAME has the destination already set by
177 // ipx_mcast4_SendGamePacket
178 if(toaddr.sin_addr.s_addr == INADDR_BROADCAST)
179 toaddr.sin_addr.s_addr = DESCENT2_ANNOUNCE_ADDR;
181 #ifdef IPX_MCAST4_DEBUG
182 printf(MSGHDR "sendto((%d),Node=[4] %02X %02X,Socket=%02X %02X,s_port=%u,",
184 IPXHeader->Destination.Node[4], IPXHeader->Destination.Node[5],
185 IPXHeader->Destination.Socket[0], IPXHeader->Destination.Socket[1],
186 ntohs(toaddr.sin_port));
191 i = (int)sendto(sk->fd, (const void *)data, dataLen, 0, (struct sockaddr *)&toaddr, sizeof(toaddr));
195 static int ipx_mcast4_ReceivePacket(ipx_socket_t *sk, char *outbuf, int outbufsize, struct ipx_recv_data *rd)
198 struct sockaddr_in fromaddr;
199 socklen_t fromaddrsize = sizeof(fromaddr);
201 if ((size = (int)recvfrom(sk->fd, outbuf, outbufsize, 0, (struct sockaddr*)&fromaddr, &fromaddrsize)) < 0)
204 #ifdef IPX_MCAST4_DEBUG
205 printf(MSGHDR "Got packet from ");
210 // We have the packet, now fill out the receive data.
211 memset(rd, 0, sizeof(*rd));
212 memcpy(rd->src_node, &fromaddr.sin_addr, 4);
213 // TODO: Include the port like in ipx_udp.c
219 /* Handle the netgame aux data
220 * Byte 0 is the protocol version number.
221 * Bytes 1-4 are the IPv4 multicast session to join, in network byte order.
223 static int ipx_mcast4_HandleNetgameAuxData(ipx_socket_t *sk, const u_char buf[NETGAME_AUX_SIZE])
225 // Extract the multicast session and subscribe to it. We should
226 // now be getting packets intended for the players of this game.
228 // Note that we stay subscribed to the game announcement session,
229 // so we can reply to game info requests
233 // Check the protocol version
234 if(buf[0] != IPX_MCAST4_VERSION)
236 FAIL("mcast4 protocol\nversion mismatch!\nGame version is %02x,\nour version is %02x", buf[0], IPX_MCAST4_VERSION);
239 // Get the multicast session
240 memcpy(&game_addr, buf + 1, sizeof(game_addr));
242 #ifdef IPX_MCAST4_DEBUG
244 struct sockaddr_in tmpaddr;
245 tmpaddr.sin_addr = game_addr;
246 tmpaddr.sin_port = 0;
248 printf("Handling netgame aux data: Subscribing to ");
254 // Set the TTL so the packets can get out of the local network.
255 if(setsockopt(sk->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const void *)&ttl, sizeof(ttl)) < 0)
256 FAIL("setsockopt() failed to set TTL to 128");
258 memset(&mreq, 0, sizeof(mreq));
259 mreq.imr_multiaddr = game_addr;
260 mreq.imr_interface.s_addr = INADDR_ANY;
262 if(setsockopt(sk->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0)
263 FAIL("setsockopt() failed to subscribe to the game group");
268 /* Create the netgame aux data.
269 * Byte 0 is the protcol version number.
270 * Bytes 1-4 hold the IPv4 multicast session for the game.
272 static void ipx_mcast4_InitNetgameAuxData(ipx_socket_t *sk, u_char buf[NETGAME_AUX_SIZE])
275 Assert(game_addr.s_addr == 0);
277 // The first byte is the version number
278 buf[0] = IPX_MCAST4_VERSION;
280 // Generate a random session
281 // game_addr = inet_makeaddr(239*256 + 255, d_rand() % 0xFFFF);
282 snprintf(addr, sizeof(addr)-1, "%i.%i.%i.%i", 239, 255, d_rand() % 0xFF, d_rand() % 0xFF);
283 game_addr.s_addr = inet_addr(addr);
284 memcpy(buf + 1, &game_addr, sizeof(game_addr));
286 // Since we're obviously the hosting machine, subscribe to this address
287 ipx_mcast4_HandleNetgameAuxData(sk, buf);
290 static void ipx_mcast4_HandleLeaveGame(ipx_socket_t *sk)
292 // We left the game, so unsubscribe from its multicast session
295 Assert(game_addr.s_addr != 0);
297 #ifdef IPX_MCAST4_DEBUG
298 printf("Unsubscribing from game's multicast group: ");
299 dumpraddr(&game_addr.s_addr);
303 mreq.imr_multiaddr = game_addr;
304 mreq.imr_interface.s_addr = INADDR_ANY;
305 if(setsockopt(sk->fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0)
306 msg("setsockopt() failed unsubscribing from previous group!");
307 game_addr.s_addr = 0;
310 // Send a packet to every member of the game. We can just multicast it here.
311 static int ipx_mcast4_SendGamePacket(ipx_socket_t *sk, ubyte *data, int dataLen)
313 struct sockaddr_in toaddr;
316 memset(&toaddr, 0, sizeof(toaddr));
317 toaddr.sin_family = AF_INET;
318 toaddr.sin_addr = game_addr;
319 toaddr.sin_port = htons(UDP_BASEPORT);
321 msg("ipx_mcast4_SendGamePacket");
323 i = (int)sendto(sk->fd, (const void *)data, dataLen, 0, (struct sockaddr *)&toaddr, sizeof(toaddr));
328 // Pull this in from ipx_udp.c since it's the same for us.
329 extern int ipx_udp_GetMyAddress(void);
331 struct ipx_driver ipx_mcast4 = {
332 ipx_udp_GetMyAddress,
333 ipx_mcast4_OpenSocket,
334 ipx_mcast4_CloseSocket,
335 ipx_mcast4_SendPacket,
336 ipx_mcast4_ReceivePacket,
337 ipx_general_PacketReady,
338 ipx_mcast4_InitNetgameAuxData,
339 ipx_mcast4_HandleNetgameAuxData,
340 ipx_mcast4_HandleLeaveGame,
341 ipx_mcast4_SendGamePacket