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
|
// Copyright (C) 2003 Davis E. King (davis@dlib.net), Miguel Grinberg
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SOCKETS_KERNEL_1_CPp_
#define DLIB_SOCKETS_KERNEL_1_CPp_
#include "../platform.h"
#ifdef WIN32
#include <winsock2.h>
#ifndef _WINSOCKAPI_
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */
#endif
#include "../windows_magic.h"
#include "sockets_kernel_1.h"
#include <windows.h>
#ifndef NI_MAXHOST
#define NI_MAXHOST 1025
#endif
// tell visual studio to link to the libraries we need if we are
// in fact using visual studio
#ifdef _MSC_VER
#pragma comment (lib, "ws2_32.lib")
#endif
#include "../assert.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
class SOCKET_container
{
/*!
This object is just a wrapper around the SOCKET type. It exists
so that we can #include the windows.h and Winsock2.h header files
in this cpp file and not at all in the header file.
!*/
public:
SOCKET_container (
SOCKET s = INVALID_SOCKET
) : val(s) {}
SOCKET val;
operator SOCKET&() { return val; }
SOCKET_container& operator= (
const SOCKET& s
) { val = s; return *this; }
bool operator== (
const SOCKET& s
) const { return s == val; }
};
// ----------------------------------------------------------------------------------------
// stuff to ensure that WSAStartup() is always called before any sockets stuff is needed
namespace sockets_kernel_1_mutex
{
mutex startup_lock;
}
class sockets_startupdown
{
public:
sockets_startupdown();
~sockets_startupdown() { WSACleanup( ); }
};
sockets_startupdown::sockets_startupdown (
)
{
WSADATA wsaData;
WSAStartup (MAKEWORD(2,0), &wsaData);
}
void sockets_startup()
{
// mutex crap to make this function thread-safe
sockets_kernel_1_mutex::startup_lock.lock();
static sockets_startupdown a;
sockets_kernel_1_mutex::startup_lock.unlock();
}
// ----------------------------------------------------------------------------------------
// lookup functions
int
get_local_hostname (
std::string& hostname
)
{
// ensure that WSAStartup has been called and WSACleanup will eventually
// be called when program ends
sockets_startup();
try
{
char temp[NI_MAXHOST];
if (gethostname(temp,NI_MAXHOST) == SOCKET_ERROR )
{
return OTHER_ERROR;
}
hostname = temp;
}
catch (...)
{
return OTHER_ERROR;
}
return 0;
}
// -----------------
int
hostname_to_ip (
const std::string& hostname,
std::string& ip,
int n
)
{
// ensure that WSAStartup has been called and WSACleanup will eventually
// be called when program ends
sockets_startup();
try
{
// lock this mutex since gethostbyname isn't really thread safe
auto_mutex M(sockets_kernel_1_mutex::startup_lock);
// if no hostname was given then return error
if ( hostname.empty())
return OTHER_ERROR;
hostent* address;
address = gethostbyname(hostname.c_str());
if (address == 0)
{
return OTHER_ERROR;
}
// find the nth address
in_addr* addr = reinterpret_cast<in_addr*>(address->h_addr_list[0]);
for (int i = 1; i <= n; ++i)
{
addr = reinterpret_cast<in_addr*>(address->h_addr_list[i]);
// if there is no nth address then return error
if (addr == 0)
return OTHER_ERROR;
}
char* resolved_ip = inet_ntoa(*addr);
// check if inet_ntoa returned an error
if (resolved_ip == NULL)
{
return OTHER_ERROR;
}
ip.assign(resolved_ip);
}
catch(...)
{
return OTHER_ERROR;
}
return 0;
}
// -----------------
int
ip_to_hostname (
const std::string& ip,
std::string& hostname
)
{
// ensure that WSAStartup has been called and WSACleanup will eventually
// be called when program ends
sockets_startup();
try
{
// lock this mutex since gethostbyaddr isn't really thread safe
auto_mutex M(sockets_kernel_1_mutex::startup_lock);
// if no ip was given then return error
if (ip.empty())
return OTHER_ERROR;
hostent* address;
unsigned long ipnum = inet_addr(ip.c_str());
// if inet_addr couldn't convert ip then return an error
if (ipnum == INADDR_NONE)
{
return OTHER_ERROR;
}
address = gethostbyaddr(reinterpret_cast<char*>(&ipnum),4,AF_INET);
// check if gethostbyaddr returned an error
if (address == 0)
{
return OTHER_ERROR;
}
hostname.assign(address->h_name);
}
catch (...)
{
return OTHER_ERROR;
}
return 0;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// connection object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
connection::
connection(
SOCKET_container sock,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port,
const std::string& local_ip
) :
user_data(0),
connection_socket(*(new SOCKET_container())),
connection_foreign_port(foreign_port),
connection_foreign_ip(foreign_ip),
connection_local_port(local_port),
connection_local_ip(local_ip),
sd(false),
sdo(false),
sdr(0)
{
connection_socket = sock;
}
// ----------------------------------------------------------------------------------------
connection::
~connection (
)
{
if (connection_socket != INVALID_SOCKET)
closesocket(connection_socket);
delete &connection_socket;
}
// ----------------------------------------------------------------------------------------
int connection::
disable_nagle()
{
int flag = 1;
int status = setsockopt( connection_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag) );
if (status == SOCKET_ERROR)
return OTHER_ERROR;
else
return 0;
}
// ----------------------------------------------------------------------------------------
long connection::
write (
const char* buf,
long num
)
{
const long old_num = num;
long status;
const long max_send_length = 1024*1024*100;
while (num > 0)
{
// Make sure to cap the max value num can take on so that if it is
// really large (it might be big on 64bit platforms) so that the OS
// can't possibly get upset about it being large.
const long length = std::min(max_send_length, num);
if ( (status = send(connection_socket,buf,length,0)) == SOCKET_ERROR)
{
if (sdo_called())
return SHUTDOWN;
else
return OTHER_ERROR;
}
num -= status;
buf += status;
}
return old_num;
}
// ----------------------------------------------------------------------------------------
long connection::
read (
char* buf,
long num
)
{
const long max_recv_length = 1024*1024*100;
// Make sure to cap the max value num can take on so that if it is
// really large (it might be big on 64bit platforms) so that the OS
// can't possibly get upset about it being large.
const long length = std::min(max_recv_length, num);
long status = recv(connection_socket,buf,length,0);
if (status == SOCKET_ERROR)
{
// if this error is the result of a shutdown call then return SHUTDOWN
if (sd_called())
return SHUTDOWN;
else
return OTHER_ERROR;
}
else if (status == 0 && sd_called())
{
return SHUTDOWN;
}
return status;
}
// ----------------------------------------------------------------------------------------
long connection::
read (
char* buf,
long num,
unsigned long timeout
)
{
if (readable(timeout) == false)
return TIMEOUT;
const long max_recv_length = 1024*1024*100;
// Make sure to cap the max value num can take on so that if it is
// really large (it might be big on 64bit platforms) so that the OS
// can't possibly get upset about it being large.
const long length = std::min(max_recv_length, num);
long status = recv(connection_socket,buf,length,0);
if (status == SOCKET_ERROR)
{
// if this error is the result of a shutdown call then return SHUTDOWN
if (sd_called())
return SHUTDOWN;
else
return OTHER_ERROR;
}
else if (status == 0 && sd_called())
{
return SHUTDOWN;
}
return status;
}
// ----------------------------------------------------------------------------------------
bool connection::
readable (
unsigned long timeout
) const
{
fd_set read_set;
// initialize read_set
FD_ZERO(&read_set);
// add the listening socket to read_set
FD_SET(connection_socket, &read_set);
// setup a timeval structure
timeval time_to_wait;
time_to_wait.tv_sec = static_cast<long>(timeout/1000);
time_to_wait.tv_usec = static_cast<long>((timeout%1000)*1000);
// wait on select
int status = select(0,&read_set,0,0,&time_to_wait);
// if select timed out or there was an error
if (status <= 0)
return false;
// data is ready to be read
return true;
}
// ----------------------------------------------------------------------------------------
int connection::
shutdown_outgoing (
)
{
sd_mutex.lock();
if (sdo || sd)
{
sd_mutex.unlock();
return sdr;
}
sdo = true;
sdr = ::shutdown(connection_socket,SD_SEND);
// convert -1 error code into the OTHER_ERROR error code
if (sdr == -1)
sdr = OTHER_ERROR;
int temp = sdr;
sd_mutex.unlock();
return temp;
}
// ----------------------------------------------------------------------------------------
int connection::
shutdown (
)
{
sd_mutex.lock();
if (sd)
{
sd_mutex.unlock();
return sdr;
}
sd = true;
SOCKET stemp = connection_socket;
connection_socket = INVALID_SOCKET;
sdr = closesocket(stemp);
// convert SOCKET_ERROR error code into the OTHER_ERROR error code
if (sdr == SOCKET_ERROR)
sdr = OTHER_ERROR;
int temp = sdr;
sd_mutex.unlock();
return temp;
}
// ----------------------------------------------------------------------------------------
connection::socket_descriptor_type connection::
get_socket_descriptor (
) const
{
return connection_socket.val;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// listener object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
listener::
listener(
SOCKET_container sock,
unsigned short port,
const std::string& ip
) :
listening_socket(*(new SOCKET_container)),
listening_port(port),
listening_ip(ip),
inaddr_any(listening_ip.empty())
{
listening_socket = sock;
}
// ----------------------------------------------------------------------------------------
listener::
~listener (
)
{
closesocket(listening_socket);
delete &listening_socket;
}
// ----------------------------------------------------------------------------------------
int listener::
accept (
std::unique_ptr<connection>& new_connection,
unsigned long timeout
)
{
new_connection.reset(0);
connection* con;
int status = this->accept(con, timeout);
if (status == 0)
new_connection.reset(con);
return status;
}
// ----------------------------------------------------------------------------------------
int listener::
accept (
connection*& new_connection,
unsigned long timeout
)
{
SOCKET incoming;
sockaddr_in incomingAddr;
int length = sizeof(sockaddr_in);
// implement timeout with select if timeout is > 0
if (timeout > 0)
{
fd_set read_set;
// initialize read_set
FD_ZERO(&read_set);
// add the listening socket to read_set
FD_SET(listening_socket, &read_set);
// setup a timeval structure
timeval time_to_wait;
time_to_wait.tv_sec = static_cast<long>(timeout/1000);
time_to_wait.tv_usec = static_cast<long>((timeout%1000)*1000);
// wait on select
int status = select(0,&read_set,0,0,&time_to_wait);
// if select timed out
if (status == 0)
return TIMEOUT;
// if select returned an error
if (status == SOCKET_ERROR)
return OTHER_ERROR;
}
// call accept to get a new connection
incoming=::accept(listening_socket,reinterpret_cast<sockaddr*>(&incomingAddr),&length);
// if there was an error return OTHER_ERROR
if ( incoming == INVALID_SOCKET )
return OTHER_ERROR;
// get the port of the foreign host into foreign_port
int foreign_port = ntohs(incomingAddr.sin_port);
// get the IP of the foreign host into foreign_ip
std::string foreign_ip;
{
char* foreign_ip_temp = inet_ntoa(incomingAddr.sin_addr);
// check if inet_ntoa() returned an error
if (foreign_ip_temp == NULL)
{
closesocket(incoming);
return OTHER_ERROR;
}
foreign_ip.assign(foreign_ip_temp);
}
// get the local ip
std::string local_ip;
if (inaddr_any == true)
{
sockaddr_in local_info;
length = sizeof(sockaddr_in);
// get the local sockaddr_in structure associated with this new connection
if ( getsockname (
incoming,
reinterpret_cast<sockaddr*>(&local_info),
&length
) == SOCKET_ERROR
)
{ // an error occurred
closesocket(incoming);
return OTHER_ERROR;
}
char* temp = inet_ntoa(local_info.sin_addr);
// check if inet_ntoa() returned an error
if (temp == NULL)
{
closesocket(incoming);
return OTHER_ERROR;
}
local_ip.assign(temp);
}
else
{
local_ip = listening_ip;
}
// set the SO_OOBINLINE option
int flag_value = 1;
if (setsockopt(incoming,SOL_SOCKET,SO_OOBINLINE,reinterpret_cast<const char*>(&flag_value),sizeof(int)) == SOCKET_ERROR )
{
closesocket(incoming);
return OTHER_ERROR;
}
// make a new connection object for this new connection
try
{
new_connection = new connection (
incoming,
foreign_port,
foreign_ip,
listening_port,
local_ip
);
}
catch (...) { closesocket(incoming); return OTHER_ERROR; }
return 0;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// socket creation functions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
int create_listener (
std::unique_ptr<listener>& new_listener,
unsigned short port,
const std::string& ip
)
{
new_listener.reset();
listener* temp;
int status = create_listener(temp,port,ip);
if (status == 0)
new_listener.reset(temp);
return status;
}
int create_listener (
listener*& new_listener,
unsigned short port,
const std::string& ip
)
{
// ensure that WSAStartup has been called and WSACleanup will eventually
// be called when program ends
sockets_startup();
sockaddr_in sa; // local socket structure
ZeroMemory(&sa,sizeof(sockaddr_in)); // initialize sa
SOCKET sock = socket (AF_INET, SOCK_STREAM, 0); // get a new socket
// if socket() returned an error then return OTHER_ERROR
if (sock == INVALID_SOCKET )
{
return OTHER_ERROR;
}
// set the local socket structure
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
if (ip.empty())
{
// if the listener should listen on any IP
sa.sin_addr.S_un.S_addr = htons(INADDR_ANY);
}
else
{
// if there is a specific ip to listen on
sa.sin_addr.S_un.S_addr = inet_addr(ip.c_str());
// if inet_addr couldn't convert the ip then return an error
if ( sa.sin_addr.S_un.S_addr == INADDR_NONE )
{
closesocket(sock);
return OTHER_ERROR;
}
}
// set the SO_REUSEADDR option
int flag_value = 1;
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,reinterpret_cast<const char*>(&flag_value),sizeof(int));
// bind the new socket to the requested port and ip
if (bind(sock,reinterpret_cast<sockaddr*>(&sa),sizeof(sockaddr_in))==SOCKET_ERROR)
{
const int err = WSAGetLastError();
// if there was an error
closesocket(sock);
// if the port is already bound then return PORTINUSE
if (err == WSAEADDRINUSE)
return PORTINUSE;
else
return OTHER_ERROR;
}
// tell the new socket to listen
if ( listen(sock,SOMAXCONN) == SOCKET_ERROR)
{
const int err = WSAGetLastError();
// if there was an error return OTHER_ERROR
closesocket(sock);
// if the port is already bound then return PORTINUSE
if (err == WSAEADDRINUSE)
return PORTINUSE;
else
return OTHER_ERROR;
}
// determine the port used if necessary
if (port == 0)
{
sockaddr_in local_info;
int length = sizeof(sockaddr_in);
if ( getsockname (
sock,
reinterpret_cast<sockaddr*>(&local_info),
&length
) == SOCKET_ERROR
)
{
closesocket(sock);
return OTHER_ERROR;
}
port = ntohs(local_info.sin_port);
}
// initialize a listener object on the heap with the new socket
try { new_listener = new listener(sock,port,ip); }
catch(...) { closesocket(sock); return OTHER_ERROR; }
return 0;
}
// ----------------------------------------------------------------------------------------
int create_connection (
std::unique_ptr<connection>& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port,
const std::string& local_ip
)
{
new_connection.reset();
connection* temp;
int status = create_connection(temp,foreign_port, foreign_ip, local_port, local_ip);
if (status == 0)
new_connection.reset(temp);
return status;
}
int create_connection (
connection*& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port,
const std::string& local_ip
)
{
// ensure that WSAStartup has been called and WSACleanup
// will eventually be called when program ends
sockets_startup();
sockaddr_in local_sa; // local socket structure
sockaddr_in foreign_sa; // foreign socket structure
ZeroMemory(&local_sa,sizeof(sockaddr_in)); // initialize local_sa
ZeroMemory(&foreign_sa,sizeof(sockaddr_in)); // initialize foreign_sa
int length;
SOCKET sock = socket (AF_INET, SOCK_STREAM, 0); // get a new socket
// if socket() returned an error then return OTHER_ERROR
if (sock == INVALID_SOCKET )
{
return OTHER_ERROR;
}
// set the foreign socket structure
foreign_sa.sin_family = AF_INET;
foreign_sa.sin_port = htons(foreign_port);
foreign_sa.sin_addr.S_un.S_addr = inet_addr(foreign_ip.c_str());
// if inet_addr couldn't convert the ip then return an error
if ( foreign_sa.sin_addr.S_un.S_addr == INADDR_NONE )
{
closesocket(sock);
return OTHER_ERROR;
}
// set up the local socket structure
local_sa.sin_family = AF_INET;
// set the local ip
if (local_ip.empty())
{
// if the listener should listen on any IP
local_sa.sin_addr.S_un.S_addr = htons(INADDR_ANY);
}
else
{
// if there is a specific ip to listen on
local_sa.sin_addr.S_un.S_addr = inet_addr(local_ip.c_str());
// if inet_addr couldn't convert the ip then return an error
if (local_sa.sin_addr.S_un.S_addr == INADDR_NONE)
{
closesocket(sock);
return OTHER_ERROR;
}
}
// set the local port
local_sa.sin_port = htons(local_port);
// bind the new socket to the requested local port and local ip
if ( bind (
sock,
reinterpret_cast<sockaddr*>(&local_sa),
sizeof(sockaddr_in)
) == SOCKET_ERROR
)
{
const int err = WSAGetLastError();
// if there was an error
closesocket(sock);
// if the port is already bound then return PORTINUSE
if (err == WSAEADDRINUSE)
return PORTINUSE;
else
return OTHER_ERROR;
}
// connect the socket
if (connect (
sock,
reinterpret_cast<sockaddr*>(&foreign_sa),
sizeof(sockaddr_in)
) == SOCKET_ERROR
)
{
const int err = WSAGetLastError();
closesocket(sock);
// if the port is already bound then return PORTINUSE
if (err == WSAEADDRINUSE)
return PORTINUSE;
else
return OTHER_ERROR;
}
// determine the local port and IP and store them in used_local_ip
// and used_local_port
int used_local_port;
std::string used_local_ip;
sockaddr_in local_info;
if (local_port == 0)
{
length = sizeof(sockaddr_in);
if (getsockname (
sock,
reinterpret_cast<sockaddr*>(&local_info),
&length
) == SOCKET_ERROR
)
{
closesocket(sock);
return OTHER_ERROR;
}
used_local_port = ntohs(local_info.sin_port);
}
else
{
used_local_port = local_port;
}
// determine real local ip
if (local_ip.empty())
{
// if local_port is not 0 then we must fill the local_info structure
if (local_port != 0)
{
length = sizeof(sockaddr_in);
if ( getsockname (
sock,
reinterpret_cast<sockaddr*>(&local_info),
&length
) == SOCKET_ERROR
)
{
closesocket(sock);
return OTHER_ERROR;
}
}
char* temp = inet_ntoa(local_info.sin_addr);
// check if inet_ntoa returned an error
if (temp == NULL)
{
closesocket(sock);
return OTHER_ERROR;
}
used_local_ip.assign(temp);
}
else
{
used_local_ip = local_ip;
}
// set the SO_OOBINLINE option
int flag_value = 1;
if (setsockopt(sock,SOL_SOCKET,SO_OOBINLINE,reinterpret_cast<const char*>(&flag_value),sizeof(int)) == SOCKET_ERROR )
{
closesocket(sock);
return OTHER_ERROR;
}
// initialize a connection object on the heap with the new socket
try
{
new_connection = new connection (
sock,
foreign_port,
foreign_ip,
used_local_port,
used_local_ip
);
}
catch(...) {closesocket(sock); return OTHER_ERROR; }
return 0;
}
// ----------------------------------------------------------------------------------------
}
#endif // WIN32
#endif // DLIB_SOCKETS_KERNEL_1_CPp_
|