int ReceiveMsgSize(SOCKET socket, int* size) { int retval = NVN_NOERR; int valid = 0; if(NVN_NOERR == ValidSocket(socket, &valid) && valid && size) { char buf[4]; int received = 0; int result = 0; while(received < 4) { result = recv(socket, buf + received, 4 - received, 0); if(result < 0) { PrintSocketError("recv failed"); retval = NVN_ECOMMFAIL; break; } else if(result == 0) { retval = NVN_ECLIENTGONE; break; } else { received += result; } } if(received == 4) { if(sizeof(int) == 4) { *size = *((int*)buf); *size = ntohl(*size); } else { retval = NVN_ERROR; fprintf(stderr, "Architecture not supported: sizeof(int) != 4\n"); } } } else { retval = NVN_EINVARGS; } return retval; }
BOOL CUDPSocketAsync::Close() { try { //Quit if not ok if (!ValidSocket()) return FALSE; //Kill the timer CAsyncSocket::SocketClosing(); //Remove from socket list RemoveSocketFromList(); //Delegate call return CUDPSocket::Close(); } ERROR_HANDLER_RETURN("Close",FALSE) }
int SendMsgSize(SOCKET socket, int size) { int retval = NVN_NOERR; char buf[4]; int sent = 0; int result = 0; int valid = 0; if(NVN_NOERR == ValidSocket(socket, &valid) && valid) { if(sizeof(int) == 4) { ((int*)buf)[0] = htonl(size); } else { retval = NVN_ERROR; fprintf(stderr, "Architecture not supported: sizeof(int) != 4\n"); } while(sent < 4) { result = send(socket, buf + sent, 4 - sent, 0); if(result <= 0) { retval = NVN_ECOMMFAIL; PrintSocketError("send failed"); break; } else { sent += result; } } } else { retval = NVN_EINVARGS; } return retval; }
// Create and bind the DNS Proxy Skts for use mDNSexport void mDNSPlatformInitDNSProxySkts(mDNS *const m, ProxyCallback UDPCallback, ProxyCallback TCPCallback) { int dpskt[4]; dpskt[0] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); dpskt[1] = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); dpskt[2] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); dpskt[3] = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); // Close all DNS Proxy skts in case any of them are invalid if (!ValidSocket(dpskt[0]) || !ValidSocket(dpskt[1]) || !ValidSocket(dpskt[2]) || !ValidSocket(dpskt[3])) { if (ValidSocket(dpskt[0])) close(dpskt[0]); if (ValidSocket(dpskt[1])) close(dpskt[1]); if (ValidSocket(dpskt[2])) close(dpskt[2]); if (ValidSocket(dpskt[3])) close(dpskt[3]); } BindDPSocket(dpskt[0], AF_INET); BindDPSocket(dpskt[1], AF_INET6); BindDPSocket(dpskt[2], AF_INET); BindDPSocket(dpskt[3], AF_INET6); LogInfo("mDNSPlatformInitDNSProxySkts: Opened Listener Sockets for DNS Proxy : %d, %d, %d, %d", dpskt[0], dpskt[1], dpskt[2], dpskt[3]); m->p->UDPProxyCallback = UDPCallback; m->p->TCPProxyCallback = TCPCallback; SetupDNSProxySkts(m, dpskt); }
BOOL CTCPSocketAsync::Close() { try { //Quit if not ok if (!ValidSocket()) return FALSE; //Delegate to remove socket if (m_pLinkedSocket && m_bCloseEventsOnly) m_pLinkedSocket->Close(); //Kill the timer CAsyncSocket::SocketClosing(); //Remove from socket list RemoveSocketFromList(); //Done return CTCPSocket::Close(); } ERROR_HANDLER_RETURN("Close",FALSE) }