Added python command app to oskit test.

svn path=/trunk/; revision=10771
This commit is contained in:
Art Yerkes 2004-09-02 02:33:11 +00:00
parent 05cf09a12d
commit b880d380cc
3 changed files with 154 additions and 13 deletions

View file

@ -0,0 +1,10 @@
from socket import *
import sys
s = socket(AF_INET,SOCK_DGRAM,0)
s.connect(('localhost',5001))
while 1:
sys.stdout.write('>> ')
line = sys.stdin.readline()
s.send('CMD ' + line)

View file

@ -1,5 +1,5 @@
#
# $Id: makefile,v 1.1 2004/08/30 04:47:33 arty Exp $
# $Id: makefile,v 1.2 2004/09/02 02:33:11 arty Exp $
PATH_TO_TOP = ../../..
@ -15,7 +15,7 @@ TARGET_SDKLIBS = ws2_32.a oskittcp.a ntdll.a
TARGET_OBJECTS = tcptest.o -lstdc++
TARGET_CPPFLAGS = -I$(PATH_TO_TOP)/drivers/lib/oskittcp/include -I$(PATH_TO_TOP)/w32api/include -I$(PATH_TO_TOP)/include
TARGET_CPPFLAGS = -I$(PATH_TO_TOP)/drivers/lib/oskittcp/include -I$(PATH_TO_TOP)/w32api/include -I$(PATH_TO_TOP)/include -g
include $(PATH_TO_TOP)/rules.mak

View file

@ -1,12 +1,16 @@
#include <list>
#include <string>
#include <sstream>
extern "C" {
typedef unsigned short u_short;
#include <stdio.h>
#include <oskittcp.h>
#include <windows.h>
#include <winsock2.h>
};
char hwaddr[6] = { 0x08, 0x00, 0x20, 0x0b, 0xb7, 0xbb };
#undef malloc
#undef free
@ -45,8 +49,13 @@ int TCPSocketState(void *ClientData,
#define STRINGIFY(x) #x
char hdr[14] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00 };
int TCPPacketSend(void *ClientData, OSK_PCHAR data, OSK_UINT len ) {
output_packets.push_back( std::string( (char *)data, (int)len ) );
output_packets.push_back( std::string( hdr, 14 ) +
std::string( (char *)data, (int)len ) );
return 0;
}
@ -54,16 +63,31 @@ struct ifaddr *TCPFindInterface( void *ClientData,
OSK_UINT AddrType,
OSK_UINT FindType,
struct sockaddr *ReqAddr ) {
return NULL;
static struct sockaddr_in ifa = { AF_INET }, nm = { AF_INET };
static struct ifaddr a = {
(struct sockaddr *)&ifa,
NULL,
(struct sockaddr *)&nm,
0,
0,
1,
1500
};
ifa.sin_addr.s_addr = inet_addr( "10.10.2.115" );
nm.sin_addr.s_addr = inet_addr( "255.255.255.0" );
return &a;
}
void *TCPMalloc( void *ClientData,
OSK_UINT Bytes, OSK_PCHAR File, OSK_UINT Line ) {
return malloc( Bytes );
void *v = malloc( Bytes );
fprintf( stderr, "(%s:%d) malloc( %d ) => %x\n", File, Line, Bytes, v );
return v;
}
void TCPFree( void *ClientData,
void *data, OSK_PCHAR File, OSK_UINT Line ) {
fprintf( stderr, "(%s:%d) free( %x )\n", File, Line, data );
free( data );
}
@ -134,12 +158,30 @@ OSKITTCP_EVENT_HANDLERS EventHandlers = {
TCPWakeup
};
void display_row( char *data, int off, int len ) {
int i;
printf( "%08x:", off );
for( i = off; i < len && i < off + 16; i++ ) {
printf( " %02x", data[i] & 0xff );
}
printf( " -- " );
for( i = off; i < len && i < off + 16; i++ ) {
printf( "%c", (data[i] >= ' ') ? data[i] : '.' );
}
printf( "\n" );
}
int main( int argc, char **argv ) {
int asock = INVALID_SOCKET, selret, dgrecv, fromsize, err, port = 5000;
int asock = INVALID_SOCKET, selret, dgrecv, fromsize, err, port = 5001;
char datagram[MAX_DG_SIZE];
void *conn;
struct fd_set readf;
struct timeval tv;
struct sockaddr_in addr_from = { AF_INET };
struct sockaddr_in addr_from = { AF_INET }, addr_to;
std::list<std::string>::iterator i;
if( argc > 1 ) port = atoi(argv[1]);
@ -168,11 +210,97 @@ int main( int argc, char **argv ) {
dgrecv = recvfrom( asock, datagram, sizeof(datagram), 0,
(struct sockaddr *)&addr_from, &fromsize );
if( dgrecv > 0 ) {
OskitTCPReceiveDatagram( (unsigned char *)datagram,
dgrecv, 20 );
if( err != 0 )
printf( "OskitTCPReceiveDatagram: %d\n", err );
if( datagram[0] == 'C' && datagram[1] == 'M' &&
datagram[2] == 'D' && datagram[3] == ' ' ) {
int theport, bytes, recvret, off, bytin;
struct sockaddr_in nam;
std::string faddr, word;
std::istringstream
cmdin( std::string( datagram + 4, dgrecv - 4 ) );
cmdin >> word;
if( word == "socket" ) {
cmdin >> faddr >> theport;
nam.sin_family = AF_INET;
nam.sin_addr.s_addr = inet_addr(faddr.c_str());
nam.sin_port = htons(theport);
if( (err = OskitTCPSocket( NULL, &conn, AF_INET,
SOCK_STREAM, 0 )) != 0 ) {
fprintf( stderr, "OskitTCPSocket: error %d\n", err );
}
if( (err = OskitTCPConnect( conn, NULL, &nam,
sizeof(nam) )) != 0 ) {
fprintf( stderr, "OskitTCPConnect: error %d\n", err );
} else {
printf( "Socket created\n" );
}
} else if( word == "recv" ) {
cmdin >> bytes;
if( (err = OskitTCPRecv( conn, (OSK_PCHAR)datagram,
sizeof(datagram),
(unsigned int *)&bytin, 0 )) != 0 ) {
fprintf( stderr, "OskitTCPRecv: error %d\n", err );
} else {
for( off = 0; off < bytin; off += 16 ) {
display_row( datagram, off, bytin );
}
printf( "\n" );
}
} else if( word == "send" ) {
off = 0;
while( cmdin >> word ) {
datagram[off++] =
atoi( (std::string("0x") + word).c_str() );
}
if( (err = OskitTCPSend( conn, (OSK_PCHAR)datagram,
off, (OSK_UINT *)&bytin, 0 ))
!= 0 ) {
fprintf( stderr, "OskitTCPConnect: error %d\n", err );
} else {
printf( "wrote %d bytes\n", bytin );
}
} else if( word == "close" ) {
OskitTCPClose( conn );
}
} else if( dgrecv > 14 ) {
addr_to = addr_from;
if( datagram[12] == 8 && datagram[13] == 6 ) {
/* Answer arp query */
char laddr[4];
/* Mark patch as to the previous sender */
memcpy( datagram + 32, datagram + 6, 6 );
memcpy( datagram, datagram + 6, 6 );
/* Mark packet as from us */
memcpy( datagram + 22, hwaddr, 6 );
memcpy( datagram + 6, hwaddr, 6 );
/* Swap inet addresses */
memcpy( laddr, datagram + 28, 4 );
memcpy( datagram + 28, datagram + 38, 4 );
memcpy( datagram + 38, laddr, 4 );
/* Set reply opcode */
datagram[21] = 2;
err = sendto( asock, datagram, dgrecv, 0,
(struct sockaddr *)&addr_to,
sizeof(addr_to) );
if( err != 0 )
printf( "sendto: %d\n", err );
} else {
memcpy( hdr, datagram + 6, 6 );
memcpy( hdr + 6, datagram, 6 );
memcpy( hdr + 12, datagram + 12, 2 );
OskitTCPReceiveDatagram
( (unsigned char *)datagram + 14,
dgrecv - 14, 20 );
}
}
}
@ -180,7 +308,9 @@ int main( int argc, char **argv ) {
for( i = output_packets.begin(); i != output_packets.end(); i++ ) {
err = sendto( asock, i->c_str(), i->size(), 0,
(struct sockaddr *)&addr_from, sizeof(addr_from) );
(struct sockaddr *)&addr_to, sizeof(addr_to) );
fprintf( stderr, "** SENDING PACKET %d bytes **\n", i->size() );
if( err != 0 )
printf( "sendto: %d\n", err );
@ -189,3 +319,4 @@ int main( int argc, char **argv ) {
output_packets.clear();
}
}