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>
29 extern unsigned char ipx_MyAddress[10];
32 ipx_linux_GetMyAddress( void )
35 struct sockaddr_ipx ipxs;
36 struct sockaddr_ipx ipxs2;
40 sock=socket(AF_IPX,SOCK_DGRAM,PF_IPX);
43 n_printf("IPX: could not open socket in GetMyAddress\n");
47 /* bind this socket to network 0 */
48 ipxs.sipx_family=AF_IPX;
49 #ifdef IPX_MANUAL_ADDRESS
50 memcpy(&ipxs.sipx_network, ipx_MyAddress, 4);
56 if(bind(sock,(struct sockaddr *)&ipxs,sizeof(ipxs))==-1)
58 n_printf("IPX: could bind to network 0 in GetMyAddress\n");
64 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
65 n_printf("IPX: could not get socket name in GetMyAddress\n");
70 memcpy(ipx_MyAddress, &ipxs2.sipx_network, 4);
71 for (i = 0; i < 6; i++) {
72 ipx_MyAddress[4+i] = ipxs2.sipx_node[i];
79 ipx_linux_OpenSocket(ipx_socket_t *sk, int port)
81 int sock; /* sock here means Linux socket handle */
83 struct sockaddr_ipx ipxs;
85 struct sockaddr_ipx ipxs2;
87 /* DANG_FIXTHIS - kludge to support broken linux IPX stack */
88 /* need to convert dynamic socket open into a real socket number */
90 n_printf("IPX: using socket %x\n", nextDynamicSocket);
91 port = nextDynamicSocket++;
94 /* do a socket call, then bind to this port */
95 sock = socket(AF_IPX, SOCK_DGRAM, PF_IPX);
97 n_printf("IPX: could not open IPX socket.\n");
103 /* turn on socket debugging */
106 if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, &opt, sizeof(opt)) == -1) {
107 leave_priv_setting();
108 n_printf("IPX: could not set socket option for debugging.\n");
111 leave_priv_setting();
115 /* Permit broadcast output */
117 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
118 &opt, sizeof(opt)) == -1) {
119 leave_priv_setting();
120 n_printf("IPX: could not set socket option for broadcast.\n");
124 /* allow setting the type field in the IPX header */
126 #if 0 /* this seems to be wrong: IPX_TYPE can only be set on level SOL_IPX */
127 if (setsockopt(sock, SOL_SOCKET, IPX_TYPE, &opt, sizeof(opt)) == -1) {
129 /* the socket _is_ an IPX socket, hence it first passes ipx_setsockopt()
130 * in file linux/net/ipx/af_ipx.c. This one handles SOL_IPX itself and
131 * passes SOL_SOCKET-levels down to sock_setsockopt().
132 * Hence I guess the below is correct (can somebody please verify this?)
133 * -- Hans, June 14 1997
135 if (setsockopt(sock, SOL_IPX, IPX_TYPE, &opt, sizeof(opt)) == -1) {
137 leave_priv_setting();
138 n_printf("IPX: could not set socket option for type.\n");
142 ipxs.sipx_family = AF_IPX;
143 ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
144 /* ipxs.sipx_network = htonl(MyNetwork); */
145 bzero(ipxs.sipx_node, 6); /* Please fill in my node name */
146 ipxs.sipx_port = htons(port);
148 /* now bind to this port */
149 if (bind(sock, (struct sockaddr *) &ipxs, sizeof(ipxs)) == -1) {
150 n_printf("IPX: could not bind socket to address\n");
152 leave_priv_setting();
158 if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0) {
159 n_printf("IPX: could not get socket name in IPXOpenSocket\n");
161 leave_priv_setting();
164 port = htons(ipxs2.sipx_port);
165 n_printf("IPX: opened dynamic socket %04x\n", port);
168 leave_priv_setting();
174 void ipx_linux_CloseSocket(ipx_socket_t *mysock) {
175 /* now close the file descriptor for the socket, and free it */
176 n_printf("IPX: closing file descriptor on socket %x\n", mysock->socket);
180 int ipx_linux_SendPacket(ipx_socket_t *mysock, IPXPacket_t *IPXHeader,
181 u_char *data, int dataLen) {
182 struct sockaddr_ipx ipxs;
184 ipxs.sipx_family = AF_IPX;
185 /* get destination address from IPX packet header */
186 memcpy(&ipxs.sipx_network, IPXHeader->Destination.Network, 4);
187 /* if destination address is 0, then send to my net */
188 if (ipxs.sipx_network == 0) {
189 ipxs.sipx_network = *((unsigned int *)&ipx_MyAddress[0]);
190 /* ipxs.sipx_network = htonl(MyNetwork); */
192 memcpy(&ipxs.sipx_node, IPXHeader->Destination.Node, 6);
193 memcpy(&ipxs.sipx_port, IPXHeader->Destination.Socket, 2);
194 ipxs.sipx_type = IPXHeader->PacketType;
195 /* ipxs.sipx_port=htons(0x452); */
196 return sendto(mysock->fd, data, dataLen, 0,
197 (struct sockaddr *) &ipxs, sizeof(ipxs));
200 int ipx_linux_ReceivePacket(ipx_socket_t *s, char *buffer, int bufsize,
201 struct ipx_recv_data *rd) {
203 struct sockaddr_ipx ipxs;
206 if ((size = recvfrom(s->fd, buffer, bufsize, 0,
207 (struct sockaddr *) &ipxs, &sz)) <= 0)
209 memcpy(rd->src_network, &ipxs.sipx_network, 4);
210 memcpy(rd->src_node, ipxs.sipx_node, 6);
211 rd->src_socket = ipxs.sipx_port;
212 rd->dst_socket = s->socket;
213 rd->pkt_type = ipxs.sipx_type;
218 #endif // __ENV_LINUX__