Dump UDP packets
This commit is contained in:
parent
6a8f90500f
commit
0264283b6e
2
Makefile
2
Makefile
@ -158,6 +158,8 @@ test/test_lua: $(TEST_OBJ_COMMON) $(OBJDIR)/src/lib_lua_common.o
|
||||
|
||||
test/test_Client: $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDP_Write.o $(OBJDIR)/src/Client.o
|
||||
|
||||
test/test_UDP_DumpPacket: $(TEST_OBJ_COMMON) $(OBJDIR)/src/util.o
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
|
||||
$(PRECOMPILE)
|
||||
|
@ -18,10 +18,6 @@ class Client
|
||||
{
|
||||
DEBUG_LOG ("Client::Client ()");
|
||||
}
|
||||
~Client (void)
|
||||
{
|
||||
DEBUG_LOG ("Client::~Client ()");
|
||||
}
|
||||
Client (const char* host, const Uint16 port, const int channel);
|
||||
UDPsocket UDP_Open (const char* host, const Uint16 port, const int channel);
|
||||
void UDP_CloseAll (void);
|
||||
|
@ -25,14 +25,6 @@ class Server
|
||||
UDPPacketV = NULL;
|
||||
mProcessPacketMutex = NULL;
|
||||
}
|
||||
~Server (void)
|
||||
{
|
||||
// UDP_CloseAll ();
|
||||
// mProcessPacketMutex = SDL_CreateMutex ();
|
||||
// UDPsocks.clear ();
|
||||
// SocketSet = NULL;
|
||||
// UDPPacketV = NULL;
|
||||
}
|
||||
Server (const char* host, const Uint16 port);
|
||||
UDPsocket UDP_Open (Uint16 port);
|
||||
void UDP_CloseAll (void);
|
||||
|
@ -1,6 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <stddef.h> /* size_t */
|
||||
|
||||
#include <SDL_net.h>
|
||||
|
||||
int IsProbablyAscii (const void *, const size_t);
|
||||
void HexDump (const void *, const size_t);
|
||||
|
||||
void UDP_DumpPacket (UDPpacket*);
|
||||
std::string HostAddress (IPaddress*);
|
||||
|
@ -1,7 +1,6 @@
|
||||
#include "debug.h"
|
||||
#include "Client.h"
|
||||
|
||||
#include <arpa/inet.h> /* inet_ntop */
|
||||
#include "util.h"
|
||||
|
||||
Client::Client (const char *host, const Uint16 port, const int channel)
|
||||
{
|
||||
@ -38,8 +37,7 @@ Client::UDP_Open (const char *host, const Uint16 port, const int channel)
|
||||
DEBUG_LOG ("SDLNet_UDP_Bind: %s", SDLNet_GetError ());
|
||||
return NULL;
|
||||
}
|
||||
inet_ntop (AF_INET, &ipaddress.host, hoststring, 128);
|
||||
DEBUG_LOG ("Bound socket %s:%d ch%d", hoststring, ipaddress.port, channel);
|
||||
DEBUG_LOG ("Bound socket %s ch%d", HostAddress (&ipaddress).c_str (), channel);
|
||||
return udpsock;
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,10 @@
|
||||
#include "debug.h"
|
||||
#include "Server.h"
|
||||
#include "events_common.h"
|
||||
|
||||
#include <arpa/inet.h> /* inet_ntop */
|
||||
#include "util.h"
|
||||
|
||||
Server::Server (const char *host, const Uint16 port)
|
||||
{
|
||||
char hoststring[128];
|
||||
IPaddress ipaddress;
|
||||
UDPsocket udpsock;
|
||||
if (SDLNet_ResolveHost (&ipaddress, host, SDL_SwapBE16 (port)) < 0)
|
||||
@ -18,7 +16,7 @@ Server::Server (const char *host, const Uint16 port)
|
||||
{
|
||||
UDPsocks.push_back (udpsock);
|
||||
mProcessPacketMutex = SDL_CreateMutex ();
|
||||
inet_ntop (AF_INET, &ipaddress.host, hoststring, 128);
|
||||
HostAddress (&ipaddress);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -65,7 +63,7 @@ Server::ProcessPacket (UDPpacket * packet)
|
||||
UDPpacket *temp;
|
||||
len = packet->len;
|
||||
temp = SDLNet_AllocPacket (len);
|
||||
SDL_memcpy (&temp->address, &packet->address, sizeof (IPaddress));
|
||||
temp->address = packet->address;
|
||||
SDL_memcpy (temp->data, packet->data, len);
|
||||
temp->len = len;
|
||||
SDL_zero (event);
|
||||
@ -157,7 +155,6 @@ Server::Stop (void)
|
||||
SocketSet = NULL;
|
||||
UDPPacketV = NULL;
|
||||
|
||||
Unlock ();
|
||||
SDL_DestroyMutex (mProcessPacketMutex);
|
||||
mProcessPacketMutex = NULL;
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "debug.h"
|
||||
#include "FileIO.h"
|
||||
#include "Client.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
@ -11,12 +12,10 @@
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <arpa/inet.h> /* inet_ntop */
|
||||
/* XXX gross */
|
||||
int
|
||||
Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const void *buf, const size_t size)
|
||||
{
|
||||
char hoststring[128];
|
||||
const char *bufPtr;
|
||||
const char *bufPtrEnd;
|
||||
int numsent, npackets;
|
||||
@ -47,9 +46,9 @@ Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const
|
||||
if (NULL == (ipaddress = SDLNet_UDP_GetPeerAddress (udpsock, channel)))
|
||||
{
|
||||
DEBUG_LOG ("SDLNet_UDP_GetPeerAddress: %s", SDLNet_GetError ());
|
||||
return 0;
|
||||
}
|
||||
inet_ntop (AF_INET, &ipaddress->host, hoststring, 128);
|
||||
DEBUG_LOG ("Bound socket %s:%d ch%d", hoststring, ipaddress->port, channel);
|
||||
DEBUG_LOG ("Sending %ld bytes to %s ch%d", size, HostAddress (ipaddress), channel);
|
||||
size_t nsent = size;
|
||||
while (i < (size_t) npackets && bufPtr < bufPtrEnd)
|
||||
{
|
||||
@ -57,7 +56,7 @@ Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const
|
||||
if (our_nsent > our_mtu)
|
||||
our_nsent = our_mtu;
|
||||
packetV[i]->channel = channel;
|
||||
SDL_memcpy (&packetV[i]->address, ipaddress, sizeof (IPaddress));
|
||||
packetV[i]->address = *ipaddress;
|
||||
SDL_memset (packetV[i]->data, 0, our_mtu);
|
||||
SDL_memcpy (packetV[i]->data, bufPtr, our_nsent);
|
||||
packetV[i]->len = our_nsent;
|
||||
|
@ -336,6 +336,7 @@ my_Input_Key (SDL_Event * event)
|
||||
}
|
||||
}
|
||||
break;
|
||||
/*
|
||||
case SDLK_m:
|
||||
{
|
||||
if (down)
|
||||
@ -358,6 +359,7 @@ my_Input_Key (SDL_Event * event)
|
||||
state.RenderShowQuad = !state.RenderShowQuad;
|
||||
}
|
||||
break;
|
||||
*/
|
||||
default:
|
||||
{
|
||||
if (event->key.keysym.scancode != SDL_GetScancodeFromKey (event->key.keysym.sym))
|
||||
|
@ -6,17 +6,13 @@
|
||||
#include "events_common.h"
|
||||
#include "trim.h"
|
||||
#include "lib_lua_common.h"
|
||||
extern "C"
|
||||
{
|
||||
#include "util.h"
|
||||
};
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <stdlib.h> /* exit */
|
||||
#include <unistd.h> /* getopt */
|
||||
#include <getopt.h> /* getopt */
|
||||
#include <arpa/inet.h> /* inet_ntop */
|
||||
|
||||
static bool my_SDL_UserEvent (SDL_Event *);
|
||||
static void my_Usage (const char *);
|
||||
@ -122,19 +118,15 @@ my_ProcessPacket (UDPpacket* packet)
|
||||
|
||||
if (!((event.type == SDL_KEYDOWN) || (event.type == SDL_KEYUP)))
|
||||
{
|
||||
std::string buf ((const char*)packet->data, len);
|
||||
if (IsProbablyAscii (buf.c_str (), buf.length ()))
|
||||
printf ("%s", buf.c_str ());
|
||||
else
|
||||
HexDump (buf.c_str (), buf.length ());
|
||||
SDL_Log ("UDP_DumpPacket:");
|
||||
UDP_DumpPacket (packet);
|
||||
return;
|
||||
}
|
||||
|
||||
std::string str = std::string (SDL_GetKeyName (event.key.keysym.sym));
|
||||
bool down = (event.type == SDL_KEYDOWN);
|
||||
|
||||
inet_ntop (AF_INET, &packet->address.host, hoststring, 128);
|
||||
DEBUG_LOG ("%d bytes from %s:%d:\n%s", packet->len, hoststring, ntohs (packet->address.port), str.c_str ());
|
||||
HostAddress (&packet->address);
|
||||
|
||||
// SDL_Log ("%d \"%s\"", str.length(), str.c_str ());
|
||||
|
||||
|
47
src/util.c
47
src/util.c
@ -1,47 +0,0 @@
|
||||
#include "util.h"
|
||||
|
||||
#include <stdio.h> /* printf */
|
||||
#include <stdint.h> /* uint8_t */
|
||||
#include <math.h> /* round */
|
||||
|
||||
int
|
||||
IsProbablyAscii (const void *buf, const size_t len)
|
||||
{
|
||||
const uint8_t* bufPtr;
|
||||
const uint8_t* bufPtrEnd;
|
||||
size_t nAsciiCount;
|
||||
nAsciiCount = 0;
|
||||
bufPtr = (const uint8_t*) buf;
|
||||
bufPtrEnd = bufPtr + len;
|
||||
while (bufPtr < bufPtrEnd && (NULL != bufPtr) && (*bufPtr != '\0'))
|
||||
{
|
||||
nAsciiCount += (*bufPtr >= ' ') && (*bufPtr <= '~');
|
||||
bufPtr++;
|
||||
}
|
||||
return (int) round ((double) nAsciiCount / (double) len);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
HexDump (const void *buf, const size_t len)
|
||||
{
|
||||
const uint8_t* bufPtr;
|
||||
const uint8_t* bufPtrEnd;
|
||||
const size_t width = 16;
|
||||
size_t nout;
|
||||
nout = 0;
|
||||
bufPtr = (const uint8_t*) buf;
|
||||
bufPtrEnd = bufPtr + len;
|
||||
while (bufPtr < bufPtrEnd && (NULL != bufPtr))
|
||||
{
|
||||
if (nout == width)
|
||||
{
|
||||
printf ("\n");
|
||||
nout = 0;
|
||||
}
|
||||
printf ("%02x ", *bufPtr);
|
||||
bufPtr++;
|
||||
nout++;
|
||||
}
|
||||
puts ("");
|
||||
}
|
107
src/util.cpp
Normal file
107
src/util.cpp
Normal file
@ -0,0 +1,107 @@
|
||||
#include "util.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <stdio.h> /* printf */
|
||||
#include <stdint.h> /* uint8_t */
|
||||
#include <math.h> /* round */
|
||||
#include <arpa/inet.h> /* inet_ntop */
|
||||
|
||||
int
|
||||
IsProbablyAscii (const void *buf, const size_t len)
|
||||
{
|
||||
const uint8_t* bufPtr;
|
||||
const uint8_t* bufPtrEnd;
|
||||
size_t nAsciiCount;
|
||||
nAsciiCount = 0;
|
||||
bufPtr = (const uint8_t*) buf;
|
||||
bufPtrEnd = bufPtr + len;
|
||||
while (bufPtr < bufPtrEnd && (NULL != bufPtr) && (*bufPtr != '\0'))
|
||||
{
|
||||
nAsciiCount += (*bufPtr >= ' ') && (*bufPtr <= '~');
|
||||
bufPtr++;
|
||||
}
|
||||
return (int) round ((double) nAsciiCount / (double) len);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
HexDump (const void *buf, const size_t len)
|
||||
{
|
||||
const uint8_t* bufPtr;
|
||||
const uint8_t* bufPtrEnd;
|
||||
const size_t width = 16;
|
||||
size_t nout;
|
||||
nout = 0;
|
||||
bufPtr = (const uint8_t*) buf;
|
||||
bufPtrEnd = bufPtr + len;
|
||||
while (bufPtr < bufPtrEnd && (NULL != bufPtr))
|
||||
{
|
||||
if (nout == width)
|
||||
{
|
||||
printf ("\n");
|
||||
nout = 0;
|
||||
}
|
||||
printf ("%02x ", *bufPtr);
|
||||
bufPtr++;
|
||||
nout++;
|
||||
}
|
||||
puts ("");
|
||||
}
|
||||
|
||||
|
||||
std::string
|
||||
HostAddress (IPaddress* ipaddress)
|
||||
{
|
||||
char hostaddress[128];
|
||||
char hoststring[256];
|
||||
|
||||
SDL_assert (NULL != ipaddress);
|
||||
SDL_zero (hostaddress);
|
||||
SDL_zero (hoststring);
|
||||
|
||||
if (ipaddress->host)
|
||||
{
|
||||
inet_ntop (AF_INET, &ipaddress->host, hostaddress, 128);
|
||||
snprintf (hoststring, (sizeof hoststring) - 1, "%s:%d", hostaddress, SDLNet_Read16 (&ipaddress->port));
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf (hoststring, (sizeof hoststring) - 1, "0.0.0.0:%d", SDLNet_Read16 (&ipaddress->port));
|
||||
}
|
||||
|
||||
return std::string (hoststring);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
UDP_DumpPacket (UDPpacket* packet)
|
||||
{
|
||||
const char* buf;
|
||||
|
||||
SDL_assert (packet != NULL);
|
||||
// SDL_assert (packet->len > 0);
|
||||
|
||||
buf = (const char*)packet->data;
|
||||
|
||||
SDL_Log ("channel: %d", packet->channel);
|
||||
SDL_Log ("len : %d", packet->len);
|
||||
SDL_Log ("maxlen : %d", packet->maxlen);
|
||||
SDL_Log ("status : %d", packet->status);
|
||||
|
||||
SDL_Log ("address: %s", HostAddress (&packet->address).c_str ());
|
||||
|
||||
if (packet->len)
|
||||
{
|
||||
if (IsProbablyAscii (buf, packet->len))
|
||||
SDL_Log ("data:\n%s", buf);
|
||||
else
|
||||
HexDump (buf, packet->len);
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_Log ("No packet data!");
|
||||
}
|
||||
|
||||
//DEBUG_LOG ("%d bytes from %s:%d:\n%s", packet->len, hoststring, ntohs (packet->address.port), str.c_str ());
|
||||
}
|
1
test/.gitignore
vendored
1
test/.gitignore
vendored
@ -2,3 +2,4 @@ test_FileIO
|
||||
test_File_Write_UDP
|
||||
test_lua
|
||||
test_Object
|
||||
test/test_UDP_DumpPacket
|
||||
|
@ -1,8 +1,5 @@
|
||||
#include "FileIO.h"
|
||||
extern "C"
|
||||
{
|
||||
#include "util.h"
|
||||
};
|
||||
|
||||
#include <stdint.h> /* uint8_t */
|
||||
#include <stdio.h>
|
||||
|
35
test/test_UDP_DumpPacket.cpp
Normal file
35
test/test_UDP_DumpPacket.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include "util.h"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_net.h>
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
int channel;
|
||||
const size_t size = strlen (argv[0]);
|
||||
UDPpacket *packet;
|
||||
UDPsocket udpsock;
|
||||
|
||||
if (!(packet = SDLNet_AllocPacket (size)))
|
||||
{
|
||||
SDL_Log ("SDLNet_AllocPacket: %s", SDLNet_GetError ());
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* TODO Something with the channel. Keep track of it client side? */
|
||||
channel = SDLNet_UDP_Bind (udpsock, -1, address);
|
||||
if (channel == -1)
|
||||
{
|
||||
SDL_Log ("SDLNet_UDP_Bind: %s", SDLNet_GetError ());
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
packet->channel = -1;
|
||||
|
||||
packet->len = snprintf ((char *) packet->data, size - 1, "%s", argv[0]);
|
||||
|
||||
UDP_DumpPacket (packet);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user