2008-01-02 15:36:51 +00:00
|
|
|
/* IPX driver for KaliNix interface */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <netinet/in.h> /* for htons & co. */
|
2009-11-24 09:48:53 +00:00
|
|
|
#include "net_ipx.h"
|
2008-01-02 15:36:51 +00:00
|
|
|
#include "ukali.h"
|
2008-04-06 20:23:28 +00:00
|
|
|
#include "console.h"
|
2008-01-02 15:36:51 +00:00
|
|
|
|
|
|
|
static int open_sockets = 0;
|
|
|
|
static int dynamic_socket = 0x401;
|
|
|
|
|
|
|
|
int have_empty_address() {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 10 && !MyAddress[i]; i++) ;
|
|
|
|
return i == 10;
|
|
|
|
}
|
|
|
|
|
2009-01-26 05:01:18 +00:00
|
|
|
int kali_get_my_address(void)
|
2008-01-02 15:36:51 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
kaliaddr_ipx mKaliAddr;
|
|
|
|
|
|
|
|
if (!have_empty_address())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (KaliGetNodeNum(&mKaliAddr) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(MyAddress, 0, 4);
|
|
|
|
memcpy(MyAddress + 4, mKaliAddr.sa_nodenum, sizeof(mKaliAddr.sa_nodenum));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-26 05:01:18 +00:00
|
|
|
int kali_open_socket(socket_t *sk, int port)
|
2008-01-02 15:36:51 +00:00
|
|
|
{
|
2008-04-06 20:23:28 +00:00
|
|
|
con_printf(CON_DEBUG,"kali: OpenSocket on port(%d)\n", port);
|
2008-01-02 15:36:51 +00:00
|
|
|
|
|
|
|
if (!open_sockets) {
|
|
|
|
if (have_empty_address()) {
|
2009-01-26 05:01:18 +00:00
|
|
|
if (kali_get_my_address() < 0) {
|
2008-04-06 20:23:28 +00:00
|
|
|
con_printf(CON_CRITICAL,"kali: Error communicating with KaliNix\n");
|
2008-01-02 15:36:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!port)
|
|
|
|
port = dynamic_socket++;
|
|
|
|
|
2008-07-19 22:28:31 +00:00
|
|
|
if ((sk->fd = KaliOpenSocket(htons(port))) < 0) {
|
2008-04-06 20:23:28 +00:00
|
|
|
con_printf(CON_CRITICAL,"kali: OpenSocket Failed on port(%d)\n", port);
|
2008-07-19 22:28:31 +00:00
|
|
|
sk->fd = -1;
|
2008-01-02 15:36:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
open_sockets++;
|
2008-07-19 22:28:31 +00:00
|
|
|
sk->socket = port;
|
2008-01-02 15:36:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-26 05:01:18 +00:00
|
|
|
void kali_close_socket(socket_t *mysock)
|
2008-01-02 15:36:51 +00:00
|
|
|
{
|
|
|
|
if (!open_sockets) {
|
2008-04-06 20:23:28 +00:00
|
|
|
con_printf(CON_CRITICAL,"kali: close w/o open\n");
|
2008-01-02 15:36:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-07-19 22:28:31 +00:00
|
|
|
con_printf(CON_DEBUG,"kali: CloseSocket on port(%d)\n", mysock->socket);
|
|
|
|
KaliCloseSocket(mysock->fd);
|
2008-01-02 15:36:51 +00:00
|
|
|
if (--open_sockets) {
|
2008-04-06 20:23:28 +00:00
|
|
|
con_printf(CON_URGENT,"kali: (closesocket) %d sockets left\n", open_sockets);
|
2008-01-02 15:36:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-26 05:01:18 +00:00
|
|
|
int kali_send_packet(socket_t *mysock, IPXPacket_t *IPXHeader, u_char *data, int dataLen)
|
2008-01-02 15:36:51 +00:00
|
|
|
{
|
|
|
|
kaliaddr_ipx toaddr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memcpy(toaddr.sa_nodenum, IPXHeader->Destination.Node, sizeof(toaddr.sa_nodenum));
|
2008-07-19 22:28:31 +00:00
|
|
|
toaddr.sa_socket=htons(mysock->socket);
|
2008-01-02 15:36:51 +00:00
|
|
|
|
2008-07-19 22:28:31 +00:00
|
|
|
if ((i = KaliSendPacket(mysock->fd, (char *)data, dataLen, &toaddr)) < 0)
|
2008-01-02 15:36:51 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2009-01-26 05:01:18 +00:00
|
|
|
int kali_receive_packet(socket_t *s, char *outbuf, int outbufsize, struct recv_data *rd)
|
2008-01-02 15:36:51 +00:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
kaliaddr_ipx fromaddr;
|
|
|
|
|
2008-07-19 22:28:31 +00:00
|
|
|
if ((size = KaliReceivePacket(s->fd, outbuf, outbufsize, &fromaddr)) < 0)
|
2008-01-02 15:36:51 +00:00
|
|
|
return -1;
|
|
|
|
|
2008-07-19 22:28:31 +00:00
|
|
|
rd->dst_socket = s->socket;
|
2008-01-02 15:36:51 +00:00
|
|
|
rd->src_socket = ntohs(fromaddr.sa_socket);
|
|
|
|
memcpy(rd->src_node, fromaddr.sa_nodenum, sizeof(fromaddr.sa_nodenum));
|
|
|
|
memset(rd->src_network, 0, 4);
|
|
|
|
rd->pkt_type = 0;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2009-01-26 05:01:18 +00:00
|
|
|
static int kali_general_packet_ready(socket_t *s)
|
2008-01-02 15:36:51 +00:00
|
|
|
{
|
2009-03-20 12:10:38 +00:00
|
|
|
return ipxdrv_general_packet_ready(s->fd);
|
2008-01-02 15:36:51 +00:00
|
|
|
}
|
|
|
|
|
2009-03-20 12:10:38 +00:00
|
|
|
struct net_driver ipxdrv_kali = {
|
2009-01-26 05:01:18 +00:00
|
|
|
kali_open_socket,
|
|
|
|
kali_close_socket,
|
|
|
|
kali_send_packet,
|
|
|
|
kali_receive_packet,
|
|
|
|
kali_general_packet_ready,
|
2008-01-02 15:36:51 +00:00
|
|
|
1,
|
|
|
|
NETPROTO_KALINIX
|
|
|
|
};
|