2 * $Source: /cvs/cvsroot/d2x/arch/linux/ipx_lin.c,v $
5 * $Date: 2001-10-19 07:39:26 $
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/
13 * Revision 1.2 2001/01/29 13:35:08 bradleyb
14 * Fixed build system, minor fixes
23 #include <sys/types.h>
24 #include <sys/socket.h>
27 #ifdef HAVE_NETIPX_IPX_H
28 #include <netipx/ipx.h>
30 # include <linux/ipx.h>
36 #include <netinet/in.h>
45 extern unsigned char ipx_MyAddress[10];
48 ipx_linux_GetMyAddress( void )
51 struct sockaddr_ipx ipxs;
52 struct sockaddr_ipx ipxs2;
56 sock=socket(AF_IPX,SOCK_DGRAM,PF_IPX);
59 n_printf("IPX: could not open socket in GetMyAddress\n");
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);
72 if(bind(sock,(struct sockaddr *)&ipxs,sizeof(ipxs))==-1)
74 n_printf("IPX: could bind to network 0 in GetMyAddress\n");
80 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
81 n_printf("IPX: could not get socket name in GetMyAddress\n");
86 memcpy(ipx_MyAddress, &ipxs2.sipx_network, 4);
87 for (i = 0; i < 6; i++) {
88 ipx_MyAddress[4+i] = ipxs2.sipx_node[i];
95 ipx_linux_OpenSocket(ipx_socket_t *sk, 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();
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);
196 int ipx_linux_SendPacket(ipx_socket_t *mysock, IPXPacket_t *IPXHeader,
197 u_char *data, int dataLen) {
198 struct sockaddr_ipx ipxs;
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); */
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));
216 int ipx_linux_ReceivePacket(ipx_socket_t *s, char *buffer, int bufsize,
217 struct ipx_recv_data *rd) {
219 struct sockaddr_ipx ipxs;
222 if ((size = recvfrom(s->fd, buffer, bufsize, 0,
223 (struct sockaddr *) &ipxs, &sz)) <= 0)
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;