[IOMultiplexerV2] added possibility to access remote and local socket addresses
[NextIRCd.git] / src / IOHandler / IOSockets.c
index a361126b5540dd10a69160587c9fcfca49bc597c..63a4115ebb49843dc307ecc817b8e033806a9b8e 100644 (file)
 #include "IOSSLBackend.h"
 
 #ifdef WIN32
+#ifdef _WIN32_WINNT
+#undef _WIN32_WINNT
+#endif
 #define _WIN32_WINNT 0x501
-#include <windows.h>
 #include <winsock2.h>
+#include <windows.h>
 #include <ws2tcpip.h>
 #else
 #include <sys/socket.h>
@@ -633,6 +636,9 @@ struct _IOSocket *iosocket_accept_client(struct _IOSocket *iosock) {
                new_iosock->socket_flags |= IOSOCKETFLAG_SSLSOCKET;
                \r
                iossl_client_accepted(iosock, new_iosock);
+       } else {
+               //initialize readbuf
+               iosocket_increase_buffer(&iosock->readbuf, 1024);
        }
        
        iosocket_activate(new_iosock);
@@ -794,6 +800,22 @@ void iosocket_close(struct IOSocket *iosocket) {
        iogc_add(iosocket);
 }
 
+struct IODNSAddress *iosocket_get_remote_addr(struct IOSocket *iosocket) {
+       if(iosock->socket_flags & IOSOCKETFLAG_PENDING_DESTDNS)
+               return NULL;
+       if(!iosock->dest.addr.addresslen)
+               return NULL;
+       return &iosock->dest.addr;
+}
+
+struct IODNSAddress *iosocket_get_local_addr(struct IOSocket *iosocket) {
+       if(iosock->socket_flags & IOSOCKETFLAG_PENDING_BINDDNS)
+               return NULL;
+       if(!iosock->bind.addr.addresslen)
+               return NULL;
+       return &iosock->bind.addr;
+}
+
 static int iosocket_try_write(struct _IOSocket *iosock) {
        if(!iosock->writebuf.bufpos && !(iosock->socket_flags & IOSOCKETFLAG_SSL_WRITEHS)) \r
                return 0;
@@ -996,12 +1018,16 @@ void iosocket_events_callback(struct _IOSocket *iosock, int readable, int writea
                        iosocketevents_callback_retry_read:
                        if((readable && ssl_rehandshake == 0) || ssl_rehandshake == 1) {
                                int bytes;
-                               if(iosock->readbuf.buflen - iosock->readbuf.bufpos >= 128) {
+                               if(iosock->readbuf.buflen - iosock->readbuf.bufpos <= 128) {
                                        int addsize;
                                        if(iosock->readbuf.buflen >= 2048)
                                                addsize = 1024;
                                        else
                                                addsize = iosock->readbuf.buflen;
+                                       if(addsize == 0) {
+                                               iolog_trigger(IOLOG_WARNING, "readbuf length is 0 when trying to read from fd %d", iosock->fd);
+                                               addsize = 512;
+                                       }
                                        iosocket_increase_buffer(&iosock->readbuf, iosock->readbuf.buflen + addsize);
                                }
                                if((iosock->socket_flags & IOSOCKETFLAG_SSLSOCKET))