unused fine
[btb/d2x.git] / arch / linux / ipx_kali.c
1 /* $Id: ipx_kali.c,v 1.4 2002-08-29 08:59:43 btb Exp $ */
2
3 /*
4  *
5  * IPX driver for KaliNix interface
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <conf.h>
11 #endif
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <netinet/in.h> /* for htons & co. */
16 #include "ipx_drv.h"
17 #include "ukali.h"
18
19 extern unsigned char ipx_MyAddress[10];
20
21 static int open_sockets = 0;
22 static int dynamic_socket = 0x401;
23 static int last_socket = 0;
24
25 int have_empty_address() {
26         int i;
27         for (i = 0; i < 10 && !ipx_MyAddress[i]; i++) ;
28         return i == 10;
29 }
30
31 int ipx_kali_GetMyAddress(void)
32 {
33
34         kaliaddr_ipx mKaliAddr;
35
36         if (!have_empty_address())
37                 return 0;
38
39         if (KaliGetNodeNum(&mKaliAddr) < 0)
40                 return -1;
41
42         memset(ipx_MyAddress, 0, 4);
43         memcpy(ipx_MyAddress + 4, mKaliAddr.sa_nodenum, sizeof(mKaliAddr.sa_nodenum));
44
45         return 0;
46 }
47
48 int ipx_kali_OpenSocket(ipx_socket_t *sk, int port)
49 {
50         printf("IPX_kali: OpenSocket on port(%d)\n", port);
51
52         if (!open_sockets) {
53                 if (have_empty_address()) {
54                         if (ipx_kali_GetMyAddress() < 0) {
55                                 printf("IPX_kali: Error communicating with KaliNix\n");
56                                 return -1;
57                         }
58                 }
59         }
60         if (!port)
61                 port = dynamic_socket++;
62
63         if ((sk->fd = KaliOpenSocket(htons(port))) < 0) {
64                 printf("IPX_kali: OpenSocket Failed on port(%d)\n", port);
65                 sk->fd = -1;
66                 return -1;
67         }
68         open_sockets++;
69         last_socket = port;
70         sk->socket = port;
71         return 0;
72 }
73
74 void ipx_kali_CloseSocket(ipx_socket_t *mysock)
75 {
76         if (!open_sockets) {
77                 printf("IPX_kali: close w/o open\n");
78                 return;
79         }
80         printf("IPX_kali: CloseSocket on port(%d)\n", mysock->socket);
81         KaliCloseSocket(mysock->fd);
82         if (--open_sockets) {
83                 printf("IPX_kali: (closesocket) %d sockets left\n", open_sockets);
84                 return;
85         }
86 }
87
88 int ipx_kali_SendPacket(ipx_socket_t *mysock, IPXPacket_t *IPXHeader,
89  u_char *data, int dataLen)
90 {
91         kaliaddr_ipx toaddr;
92         int i;
93
94         memcpy(toaddr.sa_nodenum, IPXHeader->Destination.Node, sizeof(toaddr.sa_nodenum));
95         memcpy(&toaddr.sa_socket, IPXHeader->Destination.Socket, sizeof(toaddr.sa_socket));
96
97         if ((i = KaliSendPacket(mysock->fd, (char *)data, dataLen, &toaddr)) < 0)
98                 return -1;
99
100         return i;
101 }
102
103 int ipx_kali_ReceivePacket(ipx_socket_t *s, char *outbuf, int outbufsize,
104  struct ipx_recv_data *rd)
105 {
106         int size;
107         kaliaddr_ipx fromaddr;
108
109         if ((size = KaliReceivePacket(s->fd, outbuf, outbufsize, &fromaddr)) < 0)
110                 return -1;
111
112         rd->dst_socket = s->socket;
113         rd->src_socket = ntohs(fromaddr.sa_socket);
114         memcpy(rd->src_node, fromaddr.sa_nodenum, sizeof(fromaddr.sa_nodenum));
115         memset(rd->src_network, 0, 4);
116         rd->pkt_type = 0;
117
118         return size;
119 }
120
121 struct ipx_driver ipx_kali = {
122         ipx_kali_GetMyAddress,
123         ipx_kali_OpenSocket,
124         ipx_kali_CloseSocket,
125         ipx_kali_SendPacket,
126         ipx_kali_ReceivePacket,
127         ipx_general_PacketReady
128 };