int AcceptTcpConnection(int listenSock) { int sock; struct sockaddr_in addr; #if defined(WIN32) int addrlen = sizeof(addr); #else socklen_t addrlen = sizeof(addr); #endif int one = 1; sock = accept(listenSock, (struct sockaddr *) &addr, &addrlen); if (sock < 0) { aLog("AcceptTcpConnection: accept"); return -1; } if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)) < 0) { aLog("AcceptTcpConnection: setsockopt"); close(sock); return -1; } return sock; }
int ConnectToTcpAddr(unsigned int host, int port) { int sock; struct sockaddr_in addr; int one = 1; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = host; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { aLog("ConnectToTcpAddr: socket\n" ); return -1; } if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { aLog("ConnectToTcpAddr: connect"); close(sock); return -1; } if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)) < 0) { aLog("ConnectToTcpAddr: setsockopt"); close(sock); return -1; } return sock; }
// A helper function for creating failure error messages in // AutoObjectMapper*::Map. static void failedToMessage(void(*aLog)(const char*), const char* aHowFailed, std::string aFileName) { char buf[300]; snprintf(buf, sizeof(buf), "AutoObjectMapper::Map: Failed to %s \'%s\'", aHowFailed, aFileName.c_str()); buf[sizeof(buf)-1] = 0; aLog(buf); }
int ListenAtTcpPort(int port) { int sock; struct sockaddr_in addr; int one = 1; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { aLog("ListenAtTcpPort: socket"); return -1; } if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, sizeof(one)) < 0) { aLog("ListenAtTcpPort: setsockopt"); close(sock); return -1; } if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { aLog("ListenAtTcpPort: bind"); close(sock); return -1; } if (listen(sock, 5) < 0) { aLog("ListenAtTcpPort: listen"); close(sock); return -1; } return sock; }
Bool WriteExact(int sock, char *buf, int n) { fd_set fds; int i = 0; int j; while (i < n) { #if defined(WIN32) j = send (sock, buf + i, (n - i), 0); #else j = write(sock, buf + i, (n - i)); #endif if (j <= 0) { if (j < 0) { if (errno == EAGAIN) { FD_ZERO(&fds); FD_SET(sock,&fds); if (select(sock+1, NULL, &fds, NULL, NULL) <= 0) { aLog("error : select"); return False; } j = 0; } else { aLog("error : write"); return False; } } else { aLog("write failed\n"); return False; } } i += j; } return True; }
// EGPublicKey::Encrypt // Encrypts a block of data of the specified length using ElGamal encryption for this // key. Returns allocated block/len of encrypted data. EGPublicKey::CryptReturn EGPublicKey::Encrypt(const void* theMsgP, unsigned long theLen) const { WTRACE("EGPublicKey::Encrypt"); // Sanity check if ((! theMsgP) || (theLen == 0)) { WDBG_LM("EGPublicKey::Encrypt No data to encrypt"); return CryptReturn(NULL,0); } try { // Allocate crypt object if needed AllocateCrypt(); // Encrypt the data ByteQueue aQueue; EncryptData(aQueue, reinterpret_cast<const unsigned char*>(theMsgP), theLen); // Build return WDBG_LL("EGPublicKey::Encrypt Building return values."); unsigned long anOutLen = aQueue.MaxRetrieveable(); if (anOutLen == 0) { WDBG_LM("EGPublicKey::Encrypt Encrypt failed, no data."); return CryptReturn(NULL, 0); } auto_ptr<unsigned char> anOutP(new unsigned char [anOutLen]); aQueue.Get(anOutP.get(), anOutLen); WDBG_LM("EGPublicKey::Encrypt Encrypted, out block len=" << anOutLen); return CryptReturn(anOutP.release(), anOutLen); } catch (Exception& anEx) { // Little trick here, construct a CryptException to auto log the failure! WDBG_AH("EGPublicKey::Encrypt Caught CryptoLib exception: " << anEx.what()); #ifndef _WONCRYPT_NOEXCEPTIONS WONCrypt::CryptException aLog(WONCommon::ExCryptoLib, __LINE__, __FILE__, anEx.what()); #endif return CryptReturn(NULL,0); } }
// EGPublicKey::Verify // Verifies a signature for a message using ElGamal signature verification. Returns // true if sig verifies, false if not. bool EGPublicKey::Verify(const unsigned char* theSigP, unsigned long theSigLen, const void* theMsgP, unsigned long theMsgLen) const { WTRACE("EGPublicKey::Verify"); WDBG_LL("EGPublicKey::Verify, sig len=" << theSigLen << ", msg len=" << theMsgLen); // Sanity check if ((! theMsgP) || (theMsgLen == 0)) { WDBG_LM("EGPublicKey::Verify No data to verify."); return false; } try { // Allocate sig object if needed AllocateSig(); // Does given sig look ok? if ((! theSigP) || (theSigLen != mSigP->SignatureLength())) { WDBG_LM("EGPublicKey::Verify Sig NULL or length mismatch (sigLen=" << theSigLen << ", Expected len=" << mSigP->SignatureLength()); return false; } // Return signature verification auto_ptr<HashModule> aHashP(mSigP->NewMessageAccumulator()); aHashP->Update(reinterpret_cast<const unsigned char*>(theMsgP), theMsgLen); bool aRet = mSigP->Verify(aHashP.release(), theSigP); WDBG_LM("EGPublicKey::Verify Sig Verify=" << aRet); return aRet; } catch (Exception& anEx) { // Little trick here, construct a CryptException to auto log the failure! WDBG_AH("EGPublicKey::Verify Caught CryptoLib exception: " << anEx.what()); #ifndef _WONCRYPT_NOEXCEPTIONS WONCrypt::CryptException aLog(WONCommon::ExCryptoLib, __LINE__, __FILE__, anEx.what()); #endif return false; } }
Bool VNCViewer::ReadFromRFBServer(char *out, unsigned int n) { if (n <= buffered) { memcpy(out, bufoutptr, n); bufoutptr += n; buffered -= n; return True; } memcpy(out, bufoutptr, buffered); out += buffered; n -= buffered; bufoutptr = buf; buffered = 0; if (n <= BUF_SIZE) { while (buffered < n) { #if defined(WIN32) int i = recv(rfbsock, buf + buffered, BUF_SIZE - buffered,0); #else int i = read(rfbsock, buf + buffered, BUF_SIZE - buffered); #endif if (i <= 0) { if (i < 0) { if (errno == EAGAIN) { i = 0; } else { aLog("error : read1 -- i %d errno %d rfbsock %d\n", i, errno, rfbsock); return False; } } else { aLog("VNC server closed connection\n"); return False; } } buffered += i; } memcpy(out, bufoutptr, n); bufoutptr += n; buffered -= n; return True; } else { while (n > 0) { #if defined(WIN32) int i = recv(rfbsock, out, n, 0); #else int i = read(rfbsock, out, n); #endif if (i <= 0) { if (i < 0) { if (errno == EAGAIN) { i = 0; } else { aLog("error : read2 -- i %d errno %d\n", i, errno); return False; } } else { aLog("VNC server closed connection\n"); return False; } } out += i; n -= i; } return True; } }
Bool SetNonBlocking(int sock) { #if defined (WIN32) unsigned long one = 1; if (ioctlsocket (sock, FIONBIO, &one) ) { aLog("SetNonBlocking: fcntl"); return False; } return True; #else #ifdef O_ASYNC if (fcntl(sock, F_SETFL, O_ASYNC) < 0) { #else int yes = 1; if (ioctl(sock, FIOASYNC, &yes) < 0) { #endif aLog("SetNonBlocking: fcntl"); return False; } return True; #endif } /* * StringToIPAddr - convert a host string to an IP address. */ Bool StringToIPAddr(const char *str, unsigned long *addr) { struct hostent *hp; if (strcmp(str,"") == 0) { *addr = 0; /* local */ return True; } *addr = inet_addr(str); aLog("inet_addr %s = %d\n", str, *addr); if (*addr != INADDR_NONE) return True; hp = gethostbyname(str); aLog("gethostbyname %s = %x\n", str, hp); if (hp) { *addr = *(unsigned int *)hp->h_addr; return True; } return False; } /* * Test if the other end of a socket is on the same machine. */ Bool SameMachine(int sock) { struct sockaddr_in peeraddr, myaddr; #if defined (WIN32) int addrlen = sizeof(struct sockaddr_in); #else socklen_t addrlen = sizeof(struct sockaddr_in); #endif getpeername(sock, (struct sockaddr *)&peeraddr, &addrlen); getsockname(sock, (struct sockaddr *)&myaddr, &addrlen); return (peeraddr.sin_addr.s_addr == myaddr.sin_addr.s_addr); }