enabled kali
[btb/d2x.git] / arch / linux / ukali.c
1 /* $Id: ukali.c,v 1.4 2003-01-01 00:56:24 btb Exp $ */
2
3 /*
4  *
5  * Kali support functions
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <conf.h>
11 #endif
12
13 #include <sys/socket.h>
14 #include <sys/types.h>
15 #include <string.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <netdb.h>
19 #include <stdlib.h>
20 #include <sys/time.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include "ukali.h"
25 //added 05/17/99 Matt Mueller - needed to redefine FD_* so that no asm is used
26 #include "checker.h"
27 //end addition -MM
28 int g_sockfd = -1;
29 struct sockaddr_in kalinix_addr;
30 char g_mynodenum[6];
31
32 int knix_newSock(void) {
33
34         int tempsock;
35         struct sockaddr_in taddr;
36
37         taddr.sin_family = AF_INET;
38         taddr.sin_addr.s_addr = inet_addr("127.0.0.1");
39         taddr.sin_port = 0;
40
41         if ((tempsock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
42                 return -1;
43
44         fcntl(tempsock, F_SETFL, fcntl(tempsock, F_GETFL) | O_NONBLOCK);
45
46         if ((bind(tempsock, (struct sockaddr *)&taddr, sizeof(taddr))) < 0) {
47                 close(tempsock);
48                 return -1;
49         }
50         return tempsock;
51 }
52
53 int knix_Send(int hand, char *data, int len) {
54         int i = 0, t;
55
56         while ((t = sendto(hand, data, len, 0, (struct sockaddr *)&kalinix_addr, 
57                         sizeof(kalinix_addr))) < 0) {
58                 i++;
59                 if (i > 10)
60                         return 0;
61         }
62
63         return t;
64 }
65
66 int knix_Recv(int hand, char *data, int len) {
67         struct sockaddr_in taddr;
68         int tlen;
69
70         tlen = sizeof(taddr);
71
72         return recvfrom(hand, data, len, 0, (struct sockaddr *)&taddr, &tlen);
73
74 }
75
76 int knix_WaitforSocket(int hand, int timems) {
77         fd_set set;
78         struct timeval tv;
79
80         FD_ZERO(&set);
81         FD_SET(hand, &set);
82         tv.tv_sec = 0;
83         // 100ms
84         tv.tv_usec = timems * 1000;
85         return select(hand + 1, &set, NULL, NULL, &tv);
86 }
87
88 int knix_ReceivePacket(int hand, char *outdata, int *outlen, kaliaddr_ipx *from) {
89         static char data[MAX_PACKET_SIZE];
90         int len;
91
92         len = knix_Recv(hand, data, sizeof(data));
93
94         if (len <= 0) return 0;
95
96         switch (data[0]) {
97 //              case 1: // open socket
98 //                      break;
99 //              case 2: // close socket
100 //                      break;
101                 case 3: // received data packet
102                         if (len < 11) break;
103                         if (!outdata)
104                                 break;
105                         from->sa_family = AF_IPX;
106                         memcpy(from->sa_nodenum, &data[1], sizeof(from->sa_nodenum));
107                         memset(from->sa_netnum, 0, sizeof(from->sa_netnum));
108                         memcpy(&from->sa_socket, &data[9], sizeof(unsigned short));
109                         memcpy(outdata, &data[11], len-11 > *outlen ? *outlen : len-11);
110                         *outlen = len-11;
111                         break;
112                 case 4: // myipxaddress
113                         if (len < 7) break;
114                         memcpy(g_mynodenum, &data[1], 6);
115                         break;
116 //              case 5: // Init KaliNix connection
117 //                      break;
118                 case 6: // open response
119                 case 7: // close response
120                         if (len < 3) break;
121 //                      memcpy(g_LastPort, &data[1], sizeof(g_LastPort))
122                         break;
123         }
124
125         return data[0];
126
127 }
128
129 int knix_GetMyAddress(void) {
130         char initdata[1];
131
132         if (g_sockfd < 0) {
133                 kalinix_addr.sin_family = AF_INET;
134                 kalinix_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
135                 kalinix_addr.sin_port = htons(4213);
136
137                 g_sockfd = knix_newSock();
138         }
139
140         initdata[0] = 5; // Get Address
141         knix_Send(g_sockfd, initdata, sizeof(initdata));
142
143         return 0;
144
145 }
146
147 int KaliSendPacket(int hand, char *data, int len, kaliaddr_ipx *to) {
148         static char sendbuf[MAX_PACKET_SIZE+11];
149 //              char    code; == 3
150 //              char    sa_nodenum[6];
151 //              char    dport[2];
152 //              char    sport[2];
153 //              char data[];
154
155         sendbuf[0] = 3;
156         memcpy(&sendbuf[1], to->sa_nodenum, sizeof(to->sa_nodenum));
157         memcpy(&sendbuf[7], &to->sa_socket, sizeof(to->sa_socket));
158         memset(&sendbuf[9], 0, sizeof(to->sa_socket));
159         len = len > MAX_PACKET_SIZE ? MAX_PACKET_SIZE : len;
160         memcpy(&sendbuf[11], data, len);
161
162         if (!knix_Send(hand, sendbuf, len+11)){
163                 return -1;
164         }
165         return len;
166
167 }
168
169 int KaliReceivePacket(int hand, char *data, int len, kaliaddr_ipx *from) {
170         int newlen;
171         int t;
172
173         newlen = len;
174
175         t = knix_ReceivePacket(hand, data, &newlen, from);
176
177         while (t != 0 && t != 3)
178                 t = knix_ReceivePacket(hand, data, &newlen, from);
179
180         if (t == 3)
181                 return newlen;
182         else return -1;
183
184 }
185
186 int KaliGetNodeNum(kaliaddr_ipx *myaddr) {
187         int tcount = 0;
188
189         if (g_sockfd < 0 && knix_GetMyAddress())
190                 return -1;
191
192         while (tcount < 5) {
193                 if (knix_WaitforSocket(g_sockfd, 100) > 0) {
194                         if (knix_ReceivePacket(g_sockfd, NULL, 0, NULL) == 4)
195                                 break;
196                         continue;
197                 }
198                 knix_GetMyAddress();
199                 tcount++;
200         }
201         if (tcount == 5)
202                 return -1;
203
204         close(g_sockfd);
205         g_sockfd = -1;
206         memcpy(myaddr->sa_nodenum, g_mynodenum, sizeof(g_mynodenum));
207
208         return 0;
209
210 }
211
212 int KaliCloseSocket(int hand) {
213         char opendata[3] = {2, 0, 0};
214         int tcount = 0;
215
216         knix_Send(hand, opendata, sizeof(opendata));
217
218         while (tcount < 5) {
219
220                 if (knix_WaitforSocket(hand, 100) > 0) {
221                         if (knix_ReceivePacket(hand, NULL, 0, NULL) == 7)
222                                 break;
223                         continue;
224                 }
225                 knix_Send(hand, opendata, sizeof(opendata));
226                 tcount++;
227         }
228         close(hand);
229         return 0;
230
231 }
232
233 int KaliOpenSocket(unsigned short port) {
234         char opendata[16];
235         int hand;
236         int tcount = 0;
237         long pid;
238
239         opendata[0] = 1; // open socket
240         memcpy(&opendata[1], &port, sizeof(port));
241         pid = (int)htonl(getpid());
242         memcpy(&opendata[3], &pid, sizeof(pid));
243         strncpy(&opendata[7], KALI_PROCESS_NAME, sizeof(KALI_PROCESS_NAME));
244         opendata[15] = 0;
245
246         if ((hand = knix_newSock()) < 0)
247                 return -1;
248
249         knix_Send(hand, opendata, sizeof(opendata));
250
251         while (tcount < 5) {
252
253                 if (knix_WaitforSocket(hand, 100) > 0) {
254                         if (knix_ReceivePacket(hand, NULL, 0, NULL) == 6)
255                                 break;
256                         continue;
257                 }
258                 knix_Send(hand, opendata, sizeof(opendata));
259                 tcount++;
260         }
261
262         if (tcount == 5) {
263                 close(hand);
264                 return -1;
265         }
266
267         return hand;
268 }
269