]> icculus.org git repositories - btb/d2x.git/blob - arch/linux/ipx_lin.c
support shareware datafiles\!
[btb/d2x.git] / arch / linux / ipx_lin.c
1 /*
2  * $Source: /cvs/cvsroot/d2x/arch/linux/ipx_lin.c,v $
3  * $Revision: 1.4 $
4  * $Author: bradleyb $
5  * $Date: 2001-10-19 07:39:26 $
6  *
7  * Linux IPX
8  *
9  * $Log: not supported by cvs2svn $
10  * Revision 1.3  2001/10/19 07:29:37  bradleyb
11  * Brought linux networking in line with d1x, moved some arch/linux_* stuff to arch/linux/
12  *
13  * Revision 1.2  2001/01/29 13:35:08  bradleyb
14  * Fixed build system, minor fixes
15  *
16  */
17
18 #ifdef HAVE_CONFIG_H
19 #include <conf.h>
20 #endif
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <string.h>
26
27 #ifdef HAVE_NETIPX_IPX_H
28 #include <netipx/ipx.h>
29 #else
30 # include <linux/ipx.h>
31 # ifndef IPX_TYPE
32 #  define IPX_TYPE 1
33 # endif
34 #endif
35
36 #include <netinet/in.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include "pstypes.h"
40
41 #include "ipx_ld.h"
42 #include "ipx_hlpr.h"
43 #include "ipx_lin.h"
44
45 extern unsigned char ipx_MyAddress[10];
46
47 int 
48 ipx_linux_GetMyAddress( void )
49 {
50   int sock;
51   struct sockaddr_ipx ipxs;
52   struct sockaddr_ipx ipxs2;
53   int len;
54   int i;
55   
56   sock=socket(AF_IPX,SOCK_DGRAM,PF_IPX);
57   if(sock==-1)
58   {
59     n_printf("IPX: could not open socket in GetMyAddress\n");
60     return(-1);
61   }
62
63   /* bind this socket to network 0 */  
64   ipxs.sipx_family=AF_IPX;
65 #ifdef IPX_MANUAL_ADDRESS
66   memcpy(&ipxs.sipx_network, ipx_MyAddress, 4);
67 #else
68   ipxs.sipx_network=0;
69 #endif  
70   ipxs.sipx_port=0;
71   
72   if(bind(sock,(struct sockaddr *)&ipxs,sizeof(ipxs))==-1)
73   {
74     n_printf("IPX: could bind to network 0 in GetMyAddress\n");
75     close( sock );
76     return(-1);
77   }
78   
79   len = sizeof(ipxs2);
80   if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
81     n_printf("IPX: could not get socket name in GetMyAddress\n");
82     close( sock );
83     return(-1);
84   }
85
86   memcpy(ipx_MyAddress, &ipxs2.sipx_network, 4);
87   for (i = 0; i < 6; i++) {
88     ipx_MyAddress[4+i] = ipxs2.sipx_node[i];
89   }
90   close( sock );
91   return(0);
92 }
93
94 int
95 ipx_linux_OpenSocket(ipx_socket_t *sk, int port)
96 {
97   int sock;                     /* sock here means Linux socket handle */
98   int opt;
99   struct sockaddr_ipx ipxs;
100   int len;
101   struct sockaddr_ipx ipxs2;
102
103   /* DANG_FIXTHIS - kludge to support broken linux IPX stack */
104   /* need to convert dynamic socket open into a real socket number */
105 /*  if (port == 0) {
106     n_printf("IPX: using socket %x\n", nextDynamicSocket);
107     port = nextDynamicSocket++;
108   }
109 */
110   /* do a socket call, then bind to this port */
111   sock = socket(AF_IPX, SOCK_DGRAM, PF_IPX);
112   if (sock == -1) {
113     n_printf("IPX: could not open IPX socket.\n");
114     return -1;
115   }
116
117 #ifdef DOSEMU
118   opt = 1;
119   /* turn on socket debugging */
120   if (d.network) {
121     enter_priv_on();
122     if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, &opt, sizeof(opt)) == -1) {
123       leave_priv_setting();
124       n_printf("IPX: could not set socket option for debugging.\n");
125       return -1;
126     }
127     leave_priv_setting();
128   }
129 #endif  
130   opt = 1;
131   /* Permit broadcast output */
132   enter_priv_on();
133   if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
134                  &opt, sizeof(opt)) == -1) {
135     leave_priv_setting();
136     n_printf("IPX: could not set socket option for broadcast.\n");
137     return -1;
138   }
139 #ifdef DOSEMU
140   /* allow setting the type field in the IPX header */
141   opt = 1;
142 #if 0 /* this seems to be wrong: IPX_TYPE can only be set on level SOL_IPX */
143   if (setsockopt(sock, SOL_SOCKET, IPX_TYPE, &opt, sizeof(opt)) == -1) {
144 #else
145   /* the socket _is_ an IPX socket, hence it first passes ipx_setsockopt()
146    * in file linux/net/ipx/af_ipx.c. This one handles SOL_IPX itself and
147    * passes SOL_SOCKET-levels down to sock_setsockopt().
148    * Hence I guess the below is correct (can somebody please verify this?)
149    * -- Hans, June 14 1997
150    */
151   if (setsockopt(sock, SOL_IPX, IPX_TYPE, &opt, sizeof(opt)) == -1) {
152 #endif
153     leave_priv_setting();
154     n_printf("IPX: could not set socket option for type.\n");
155     return -1;
156   }
157 #endif  
158   ipxs.sipx_family = AF_IPX;
159   ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
160 /*  ipxs.sipx_network = htonl(MyNetwork); */
161   bzero(ipxs.sipx_node, 6);     /* Please fill in my node name */
162   ipxs.sipx_port = htons(port);
163
164   /* now bind to this port */
165   if (bind(sock, (struct sockaddr *) &ipxs, sizeof(ipxs)) == -1) {
166     n_printf("IPX: could not bind socket to address\n");
167     close( sock );
168     leave_priv_setting();
169     return -1;
170   }
171   
172   if( port==0 ) {
173     len = sizeof(ipxs2);
174     if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
175       n_printf("IPX: could not get socket name in IPXOpenSocket\n");
176       close( sock );
177       leave_priv_setting();
178       return -1;
179     } else {
180       port = htons(ipxs2.sipx_port);
181       n_printf("IPX: opened dynamic socket %04x\n", port);
182     }
183   }
184   leave_priv_setting();
185   sk->fd = sock;
186   sk->socket = port;
187   return 0;
188 }
189
190 void ipx_linux_CloseSocket(ipx_socket_t *mysock) {
191   /* now close the file descriptor for the socket, and free it */
192   n_printf("IPX: closing file descriptor on socket %x\n", mysock->socket);
193   close(mysock->fd);
194 }
195
196 int ipx_linux_SendPacket(ipx_socket_t *mysock, IPXPacket_t *IPXHeader,
197  u_char *data, int dataLen) {
198   struct sockaddr_ipx ipxs;
199  
200   ipxs.sipx_family = AF_IPX;
201   /* get destination address from IPX packet header */
202   memcpy(&ipxs.sipx_network, IPXHeader->Destination.Network, 4);
203   /* if destination address is 0, then send to my net */
204   if (ipxs.sipx_network == 0) {
205     ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
206 /*  ipxs.sipx_network = htonl(MyNetwork); */
207   }
208   memcpy(&ipxs.sipx_node, IPXHeader->Destination.Node, 6);
209   memcpy(&ipxs.sipx_port, IPXHeader->Destination.Socket, 2);
210   ipxs.sipx_type = IPXHeader->PacketType;
211   /*    ipxs.sipx_port=htons(0x452); */
212   return sendto(mysock->fd, data, dataLen, 0,
213              (struct sockaddr *) &ipxs, sizeof(ipxs));
214 }
215
216 int ipx_linux_ReceivePacket(ipx_socket_t *s, char *buffer, int bufsize, 
217  struct ipx_recv_data *rd) {
218         int sz, size;
219         struct sockaddr_ipx ipxs;
220  
221         sz = sizeof(ipxs);
222         if ((size = recvfrom(s->fd, buffer, bufsize, 0,
223              (struct sockaddr *) &ipxs, &sz)) <= 0)
224              return size;
225         memcpy(rd->src_network, &ipxs.sipx_network, 4);
226         memcpy(rd->src_node, ipxs.sipx_node, 6);
227         rd->src_socket = ipxs.sipx_port;
228         rd->dst_socket = s->socket;
229         rd->pkt_type = ipxs.sipx_type;
230   
231         return size;
232 }