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