diff --git a/Makefile b/Makefile index e197f77..7ba3ff6 100644 --- a/Makefile +++ b/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) diff --git a/include/Client.h b/include/Client.h index 6bbc400..be95d91 100644 --- a/include/Client.h +++ b/include/Client.h @@ -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); diff --git a/include/Server.h b/include/Server.h index e459dd3..89daeec 100644 --- a/include/Server.h +++ b/include/Server.h @@ -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); diff --git a/include/util.h b/include/util.h index c77daf0..818b2f7 100644 --- a/include/util.h +++ b/include/util.h @@ -1,6 +1,13 @@ #pragma once +#include + #include /* size_t */ +#include + int IsProbablyAscii (const void *, const size_t); void HexDump (const void *, const size_t); + +void UDP_DumpPacket (UDPpacket*); +std::string HostAddress (IPaddress*); diff --git a/src/Client.cpp b/src/Client.cpp index c41f583..76cf8a3 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -1,7 +1,6 @@ #include "debug.h" #include "Client.h" - -#include /* 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; } diff --git a/src/Server.cpp b/src/Server.cpp index 6235239..d8737d7 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -1,12 +1,10 @@ #include "debug.h" #include "Server.h" #include "events_common.h" - -#include /* 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; diff --git a/src/UDP_Write.cpp b/src/UDP_Write.cpp index 2c0afe2..d4cc19d 100644 --- a/src/UDP_Write.cpp +++ b/src/UDP_Write.cpp @@ -1,6 +1,7 @@ #include "debug.h" #include "FileIO.h" #include "Client.h" +#include "util.h" #include @@ -11,12 +12,10 @@ #include #include -#include /* 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; diff --git a/src/client_main.cpp b/src/client_main.cpp index b3403e6..89f20ba 100644 --- a/src/client_main.cpp +++ b/src/client_main.cpp @@ -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)) diff --git a/src/server_main.cpp b/src/server_main.cpp index 6c0695d..94ff552 100644 --- a/src/server_main.cpp +++ b/src/server_main.cpp @@ -6,17 +6,13 @@ #include "events_common.h" #include "trim.h" #include "lib_lua_common.h" -extern "C" -{ #include "util.h" -}; #include #include /* exit */ #include /* getopt */ #include /* getopt */ -#include /* 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 ()); diff --git a/src/util.c b/src/util.c deleted file mode 100644 index c2b68ab..0000000 --- a/src/util.c +++ /dev/null @@ -1,47 +0,0 @@ -#include "util.h" - -#include /* printf */ -#include /* uint8_t */ -#include /* 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 (""); -} diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000..22698e4 --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,107 @@ +#include "util.h" + +#include + +#include /* printf */ +#include /* uint8_t */ +#include /* round */ +#include /* 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 ()); +} diff --git a/test/.gitignore b/test/.gitignore index cfaabe7..2a0ff48 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -2,3 +2,4 @@ test_FileIO test_File_Write_UDP test_lua test_Object +test/test_UDP_DumpPacket diff --git a/test/test_FileIO.cpp b/test/test_FileIO.cpp index 8888990..0dc82eb 100644 --- a/test/test_FileIO.cpp +++ b/test/test_FileIO.cpp @@ -1,8 +1,5 @@ #include "FileIO.h" -extern "C" -{ #include "util.h" -}; #include /* uint8_t */ #include diff --git a/test/test_UDP_DumpPacket.cpp b/test/test_UDP_DumpPacket.cpp new file mode 100644 index 0000000..5401681 --- /dev/null +++ b/test/test_UDP_DumpPacket.cpp @@ -0,0 +1,35 @@ +#include "util.h" + +#include +#include + +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; +}