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