1 /* IPX driver using BSD style sockets */
2 /* Mostly taken from dosemu */
8 #include <sys/socket.h>
11 #ifdef HAVE_NETIPX_IPX_H
12 #include <netipx/ipx.h>
14 # include <linux/ipx.h>
20 #include <netinet/in.h>
30 #define n_printf(format, args...) mprintf((1, format, ## args))
31 #define enter_priv_on()
32 #define leave_priv_setting()
35 static int ipx_bsd_GetMyAddress( void )
38 struct sockaddr_ipx ipxs;
39 struct sockaddr_ipx ipxs2;
43 sock=socket(AF_IPX,SOCK_DGRAM,PF_IPX);
46 n_printf("IPX: could not open socket in GetMyAddress\n");
50 /* bind this socket to network 0 */
51 ipxs.sipx_family=AF_IPX;
52 #ifdef IPX_MANUAL_ADDRESS
53 memcpy(&ipxs.sipx_network, ipx_MyAddress, 4);
59 if(bind(sock,(struct sockaddr *)&ipxs,sizeof(ipxs))==-1)
61 n_printf("IPX: could bind to network 0 in GetMyAddress\n");
67 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
68 n_printf("IPX: could not get socket name in GetMyAddress\n");
73 memcpy(ipx_MyAddress, &ipxs2.sipx_network, 4);
74 for (i = 0; i < 6; i++) {
75 ipx_MyAddress[4+i] = ipxs2.sipx_node[i];
81 static int ipx_bsd_OpenSocket(ipx_socket_t *sk, int port)
83 int sock; /* sock here means Linux socket handle */
85 struct sockaddr_ipx ipxs;
87 struct sockaddr_ipx ipxs2;
89 /* DANG_FIXTHIS - kludge to support broken linux IPX stack */
90 /* need to convert dynamic socket open into a real socket number */
92 n_printf("IPX: using socket %x\n", nextDynamicSocket);
93 port = nextDynamicSocket++;
96 /* do a socket call, then bind to this port */
97 sock = socket(AF_IPX, SOCK_DGRAM, PF_IPX);
99 n_printf("IPX: could not open IPX socket.\n");
105 /* turn on socket debugging */
108 if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, &opt, sizeof(opt)) == -1) {
109 leave_priv_setting();
110 n_printf("IPX: could not set socket option for debugging.\n");
113 leave_priv_setting();
117 /* Permit broadcast output */
119 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
120 &opt, sizeof(opt)) == -1) {
121 leave_priv_setting();
122 n_printf("IPX: could not set socket option for broadcast.\n");
126 /* allow setting the type field in the IPX header */
128 #if 0 /* this seems to be wrong: IPX_TYPE can only be set on level SOL_IPX */
129 if (setsockopt(sock, SOL_SOCKET, IPX_TYPE, &opt, sizeof(opt)) == -1) {
131 /* the socket _is_ an IPX socket, hence it first passes ipx_setsockopt()
132 * in file linux/net/ipx/af_ipx.c. This one handles SOL_IPX itself and
133 * passes SOL_SOCKET-levels down to sock_setsockopt().
134 * Hence I guess the below is correct (can somebody please verify this?)
135 * -- Hans, June 14 1997
137 if (setsockopt(sock, SOL_IPX, IPX_TYPE, &opt, sizeof(opt)) == -1) {
139 leave_priv_setting();
140 n_printf("IPX: could not set socket option for type.\n");
144 ipxs.sipx_family = AF_IPX;
145 ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
146 /* ipxs.sipx_network = htonl(MyNetwork); */
147 bzero(ipxs.sipx_node, 6); /* Please fill in my node name */
148 ipxs.sipx_port = htons(port);
150 /* now bind to this port */
151 if (bind(sock, (struct sockaddr *) &ipxs, sizeof(ipxs)) == -1) {
152 n_printf("IPX: could not bind socket to address\n");
154 leave_priv_setting();
160 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
161 n_printf("IPX: could not get socket name in IPXOpenSocket\n");
163 leave_priv_setting();
166 port = htons(ipxs2.sipx_port);
167 n_printf("IPX: opened dynamic socket %04x\n", port);
170 leave_priv_setting();
176 static void ipx_bsd_CloseSocket(ipx_socket_t *mysock) {
177 /* now close the file descriptor for the socket, and free it */
178 n_printf("IPX: closing file descriptor on socket %x\n", mysock->socket);
182 static int ipx_bsd_SendPacket(ipx_socket_t *mysock, IPXPacket_t *IPXHeader,
183 u_char *data, int dataLen) {
184 struct sockaddr_ipx ipxs;
186 ipxs.sipx_family = AF_IPX;
187 /* get destination address from IPX packet header */
188 memcpy(&ipxs.sipx_network, IPXHeader->Destination.Network, 4);
189 /* if destination address is 0, then send to my net */
190 if (ipxs.sipx_network == 0) {
191 ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
192 /* ipxs.sipx_network = htonl(MyNetwork); */
194 memcpy(&ipxs.sipx_node, IPXHeader->Destination.Node, 6);
195 memcpy(&ipxs.sipx_port, IPXHeader->Destination.Socket, 2);
196 ipxs.sipx_type = IPXHeader->PacketType;
197 /* ipxs.sipx_port=htons(0x452); */
198 return sendto(mysock->fd, data, dataLen, 0,
199 (struct sockaddr *) &ipxs, sizeof(ipxs));
202 static int ipx_bsd_ReceivePacket(ipx_socket_t *s, char *buffer, int bufsize,
203 struct ipx_recv_data *rd) {
205 struct sockaddr_ipx ipxs;
208 if ((size = recvfrom(s->fd, buffer, bufsize, 0,
209 (struct sockaddr *) &ipxs, &sz)) <= 0)
211 memcpy(rd->src_network, &ipxs.sipx_network, 4);
212 memcpy(rd->src_node, ipxs.sipx_node, 6);
213 rd->src_socket = ipxs.sipx_port;
214 rd->dst_socket = s->socket;
215 rd->pkt_type = ipxs.sipx_type;
220 struct ipx_driver ipx_bsd = {
221 ipx_bsd_GetMyAddress,
225 ipx_bsd_ReceivePacket,
226 ipx_general_PacketReady