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