]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/socket.cpp
Fix modstore/favourites hang by adding asynchronous lua job support
[dragonfireclient.git] / src / socket.cpp
index a889223b474873e1a5f3947ca06efc1ee455b7ee..c3873dab65131ab314cc2a4f8b5666b653c36263 100644 (file)
@@ -56,6 +56,7 @@ typedef int socket_t;
 #include <string.h>
 #include <errno.h>
 #include <sstream>
+#include <iomanip>
 #include "util/string.h"
 #include "util/numeric.h"
 
@@ -185,11 +186,41 @@ void Address::Resolve(const char *name)
 // IP address -> textual representation
 std::string Address::serializeString() const
 {
+// windows XP doesnt have inet_ntop, maybe use better func
+#ifdef _WIN32
+       if(m_addr_family == AF_INET)
+       {
+               u8 a, b, c, d, addr;
+               addr = ntohl(m_address.ipv4.sin_addr.s_addr);
+               a = (addr & 0xFF000000) >> 24;
+               b = (addr & 0x00FF0000) >> 16;
+               c = (addr & 0x0000FF00) >> 8;
+               d = (addr & 0x000000FF);
+               return itos(a) + "." + itos(b) + "." + itos(c) + "." + itos(d);
+       }
+       else if(m_addr_family == AF_INET6)
+       {
+               std::ostringstream os;
+               for(int i = 0; i < 16; i += 2)
+               {
+                       u16 section =
+                       (m_address.ipv6.sin6_addr.s6_addr[i] << 8) |
+                       (m_address.ipv6.sin6_addr.s6_addr[i + 1]);
+                       os << std::hex << section;
+                       if(i < 14)
+                               os << ":";
+               }
+               return os.str();
+       }
+       else
+               return std::string("");
+#else
        char str[INET6_ADDRSTRLEN];
        if (inet_ntop(m_addr_family, (m_addr_family == AF_INET) ? (void*)&(m_address.ipv4.sin_addr) : (void*)&(m_address.ipv6.sin6_addr), str, INET6_ADDRSTRLEN) == NULL) {
-           return std::string("");
+               return std::string("");
        }
        return std::string(str);
+#endif
 }
 
 struct sockaddr_in Address::getAddress() const
@@ -314,6 +345,8 @@ void UDPSocket::Bind(u16 port)
        if(m_addr_family == AF_INET6)
        {
                struct sockaddr_in6 address;
+               memset(&address, 0, sizeof(address));
+
                address.sin6_family = AF_INET6;
                address.sin6_addr   = in6addr_any;
                address.sin6_port   = htons(port);
@@ -329,6 +362,8 @@ void UDPSocket::Bind(u16 port)
        else
        {
                struct sockaddr_in address;
+               memset(&address, 0, sizeof(address));
+
                address.sin_family      = AF_INET;
                address.sin_addr.s_addr = INADDR_ANY;
                address.sin_port        = htons(port);
@@ -362,9 +397,10 @@ void UDPSocket::Send(const Address & destination, const void * data, int size)
                for(int i = 0; i < size && i < 20; i++)
                {
                        if(i % 2 == 0)
-                               DEBUGPRINT(" ");
+                               dstream << " ";
                        unsigned int a = ((const unsigned char *) data)[i];
-                       DEBUGPRINT("%.2X", a);
+                       dstream << std::hex << std::setw(2) << std::setfill('0')
+                               << a;
                }
                
                if(size > 20)
@@ -422,6 +458,7 @@ int UDPSocket::Receive(Address & sender, void * data, int size)
        if(m_addr_family == AF_INET6)
        {
                struct sockaddr_in6 address;
+               memset(&address, 0, sizeof(address));
                socklen_t address_len = sizeof(address);
 
                received = recvfrom(m_handle, (char *) data,
@@ -438,6 +475,8 @@ int UDPSocket::Receive(Address & sender, void * data, int size)
        else
        {
                struct sockaddr_in address;
+               memset(&address, 0, sizeof(address));
+
                socklen_t address_len = sizeof(address);
 
                received = recvfrom(m_handle, (char *) data,
@@ -464,9 +503,10 @@ int UDPSocket::Receive(Address & sender, void * data, int size)
                for(int i = 0; i < received && i < 20; i++)
                {
                        if(i % 2 == 0)
-                               DEBUGPRINT(" ");
+                               dstream << " ";
                        unsigned int a = ((const unsigned char *) data)[i];
-                       DEBUGPRINT("%.2X", a);
+                       dstream << std::hex << std::setw(2) << std::setfill('0')
+                               << a;
                }
                if(received > 20)
                        dstream << "...";
@@ -535,5 +575,3 @@ bool UDPSocket::WaitData(int timeout_ms)
        // There is data
        return true;
 }
-
-