1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
|
/*******************************************************************************
* shroudBNC - an object-oriented framework for IRC *
* Copyright (C) 2005-2007 Gunnar Beutner *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
*******************************************************************************/
#include "StdAfx.h"
#define BLOCKSIZE 4096
IMPL_DNSEVENTPROXY(CConnection, AsyncDnsFinished);
IMPL_DNSEVENTPROXY(CConnection, AsyncBindIpDnsFinished);
/**
* CConnection
*
* Constructs a new connection object.
*
* @param Socket the underlying socket
* @param SSL whether to use SSL
* @param Role the role of the connection
*/
CConnection::CConnection(SOCKET Socket, bool SSL, connection_role_e Role) {
char Address[MAX_SOCKADDR_LEN];
socklen_t AddressLength = sizeof(Address);
SetRole(Role);
if (Socket != INVALID_SOCKET) {
safe_getsockname(Socket, (sockaddr *)Address, &AddressLength);
m_Family = ((sockaddr *)Address)->sa_family;
} else {
m_Family = AF_INET;
}
InitConnection(Socket, SSL);
}
/**
* CConnection
*
* Constructs a new connection object.
*
* @param Host the remote host
* @param Port the port
* @param BindIp the local bind address
* @param SSL whether to use SSL for the connection
* @param Family the socket family
*/
CConnection::CConnection(const char *Host, unsigned short Port, const char *BindIp, bool SSL, int Family) {
m_Family = Family;
SetRole(Role_Client);
InitConnection(INVALID_SOCKET, SSL);
m_Socket = INVALID_SOCKET;
m_PortCache = Port;
m_BindIpCache = BindIp ? strdup(BindIp) : NULL;
if (Host != NULL) {
m_DnsQuery = new CDnsQuery(this, USE_DNSEVENTPROXY(CConnection, AsyncDnsFinished));
m_DnsQuery->GetHostByName(Host, Family);
if (m_BindIpCache != NULL) {
m_BindDnsQuery = new CDnsQuery(this, USE_DNSEVENTPROXY(CConnection, AsyncBindIpDnsFinished));
m_BindDnsQuery->GetHostByName(BindIp, Family);
} else {
m_BindDnsQuery = NULL;
}
// try to connect.. maybe we already have both addresses
AsyncConnect();
}
}
/**
* InitConnection
*
* Initializes a new connection object.
*
* @param Client the socket
* @param SSL whether to use SSL
*/
void CConnection::InitConnection(SOCKET Client, bool SSL) {
m_Socket = Client;
m_Locked = false;
m_Shutdown = false;
m_Timeout = 0;
m_Traffic = NULL;
m_DnsQuery = NULL;
m_BindDnsQuery = NULL;
m_HostAddr = NULL;
m_BindAddr = NULL;
m_BindIpCache = NULL;
m_PortCache = 0;
m_LatchedDestruction = false;
m_Connected = false;
m_InboundTrafficReset = g_CurrentTime;
m_InboundTraffic = 0;
#ifdef USESSL
m_HasSSL = SSL;
m_SSL = NULL;
if (GetRole() == Role_Server && g_Bouncer->GetSSLContext() == NULL && SSL) {
m_HasSSL = false;
LOGERROR("No SSL server certificate available. Falling back to"
" non-SSL mode. This might not work.");
}
#endif
if (Client != INVALID_SOCKET) {
InitSocket();
}
m_SendQ = new CFIFOBuffer();
m_RecvQ = new CFIFOBuffer();
}
/**
* ~CConnection
*
* Destructs the connection object.
*/
CConnection::~CConnection(void) {
g_Bouncer->UnregisterSocket(m_Socket);
delete m_DnsQuery;
delete m_BindDnsQuery;
free(m_BindIpCache);
if (m_Socket != INVALID_SOCKET) {
safe_shutdown(m_Socket, SD_BOTH);
safe_closesocket(m_Socket);
}
free(m_HostAddr);
free(m_BindAddr);
delete m_SendQ;
delete m_RecvQ;
#ifdef USESSL
if (IsSSL() && m_SSL != NULL) {
SSL_free(m_SSL);
}
#endif
}
/**
* InitSocket
*
* Initializes the socket.
*/
void CConnection::InitSocket(void) {
if (m_Socket == INVALID_SOCKET) {
return;
}
#ifndef _WIN32
const int optLinger = 0;
setsockopt(m_Socket, SOL_SOCKET, SO_LINGER, &optLinger, sizeof(optLinger));
#endif
#ifdef USESSL
if (IsSSL()) {
if (m_SSL != NULL) {
SSL_free(m_SSL);
}
if (GetRole() == Role_Client) {
m_SSL = SSL_new(g_Bouncer->GetSSLClientContext());
} else {
m_SSL = SSL_new(g_Bouncer->GetSSLContext());
}
if (m_SSL != NULL) {
BIO *Bio = BIO_new_safe_socket(m_Socket, 0);
SSL_set_bio(m_SSL, Bio, Bio);
//SSL_set_fd(m_SSL, m_Socket);
if (GetRole() == Role_Client) {
SSL_set_connect_state(m_SSL);
} else {
SSL_set_accept_state(m_SSL);
}
SSL_set_ex_data(m_SSL, g_Bouncer->GetSSLCustomIndex(), this);
}
} else {
m_SSL = NULL;
}
#endif
g_Bouncer->RegisterSocket(m_Socket, (CSocketEvents *)this);
}
/**
* SetSocket
*
* Sets the socket for the connection object.
*
* @param Socket the new socket
*/
void CConnection::SetSocket(SOCKET Socket) {
m_Socket = Socket;
if (Socket != INVALID_SOCKET) {
InitSocket();
}
}
/**
* GetSocket
*
* Returns the socket for the connection object.
*/
SOCKET CConnection::GetSocket(void) const {
return m_Socket;
}
/**
* Read
*
* Called when data is available on the socket.
*
* @param DontProcess determines whether to process the data
*/
int CConnection::Read(bool DontProcess) {
int ReadResult;
static char *Buffer = NULL;
static int BufferSize = 0;
socklen_t OptLenght = sizeof(BufferSize);
m_Connected = true;
if (m_Shutdown) {
return 0;
}
if (BufferSize == 0 && safe_getsockopt(m_Socket, SOL_SOCKET, SO_RCVBUF, (char *)&BufferSize, &OptLenght) != 0) {
BufferSize = 8192;
}
if (Buffer == NULL) {
Buffer = (char *)malloc(BufferSize);
}
CHECK_ALLOC_RESULT(Buffer, malloc) {
return -1;
} CHECK_ALLOC_RESULT_END;
#ifdef USESSL
if (IsSSL()) {
ReadResult = SSL_read(m_SSL, Buffer, BufferSize);
if (ReadResult < 0) {
switch (SSL_get_error(m_SSL, ReadResult)) {
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_WANT_READ:
case SSL_ERROR_NONE:
case SSL_ERROR_ZERO_RETURN:
return 0;
default:
return -1;
}
}
ERR_print_errors_fp(stdout);
} else
#endif
ReadResult = safe_recv(m_Socket, Buffer, BufferSize, 0);
if (ReadResult > 0) {
if (g_CurrentTime - m_InboundTrafficReset > 30) {
m_InboundTrafficReset = g_CurrentTime;
m_InboundTraffic = 0;
}
m_InboundTraffic += ReadResult;
m_RecvQ->Write(Buffer, ReadResult);
if (m_Traffic) {
m_Traffic->AddInbound(ReadResult);
}
} else {
int ErrorCode;
if (ReadResult == 0) {
return -1;
}
ErrorCode = safe_errno();
#ifdef _WIN32
if (ErrorCode == WSAEWOULDBLOCK) {
return 0;
}
#else
if (ErrorCode == EAGAIN) {
return 0;
}
#endif
#ifdef USESSL
if (IsSSL()) {
SSL_shutdown(m_SSL);
}
#endif
return ErrorCode;
}
if (!DontProcess) {
ProcessBuffer();
}
return 0;
}
/**
* Write
*
* Called when data can be written for the socket.
*/
int CConnection::Write(void) {
size_t Size;
int ReturnValue;
Size = m_SendQ->GetSize();
if (Size > 0) {
int WriteResult;
#ifdef USESSL
if (IsSSL()) {
WriteResult = SSL_write(m_SSL, m_SendQ->Peek(), Size);
if (WriteResult == -1) {
switch (SSL_get_error(m_SSL, WriteResult)) {
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_WANT_READ:
return 0;
default:
break;
}
}
} else {
#endif
WriteResult = safe_send(m_Socket, m_SendQ->Peek(), Size, 0);
#ifdef USESSL
}
#endif
ReturnValue = safe_errno();
if (WriteResult > 0) {
if (m_Traffic) {
m_Traffic->AddOutbound(WriteResult);
}
m_SendQ->Read(WriteResult);
} else if (WriteResult < 0) {
Shutdown();
}
}
if (m_Shutdown) {
#ifdef USESSL
if (IsSSL()) {
SSL_shutdown(m_SSL);
}
#endif
if (m_Socket != INVALID_SOCKET) {
safe_shutdown(m_Socket, SD_BOTH);
safe_closesocket(m_Socket);
}
}
return ReturnValue;
}
/**
* ProcessBuffer
*
* Processes the data which is in the recvq.
*/
void CConnection::ProcessBuffer(void) {
char *RecvQ, *Line;
size_t Size;
Line = RecvQ = m_RecvQ->Peek();
Size = m_RecvQ->GetSize();
for (unsigned int i = 0; i < Size; i++) {
if (RecvQ[i] == '\n' || (RecvQ[i] == '\r' && Size > i + 1 && RecvQ[i + 1] == '\n')) {
char *dupLine = (char *)malloc(&(RecvQ[i]) - Line + 1);
CHECK_ALLOC_RESULT(dupLine, malloc) {
return;
} CHECK_ALLOC_RESULT_END;
mmark(dupLine);
memcpy(dupLine, Line, &(RecvQ[i]) - Line);
dupLine[&(RecvQ[i]) - Line] = '\0';
if (dupLine[0] != '\0') {
ParseLine(dupLine);
}
free(dupLine);
Line = &RecvQ[i + 1];
}
}
m_RecvQ->Read(Line - RecvQ);
}
/**
* ReadLine
*
* Reads a line from the recvq and places a pointer to the line in "Out". This
* function is rather inefficient. Use ReadLines() instead.
*
* @param Out points to the line
*/
bool CConnection::ReadLine(char **Out) {
char *old_recvq;
size_t Size;
char *Pos = NULL;
bool advance = false;
old_recvq = m_RecvQ->Peek();
if (old_recvq == NULL) {
return false;
}
Size = m_RecvQ->GetSize();
for (unsigned int i = 0; i < Size; i++) {
if (old_recvq[i] == '\n' || (Size > i + 1 && old_recvq[i] == '\r' && old_recvq[i + 1] == '\n')) {
if (old_recvq[i] == '\r') {
advance = true;
}
Pos = old_recvq + i;
break;
}
}
if (Pos != NULL) {
size_t Size;
*Pos = '\0';
char *NewPtr = Pos + 1 + (advance ? 1 : 0);
Size = NewPtr - old_recvq + 1;
*Out = (char *)g_Bouncer->GetUtilities()->Alloc(Size);
strmcpy(*Out, m_RecvQ->Read(NewPtr - old_recvq), Size);
CHECK_ALLOC_RESULT(*Out, strdup) {
return false;
} CHECK_ALLOC_RESULT_END;
return true;
} else {
*Out = NULL;
return false;
}
}
/**
* WriteUnformattedLine
*
* Writes a line for the connection.
*
* @param Line the line
*/
void CConnection::WriteUnformattedLine(const char *Line) {
if (!m_Locked && Line != NULL) {
m_SendQ->WriteUnformattedLine(Line);
}
}
/**
* WriteLine
*
* Writes a line for the connection.
*
* @param Format the format string
* @param ... additional parameters used in the format string
*/
void CConnection::WriteLine(const char *Format, ...) {
char *Line;
va_list Marker;
if (m_Shutdown) {
return;
}
va_start(Marker, Format);
vasprintf(&Line, Format, Marker);
va_end(Marker);
CHECK_ALLOC_RESULT(Line, vasprintf) {
return;
} CHECK_ALLOC_RESULT_END;
WriteUnformattedLine(Line);
free(Line);
}
/**
* ParseLine
*
* Processes a line.
*
* @param Line the line
*/
void CConnection::ParseLine(const char *Line) {
// default implementation ignores any data
}
/**
* GetRole
*
* Returns the role of the connection.
*/
connection_role_e CConnection::GetRole(void) const {
return m_Role;
}
/**
* SetRole
*
* Sets the role of the connection.
*
* @param Role the role of the connection
*/
void CConnection::SetRole(connection_role_e Role) {
m_Role = Role;
}
/**
* Kill
*
* "Kills" the connection.
*
* @param Error the reason
*/
void CConnection::Kill(const char *Error) {
if (m_Shutdown) {
return;
}
SetTrafficStats(NULL);
Shutdown();
Timeout(10);
}
/**
* HasQueuedData
*
* Checks whether the connection object has data which can be
* written to the socket.
*/
bool CConnection::HasQueuedData(void) const {
#ifdef USESSL
if (IsSSL()) {
if (SSL_want_write(m_SSL)) {
return true;
}
if (SSL_state(m_SSL) != SSL_ST_OK) {
return false;
}
}
#endif
return m_SendQ->GetSize() > 0;
}
/**
* GetSendqSize
*
* Returns the size of the sendq.
*/
size_t CConnection::GetSendqSize(void) const {
return m_SendQ->GetSize();
}
/**
* GetRecvqSize
*
* Returns the size of the recvq.
*/
size_t CConnection::GetRecvqSize(void) const {
return m_RecvQ->GetSize();
}
/**
* Error
*
* Called when an error occured for the socket.
*
* @param ErrorCode the error code
*/
void CConnection::Error(int ErrorCode) {
}
/**
* Destroy
*
* Destroys the connection object.
*/
void CConnection::Destroy(void) {
delete this;
}
/**
* Lock
*
* Locks the connection object.
*/
void CConnection::Lock(void) {
m_Locked = true;
}
/**
* IsLocked
*
* Checks whether the connection object is locked.
*/
bool CConnection::IsLocked(void) const {
return m_Locked;
}
/**
* Shutdown
*
* Shuts down the connection object.
*/
void CConnection::Shutdown(void) {
m_Shutdown = true;
}
/**
* Timeout
*
* Sets the timeout TS for the connection.
*
* @param TimeLeft the time
*/
void CConnection::Timeout(int TimeLeft) {
m_Timeout = g_CurrentTime + TimeLeft;
}
/**
* SetTrafficStats
*
* Sets the traffic stats object for the connection.
*
* @param Stats the new stats object
*/
void CConnection::SetTrafficStats(CTrafficStats *Stats) {
m_Traffic = Stats;
}
/**
* GetTrafficStats
*
* Returns the traffic stats object for the connection.
*/
const CTrafficStats *CConnection::GetTrafficStats(void) const {
return m_Traffic;
}
/**
* GetClassName
*
* Returns the class' name.
*/
const char *CConnection::GetClassName(void) const {
return "CConnection";
}
/**
* FlushSendQ
*
* Flushes the sendq.
*/
void CConnection::FlushSendQ(void) {
m_SendQ->Flush();
}
/**
* IsSSL
*
* Returns whether SSL is enabled for the connection.
*/
bool CConnection::IsSSL(void) const {
#ifdef USESSL
return m_HasSSL;
#else
return false;
#endif
}
/**
* GetPeerCertificate
*
* Gets the remote side's certificate.
*/
const X509 *CConnection::GetPeerCertificate(void) const {
#ifdef USESSL
if (IsSSL()) {
return SSL_get_peer_certificate(m_SSL);
}
#endif
return NULL;
}
/**
* SSLVerify
*
* Used for verifying the peer's client certificate.
*/
int CConnection::SSLVerify(int PreVerifyOk, X509_STORE_CTX *Context) const {
return 1;
}
/**
* AsyncConnect
*
* Tries to establish a connection.
*/
void CConnection::AsyncConnect(void) {
if (m_HostAddr != NULL && (m_BindAddr != NULL || m_BindIpCache == NULL)) {
sockaddr *Remote, *Bind = NULL;
if (m_Family == AF_INET) {
sockaddr_in RemoteV4, BindV4;
memset(&RemoteV4, 0, sizeof(RemoteV4));
RemoteV4.sin_family = m_Family;
RemoteV4.sin_port = htons(m_PortCache);
RemoteV4.sin_addr.s_addr = ((in_addr *)m_HostAddr)->s_addr;
Remote = (sockaddr *)&RemoteV4;
if (m_BindAddr != NULL) {
memset(&BindV4, 0, sizeof(BindV4));
BindV4.sin_family = m_Family;
BindV4.sin_port = 0;
BindV4.sin_addr.s_addr = ((in_addr *)m_BindAddr)->s_addr;
Bind = (sockaddr *)&BindV4;
}
#ifdef IPV6
} else {
sockaddr_in6 RemoteV6, BindV6;
memset(&RemoteV6, 0, sizeof(RemoteV6));
RemoteV6.sin6_family = m_Family;
RemoteV6.sin6_port = htons(m_PortCache);
memcpy(&(RemoteV6.sin6_addr), m_HostAddr, sizeof(in6_addr));
Remote = (sockaddr *)&RemoteV6;
if (m_BindAddr != NULL) {
memset(&BindV6, 0, sizeof(BindV6));
BindV6.sin6_family = m_Family;
BindV6.sin6_port = 0;
memcpy(&(BindV6.sin6_addr), m_BindAddr, sizeof(in6_addr));
Bind = (sockaddr *)&BindV6;
}
#endif
}
m_Socket = SocketAndConnectResolved(Remote, Bind);
free(m_HostAddr);
m_HostAddr = NULL;
if (m_Socket == INVALID_SOCKET) {
int ErrorCode;
ErrorCode = safe_errno();
#ifndef _WIN32
if (ErrorCode == 0) {
ErrorCode = -1;
}
#endif
Error(ErrorCode);
m_LatchedDestruction = true;
} else {
InitSocket();
}
}
}
/**
* AsyncDnsFinished
*
* Called when the DNS query for the remote host is finished.
*
* @param Response the response
*/
void CConnection::AsyncDnsFinished(hostent *Response) {
if (Response == NULL) {
// we cannot destroy the object here as there might still be the other
// dns query (bind ip) in the queue which would get destroyed in the
// destructor; this causes a crash in the StartMainLoop() function
m_LatchedDestruction = true;
} else {
int Size;
if (Response->h_addrtype == AF_INET) {
Size = sizeof(in_addr);
} else {
Size = sizeof(in6_addr);
}
m_HostAddr = (in_addr *)malloc(Size);
CHECK_ALLOC_RESULT(m_HostAddr, malloc) {
m_LatchedDestruction = true;
return;
} CHECK_ALLOC_RESULT_END;
memcpy(m_HostAddr, Response->h_addr_list[0], Size);
AsyncConnect();
}
}
/**
* AsyncBindIpDnsFinished
*
* Called when the DNS query for the local bind address is finished.
*
* @param Response the response
*/
void CConnection::AsyncBindIpDnsFinished(hostent *Response) {
if (Response != NULL) {
int Size;
if (Response->h_addrtype == AF_INET) {
Size = sizeof(in_addr);
} else {
Size = sizeof(in6_addr);
}
m_BindAddr = (in_addr *)malloc(Size);
memcpy(m_BindAddr, Response->h_addr_list[0], Size);
}
free(m_BindIpCache);
m_BindIpCache = NULL;
AsyncConnect();
}
/**
* ShouldDestroy
*
* Called to determine whether this connection object is to be destroyed.
*/
bool CConnection::ShouldDestroy(void) const {
if (m_Timeout > 0 && m_Timeout < g_CurrentTime) {
return true;
} else {
return m_LatchedDestruction;
}
}
/**
* GetRemoteAddress
*
* Returns the remote side's address.
*/
sockaddr *CConnection::GetRemoteAddress(void) const {
static char Result[MAX_SOCKADDR_LEN];
socklen_t ResultLength = sizeof(Result);
if (m_Socket != INVALID_SOCKET && safe_getpeername(m_Socket, (sockaddr *)Result, &ResultLength) == 0) {
return (sockaddr *)Result;
} else {
return NULL;
}
}
/**
* GetLocalAddress
*
* Returns the local side's address.
*/
sockaddr *CConnection::GetLocalAddress(void) const {
static char Result[MAX_SOCKADDR_LEN];
socklen_t ResultLength = sizeof(Result);
if (m_Socket != INVALID_SOCKET && safe_getsockname(m_Socket, (sockaddr *)Result, &ResultLength) == 0) {
return (sockaddr *)Result;
} else {
return NULL;
}
}
/**
* IsConnected
*
* Checks whether the connection object is connected.
*/
bool CConnection::IsConnected(void) {
return m_Connected;
}
/**
* GetInboundRate
*
* Gets the current inbound rate (in bytes per second).
*/
size_t CConnection::GetInboundRate(void) {
if (g_CurrentTime - m_InboundTrafficReset > 0) {
return m_InboundTraffic / (g_CurrentTime - m_InboundTrafficReset);
} else {
return 0;
}
}
/**
* SetSendQ
*
* Deletes the current send queue and sets a new queue.
*
* @param Buffer the new queue
*/
void CConnection::SetSendQ(CFIFOBuffer *Buffer) {
if (m_SendQ != NULL) {
delete m_SendQ;
}
m_SendQ = Buffer;
if (m_SendQ == NULL) {
m_SendQ = new CFIFOBuffer();
}
}
/**
* SetRecvQ
*
* Deletes the current receive queue and sets a new queue.
*
* @param Buffer the new queue
*/
void CConnection::SetRecvQ(CFIFOBuffer *Buffer) {
if (m_RecvQ != NULL) {
delete m_RecvQ;
}
m_RecvQ = Buffer;
if (m_RecvQ == NULL) {
m_RecvQ = new CFIFOBuffer();
}
}
/**
* SetSSL
*
* Sets the SSL object for the connection.
*
* @param SSLObject ssl object
*/
void CConnection::SetSSLObject(void *SSLObject) {
#ifdef USESSL
if (SSLObject == NULL) {
m_HasSSL = false;
} else {
m_HasSSL = true;
}
if (m_SSL != NULL) {
SSL_free(m_SSL);
}
m_SSL = (SSL *)SSLObject;
#endif
}
|