2 * $Source: /cvs/cvsroot/d2x/arch/linux/ipx_bsd.c,v $
5 * $Date: 2001-10-19 07:29:36 $
7 * IPX driver using BSD style sockets
8 * Mostly taken from dosemu
10 * $Log: not supported by cvs2svn $
11 * Revision 1.2 2001/01/29 13:35:08 bradleyb
12 * Fixed build system, minor fixes
20 #include <sys/types.h>
21 #include <sys/socket.h>
24 #ifdef HAVE_NETIPX_IPX_H
25 #include <netipx/ipx.h>
27 # include <linux/ipx.h>
33 #include <netinet/in.h>
42 #define n_printf(format, args...) mprintf((1, format, ## args))
43 #define enter_priv_on()
44 #define leave_priv_setting()
47 static ipx_socket_t mysock;
49 static int ipx_bsd_GetMyAddress( void )
52 struct sockaddr_ipx ipxs;
53 struct sockaddr_ipx ipxs2;
57 sock=socket(AF_IPX,SOCK_DGRAM,PF_IPX);
60 n_printf("IPX: could not open socket in GetMyAddress\n");
64 /* bind this socket to network 0 */
65 ipxs.sipx_family=AF_IPX;
66 #ifdef IPX_MANUAL_ADDRESS
67 memcpy(&ipxs.sipx_network, ipx_MyAddress, 4);
73 if(bind(sock,(struct sockaddr *)&ipxs,sizeof(ipxs))==-1)
75 n_printf("IPX: could bind to network 0 in GetMyAddress\n");
81 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
82 n_printf("IPX: could not get socket name in GetMyAddress\n");
87 memcpy(ipx_MyAddress, &ipxs2.sipx_network, 4);
88 for (i = 0; i < 6; i++) {
89 ipx_MyAddress[4+i] = ipxs2.sipx_node[i];
95 static int ipx_bsd_OpenSocket(int port)
97 int sock; /* sock here means Linux socket handle */
99 struct sockaddr_ipx ipxs;
101 struct sockaddr_ipx ipxs2;
103 /* DANG_FIXTHIS - kludge to support broken linux IPX stack */
104 /* need to convert dynamic socket open into a real socket number */
106 n_printf("IPX: using socket %x\n", nextDynamicSocket);
107 port = nextDynamicSocket++;
110 /* do a socket call, then bind to this port */
111 sock = socket(AF_IPX, SOCK_DGRAM, PF_IPX);
113 n_printf("IPX: could not open IPX socket.\n");
119 /* turn on socket debugging */
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");
127 leave_priv_setting();
131 /* Permit broadcast output */
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");
140 /* allow setting the type field in the IPX header */
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) {
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
151 if (setsockopt(sock, SOL_IPX, IPX_TYPE, &opt, sizeof(opt)) == -1) {
153 leave_priv_setting();
154 n_printf("IPX: could not set socket option for type.\n");
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);
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");
168 leave_priv_setting();
174 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
175 n_printf("IPX: could not get socket name in IPXOpenSocket\n");
177 leave_priv_setting();
180 port = htons(ipxs2.sipx_port);
181 n_printf("IPX: opened dynamic socket %04x\n", port);
184 leave_priv_setting();
186 mysock.socket = port;
188 ipx_bsd_GetMyAddress();
193 static void ipx_bsd_CloseSocket(void) {
194 /* now close the file descriptor for the socket, and free it */
195 n_printf("IPX: closing file descriptor on socket %x\n", mysock.socket);
199 static int ipx_bsd_SendPacket(IPXPacket_t *IPXHeader,
200 u_char *data, int dataLen) {
201 struct sockaddr_ipx ipxs;
203 ipxs.sipx_family = AF_IPX;
204 /* get destination address from IPX packet header */
205 memcpy(&ipxs.sipx_network, IPXHeader->Destination.Network, 4);
206 /* if destination address is 0, then send to my net */
207 if (ipxs.sipx_network == 0) {
208 ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
209 /* ipxs.sipx_network = htonl(MyNetwork); */
211 memcpy(&ipxs.sipx_node, IPXHeader->Destination.Node, 6);
212 //memcpy(&ipxs.sipx_port, IPXHeader->Destination.Socket, 2);
213 ipxs.sipx_port=htons(mysock.socket);
214 ipxs.sipx_type = IPXHeader->PacketType;
215 /* ipxs.sipx_port=htons(0x452); */
216 return sendto(mysock.fd, data, dataLen, 0,
217 (struct sockaddr *) &ipxs, sizeof(ipxs));
220 static int ipx_bsd_ReceivePacket(char *buffer, int bufsize,
221 struct ipx_recv_data *rd) {
223 struct sockaddr_ipx ipxs;
226 if ((size = recvfrom(mysock.fd, buffer, bufsize, 0,
227 (struct sockaddr *) &ipxs, &sz)) <= 0)
229 memcpy(rd->src_network, &ipxs.sipx_network, 4);
230 memcpy(rd->src_node, ipxs.sipx_node, 6);
231 rd->src_socket = ipxs.sipx_port;
232 rd->dst_socket = mysock.socket;
233 rd->pkt_type = ipxs.sipx_type;
238 static int ipx_bsd_general_PacketReady(void) {
239 return ipx_general_PacketReady(mysock.fd);
242 struct ipx_driver ipx_bsd = {
243 // ipx_bsd_GetMyAddress,
247 ipx_bsd_ReceivePacket,
248 ipx_bsd_general_PacketReady,