1 /* $Id: ipx_win.c,v 1.5 2003-02-28 23:34:15 btb Exp $ */
5 * IPX driver using BSD style sockets
6 * Mostly taken from dosemu
23 #define _do_mprintf(n, format, args...) printf(format, ## args)
24 #define mprintf(args) _do_mprintf args
26 //#define n_printf(format, args...) mprintf((1, format, ## args))
28 static int ipx_win_GetMyAddress( void )
32 struct sockaddr_ipx ipxs;
33 struct sockaddr_ipx ipxs2;
37 // sock=socket(AF_IPX,SOCK_DGRAM,PF_IPX);
38 sock=socket(AF_IPX,SOCK_DGRAM,0);
42 mprintf((1,"IPX: could not open socket in GetMyAddress\n"));
46 /* bind this socket to network 0 */
47 ipxs.sa_family=AF_IPX;
48 #ifdef IPX_MANUAL_ADDRESS
49 memcpy(ipxs.sa_netnum, ipx_MyAddress, 4);
51 memset(ipxs.sa_netnum, 0, 4);
55 if(bind(sock,(struct sockaddr *)&ipxs,sizeof(ipxs))==-1)
57 mprintf((1,"IPX: could bind to network 0 in GetMyAddress\n"));
63 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
64 mprintf((1,"IPX: could not get socket name in GetMyAddress\n"));
69 memcpy(ipx_MyAddress, ipxs2.sa_netnum, 4);
70 for (i = 0; i < 6; i++) {
71 ipx_MyAddress[4+i] = ipxs2.sa_nodenum[i];
73 /* printf("My address is 0x%.4X:%.2X.%.2X.%.2X.%.2X.%.2X.%.2X\n", *((int *)ipxs2.sa_netnum), ipxs2.sa_nodenum[0],
74 ipxs2.sa_nodenum[1], ipxs2.sa_nodenum[2], ipxs2.sa_nodenum[3], ipxs2.sa_nodenum[4], ipxs2.sa_nodenum[5]);*/
82 static int ipx_win_OpenSocket(ipx_socket_t *sk, int port)
84 int sock; /* sock here means Linux socket handle */
86 struct sockaddr_ipx ipxs;
88 struct sockaddr_ipx ipxs2;
90 /* DANG_FIXTHIS - kludge to support broken linux IPX stack */
91 /* need to convert dynamic socket open into a real socket number */
93 mprintf((1,"IPX: using socket %x\n", nextDynamicSocket));
94 port = nextDynamicSocket++;
97 /* do a socket call, then bind to this port */
98 // sock = socket(AF_IPX, SOCK_DGRAM, PF_IPX);
99 sock = socket(AF_IPX, SOCK_DGRAM, 0);
101 mprintf((1,"IPX: could not open IPX socket.\n"));
107 /* Permit broadcast output */
108 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
109 (const char *)&opt, sizeof(opt)) == -1) {
110 mprintf((1,"IPX: could not set socket option for broadcast.\n"));
113 ipxs.sa_family = AF_IPX;
114 memcpy(ipxs.sa_netnum, ipx_MyAddress, 4);
115 // *((unsigned int *)&ipxs.sa_netnum[0]) = *((unsigned int *)&ipx_MyAddress[0]);
116 /* ipxs.sa_netnum = htonl(MyNetwork); */
117 memset(ipxs.sa_nodenum, 0, 6);
118 // bzero(ipxs.sa_nodenum, 6); /* Please fill in my node name */
119 ipxs.sa_socket = htons((short)port);
121 /* now bind to this port */
122 if (bind(sock, (struct sockaddr *) &ipxs, sizeof(ipxs)) == -1) {
123 mprintf((1,"IPX: could not bind socket to address\n"));
129 // len = sizeof(ipxs2);
130 // if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
131 // //n_printf("IPX: could not get socket name in IPXOpenSocket\n");
132 // closesocket( sock );
135 // port = htons(ipxs2.sa_socket);
136 // //n_printf("IPX: opened dynamic socket %04x\n", port);
139 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
140 mprintf((1,"IPX: could not get socket name in IPXOpenSocket\n"));
145 port = htons(ipxs2.sa_socket);
146 mprintf((1,"IPX: opened dynamic socket %04x\n", port));
149 memcpy(ipx_MyAddress, ipxs2.sa_netnum, 4);
150 memcpy(ipx_MyAddress + 4, ipxs2.sa_nodenum, 6);
157 static void ipx_win_CloseSocket(ipx_socket_t *mysock) {
158 /* now close the file descriptor for the socket, and free it */
159 mprintf((1,"IPX: closing file descriptor on socket %x\n", mysock->socket));
160 closesocket(mysock->fd);
163 static int ipx_win_SendPacket(ipx_socket_t *mysock, IPXPacket_t *IPXHeader,
164 u_char *data, int dataLen) {
165 struct sockaddr_ipx ipxs;
167 ipxs.sa_family = AF_IPX;
168 /* get destination address from IPX packet header */
169 memcpy(&ipxs.sa_netnum, IPXHeader->Destination.Network, 4);
170 /* if destination address is 0, then send to my net */
171 if ((*(unsigned int *)&ipxs.sa_netnum) == 0) {
172 (*(unsigned int *)&ipxs.sa_netnum)= *((unsigned int *)&ipx_MyAddress[0]);
173 /* ipxs.sa_netnum = htonl(MyNetwork); */
175 memcpy(&ipxs.sa_nodenum, IPXHeader->Destination.Node, 6);
176 memcpy(&ipxs.sa_socket, IPXHeader->Destination.Socket, 2);
177 // ipxs.sa_type = IPXHeader->PacketType;
178 /* ipxs.sipx_port=htons(0x452); */
179 return sendto(mysock->fd, data, dataLen, 0,
180 (struct sockaddr *) &ipxs, sizeof(ipxs));
183 static int ipx_win_ReceivePacket(ipx_socket_t *s, char *buffer, int bufsize,
184 struct ipx_recv_data *rd) {
186 struct sockaddr_ipx ipxs;
189 if ((size = recvfrom(s->fd, buffer, bufsize, 0,
190 (struct sockaddr *) &ipxs, &sz)) <= 0)
192 memcpy(rd->src_network, ipxs.sa_netnum, 4);
193 memcpy(rd->src_node, ipxs.sa_nodenum, 6);
194 rd->src_socket = ipxs.sa_socket;
195 rd->dst_socket = s->socket;
196 // rd->pkt_type = ipxs.sipx_type;
201 struct ipx_driver ipx_win = {
202 ipx_win_GetMyAddress,
206 ipx_win_ReceivePacket,
207 ipx_general_PacketReady