1 /* $Id: ipx_lin.c,v 1.5 2003-03-13 00:20:21 btb Exp $ */
13 #include <sys/types.h>
14 #include <sys/socket.h>
17 #ifdef HAVE_NETIPX_IPX_H
18 #include <netipx/ipx.h>
20 # include <linux/ipx.h>
26 #include <netinet/in.h>
35 extern unsigned char ipx_MyAddress[10];
38 ipx_linux_GetMyAddress( void )
41 struct sockaddr_ipx ipxs;
42 struct sockaddr_ipx ipxs2;
46 sock=socket(AF_IPX,SOCK_DGRAM,PF_IPX);
49 n_printf("IPX: could not open socket in GetMyAddress\n");
53 /* bind this socket to network 0 */
54 ipxs.sipx_family=AF_IPX;
55 #ifdef IPX_MANUAL_ADDRESS
56 memcpy(&ipxs.sipx_network, ipx_MyAddress, 4);
62 if(bind(sock,(struct sockaddr *)&ipxs,sizeof(ipxs))==-1)
64 n_printf("IPX: could bind to network 0 in GetMyAddress\n");
70 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
71 n_printf("IPX: could not get socket name in GetMyAddress\n");
76 memcpy(ipx_MyAddress, &ipxs2.sipx_network, 4);
77 for (i = 0; i < 6; i++) {
78 ipx_MyAddress[4+i] = ipxs2.sipx_node[i];
85 ipx_linux_OpenSocket(ipx_socket_t *sk, int port)
87 int sock; /* sock here means Linux socket handle */
89 struct sockaddr_ipx ipxs;
91 struct sockaddr_ipx ipxs2;
93 /* DANG_FIXTHIS - kludge to support broken linux IPX stack */
94 /* need to convert dynamic socket open into a real socket number */
96 n_printf("IPX: using socket %x\n", nextDynamicSocket);
97 port = nextDynamicSocket++;
100 /* do a socket call, then bind to this port */
101 sock = socket(AF_IPX, SOCK_DGRAM, PF_IPX);
103 n_printf("IPX: could not open IPX socket.\n");
109 /* turn on socket debugging */
112 if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, &opt, sizeof(opt)) == -1) {
113 leave_priv_setting();
114 n_printf("IPX: could not set socket option for debugging.\n");
117 leave_priv_setting();
121 /* Permit broadcast output */
123 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
124 &opt, sizeof(opt)) == -1) {
125 leave_priv_setting();
126 n_printf("IPX: could not set socket option for broadcast.\n");
130 /* allow setting the type field in the IPX header */
132 #if 0 /* this seems to be wrong: IPX_TYPE can only be set on level SOL_IPX */
133 if (setsockopt(sock, SOL_SOCKET, IPX_TYPE, &opt, sizeof(opt)) == -1) {
135 /* the socket _is_ an IPX socket, hence it first passes ipx_setsockopt()
136 * in file linux/net/ipx/af_ipx.c. This one handles SOL_IPX itself and
137 * passes SOL_SOCKET-levels down to sock_setsockopt().
138 * Hence I guess the below is correct (can somebody please verify this?)
139 * -- Hans, June 14 1997
141 if (setsockopt(sock, SOL_IPX, IPX_TYPE, &opt, sizeof(opt)) == -1) {
143 leave_priv_setting();
144 n_printf("IPX: could not set socket option for type.\n");
148 ipxs.sipx_family = AF_IPX;
149 ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
150 /* ipxs.sipx_network = htonl(MyNetwork); */
151 bzero(ipxs.sipx_node, 6); /* Please fill in my node name */
152 ipxs.sipx_port = htons(port);
154 /* now bind to this port */
155 if (bind(sock, (struct sockaddr *) &ipxs, sizeof(ipxs)) == -1) {
156 n_printf("IPX: could not bind socket to address\n");
158 leave_priv_setting();
164 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
165 n_printf("IPX: could not get socket name in IPXOpenSocket\n");
167 leave_priv_setting();
170 port = htons(ipxs2.sipx_port);
171 n_printf("IPX: opened dynamic socket %04x\n", port);
174 leave_priv_setting();
180 void ipx_linux_CloseSocket(ipx_socket_t *mysock) {
181 /* now close the file descriptor for the socket, and free it */
182 n_printf("IPX: closing file descriptor on socket %x\n", mysock->socket);
186 int ipx_linux_SendPacket(ipx_socket_t *mysock, IPXPacket_t *IPXHeader,
187 u_char *data, int dataLen) {
188 struct sockaddr_ipx ipxs;
190 ipxs.sipx_family = AF_IPX;
191 /* get destination address from IPX packet header */
192 memcpy(&ipxs.sipx_network, IPXHeader->Destination.Network, 4);
193 /* if destination address is 0, then send to my net */
194 if (ipxs.sipx_network == 0) {
195 ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
196 /* ipxs.sipx_network = htonl(MyNetwork); */
198 memcpy(&ipxs.sipx_node, IPXHeader->Destination.Node, 6);
199 memcpy(&ipxs.sipx_port, IPXHeader->Destination.Socket, 2);
200 ipxs.sipx_type = IPXHeader->PacketType;
201 /* ipxs.sipx_port=htons(0x452); */
202 return sendto(mysock->fd, data, dataLen, 0,
203 (struct sockaddr *) &ipxs, sizeof(ipxs));
206 int ipx_linux_ReceivePacket(ipx_socket_t *s, char *buffer, int bufsize,
207 struct ipx_recv_data *rd) {
209 struct sockaddr_ipx ipxs;
212 if ((size = recvfrom(s->fd, buffer, bufsize, 0,
213 (struct sockaddr *) &ipxs, &sz)) <= 0)
215 memcpy(rd->src_network, &ipxs.sipx_network, 4);
216 memcpy(rd->src_node, ipxs.sipx_node, 6);
217 rd->src_socket = ipxs.sipx_port;
218 rd->dst_socket = s->socket;
219 rd->pkt_type = ipxs.sipx_type;