3 * IPX driver using BSD style sockets
4 * Mostly taken from dosemu
13 #include <sys/types.h>
14 #include <sys/socket.h>
17 #ifdef HAVE_NETIPX_IPX_H
18 #include <netipx/ipx.h>
20 # include <linux/ipx.h>
26 #include <netinet/in.h>
36 #define n_printf(format, args...) mprintf((1, format, ## args))
37 #define enter_priv_on()
38 #define leave_priv_setting()
41 static int ipx_bsd_GetMyAddress(void)
44 struct sockaddr_ipx ipxs;
45 struct sockaddr_ipx ipxs2;
49 sock=socket(AF_IPX,SOCK_DGRAM,PF_IPX);
51 n_printf("IPX: could not open socket in GetMyAddress\n");
55 /* bind this socket to network 0 */
56 ipxs.sipx_family=AF_IPX;
57 #ifdef IPX_MANUAL_ADDRESS
58 memcpy(&ipxs.sipx_network, ipx_MyAddress, 4);
64 if (bind(sock,(struct sockaddr *)&ipxs,sizeof(ipxs)) == -1) {
65 n_printf("IPX: could bind to network 0 in GetMyAddress\n");
71 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
72 n_printf("IPX: could not get socket name in GetMyAddress\n");
77 memcpy(ipx_MyAddress, &ipxs2.sipx_network, 4);
78 for (i = 0; i < 6; i++) {
79 ipx_MyAddress[4+i] = ipxs2.sipx_node[i];
85 static int ipx_bsd_OpenSocket(ipx_socket_t *sk, int port)
87 int sock; /* sock here means Linux socket handle */
89 struct sockaddr_ipx ipxs;
91 struct sockaddr_ipx ipxs2;
93 /* DANG_FIXTHIS - kludge to support broken linux IPX stack */
94 /* need to convert dynamic socket open into a real socket number */
97 n_printf("IPX: using socket %x\n", nextDynamicSocket);
98 port = nextDynamicSocket++;
101 /* do a socket call, then bind to this port */
102 sock = socket(AF_IPX, SOCK_DGRAM, PF_IPX);
104 n_printf("IPX: could not open IPX socket.\n");
110 /* turn on socket debugging */
113 if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, &opt, sizeof(opt)) == -1) {
114 leave_priv_setting();
115 n_printf("IPX: could not set socket option for debugging.\n");
118 leave_priv_setting();
122 /* Permit broadcast output */
124 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
125 &opt, sizeof(opt)) == -1) {
126 leave_priv_setting();
127 n_printf("IPX: could not set socket option for broadcast.\n");
131 /* allow setting the type field in the IPX header */
133 #if 0 /* this seems to be wrong: IPX_TYPE can only be set on level SOL_IPX */
134 if (setsockopt(sock, SOL_SOCKET, IPX_TYPE, &opt, sizeof(opt)) == -1) {
136 /* the socket _is_ an IPX socket, hence it first passes ipx_setsockopt()
137 * in file linux/net/ipx/af_ipx.c. This one handles SOL_IPX itself and
138 * passes SOL_SOCKET-levels down to sock_setsockopt().
139 * Hence I guess the below is correct (can somebody please verify this?)
140 * -- Hans, June 14 1997
142 if (setsockopt(sock, SOL_IPX, IPX_TYPE, &opt, sizeof(opt)) == -1) {
144 leave_priv_setting();
145 n_printf("IPX: could not set socket option for type.\n");
149 ipxs.sipx_family = AF_IPX;
150 ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
151 /* ipxs.sipx_network = htonl(MyNetwork); */
152 bzero(ipxs.sipx_node, 6); /* Please fill in my node name */
153 ipxs.sipx_port = htons(port);
155 /* now bind to this port */
156 if (bind(sock, (struct sockaddr *) &ipxs, sizeof(ipxs)) == -1) {
157 n_printf("IPX: could not bind socket to address\n");
159 leave_priv_setting();
165 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
166 n_printf("IPX: could not get socket name in IPXOpenSocket\n");
168 leave_priv_setting();
171 port = htons(ipxs2.sipx_port);
172 n_printf("IPX: opened dynamic socket %04x\n", port);
175 leave_priv_setting();
181 static void ipx_bsd_CloseSocket(ipx_socket_t *mysock) {
182 /* now close the file descriptor for the socket, and free it */
183 n_printf("IPX: closing file descriptor on socket %x\n", mysock->socket);
187 static int ipx_bsd_SendPacket(ipx_socket_t *mysock, IPXPacket_t *IPXHeader,
188 u_char *data, int dataLen) {
189 struct sockaddr_ipx ipxs;
191 ipxs.sipx_family = AF_IPX;
192 /* get destination address from IPX packet header */
193 memcpy(&ipxs.sipx_network, IPXHeader->Destination.Network, 4);
194 /* if destination address is 0, then send to my net */
195 if (ipxs.sipx_network == 0) {
196 ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
197 /* ipxs.sipx_network = htonl(MyNetwork); */
199 memcpy(&ipxs.sipx_node, IPXHeader->Destination.Node, 6);
200 memcpy(&ipxs.sipx_port, IPXHeader->Destination.Socket, 2);
201 ipxs.sipx_type = IPXHeader->PacketType;
202 /* ipxs.sipx_port=htons(0x452); */
203 return sendto(mysock->fd, data, dataLen, 0,
204 (struct sockaddr *) &ipxs, sizeof(ipxs));
207 static int ipx_bsd_ReceivePacket(ipx_socket_t *s, char *buffer, int bufsize,
208 struct ipx_recv_data *rd) {
211 struct sockaddr_ipx ipxs;
214 if ((size = recvfrom(s->fd, buffer, bufsize, 0,
215 (struct sockaddr *) &ipxs, &sz)) <= 0)
217 memcpy(rd->src_network, &ipxs.sipx_network, 4);
218 memcpy(rd->src_node, ipxs.sipx_node, 6);
219 rd->src_socket = ipxs.sipx_port;
220 rd->dst_socket = s->socket;
221 rd->pkt_type = ipxs.sipx_type;
226 struct ipx_driver ipx_bsd = {
227 ipx_bsd_GetMyAddress,
231 ipx_bsd_ReceivePacket,
232 ipx_general_PacketReady,
233 NULL, // InitNetgameAuxData
234 NULL, // HandleNetgameAuxData
235 NULL, // HandleLeaveGame
236 NULL // SendGamePacket