win_sock.h File Reference


Detailed Description

Provides low-level implementation of Windows(TM) IP sockets (Windows(TM) only).

Definition in file win_sock.h.

#include "sock_shared.h"
#include "sock.h"

Include dependency graph for win_sock.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

s_socket sock_accept (s_socket &socket)
 Accepts client connection on listening server socket.
s_socket sock_accept6 (s_socket &socket)
 Accepts client connection on listening server socket.
s_socket sock_bind (in_addr local, unsigned short local_port, int &error_code)
 Binds server socket and start listening.
s_socket sock_bind6 (char *local, unsigned short port, int &error_code)
 Binds server socket and start listening.
void sock_close (s_socket &socket)
 Closes connected socket.
s_socket sock_connect (char *localIP, char *remote, unsigned short remote_port, int &error_code, bool async)
 Connects to remote host.
const char * sock_error (int code)
 Returns OS's error string from socket I/O error.
size_t sock_get_receive_size (s_socket &socket)
 Get size of input buffer on socket.
in_addr sock_get_remote_addr (s_socket &socket, int &error_code)
 Gets remote IPv4 address of remote host.
in_addr6_ sock_get_remote_addr6 (s_socket &socket)
 Gets remote IPv6 address of remote host.
size_t sock_read (s_socket &socket, char *data, size_t size, int &error_code, bool &connection_closed)
 Reads data from connected socket.
in_addr sock_resolve (const char *domain, char *ip_string)
in_addr6_ sock_resolve6 (char *domain)
void sock_reverse (char *address, char *dns_name, int dns_max_size)
 Resolves IPv4 address to DNS name via DNS (reverse lookup).
size_t sock_send (s_socket &socket, const char *data, size_t size, int &error_code)
void sock_set_blocking (s_socket &socket, bool non_blocking)
 Sets or unsets the non-blocking mode for the socket.
int sock_shutdown ()
 Shuts down WinSock.
int sock_startup ()
 Initializes WinSock.


Function Documentation

s_socket sock_accept s_socket socket  ) 
 

Accepts client connection on listening server socket.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
socket Socket handle of listening server socket
Returns:
Returns socket handle of client connection

Definition at line 685 of file win_sock.cpp.

References s_socket::clear(), s_socket::handle, INVALID_SOCKET, sock_create_later(), and SOCKET.

Referenced by dcc_loop(), and identd_check().

00686 {
00687     fd_set s_;
00688     FD_ZERO(&s_);
00689     FD_SET(socket.handle,&s_);
00690     timeval timeout;
00691     timeout.tv_sec=0;
00692     timeout.tv_usec=100;
00693     int num=select((int)socket.handle+1,&s_,NULL,NULL,&timeout);
00694 
00695     s_socket nul;
00696     nul.clear();
00697 
00698     if(num==0)
00699         return nul;
00700     sockaddr_in SockAddrIn;
00701     socklen_t i1=sizeof(SockAddrIn);
00702     SOCKET ec=accept(socket.handle,reinterpret_cast<sockaddr *>(&SockAddrIn),&i1);
00703 
00704     if(ec!=INVALID_SOCKET) {
00705         s_socket s;
00706         s.clear();
00707         s.handle=ec;
00708 
00709         sock_create_later(s,false);
00710         //sock_async(ec);
00711 
00712         return s;
00713     }
00714 
00715     return nul;
00716 }

Here is the call graph for this function:

s_socket sock_accept6 s_socket socket  ) 
 

Accepts client connection on listening server socket.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
socket Socket handle of listening server socket
Returns:
Returns socket handle of client connection

Definition at line 726 of file win_sock.cpp.

References s_socket::clear(), s_socket::handle, s_socket::host, INVALID_SOCKET, sock_create_later(), and SOCKET.

Referenced by dcc_loop(), and identd_check().

00727 {
00728     s_socket nul;
00729     nul.clear();
00730 #ifdef IPv6
00731     fd_set s_;
00732     FD_ZERO(&s_);
00733     FD_SET(socket.handle,&s_);
00734     timeval timeout;
00735     timeout.tv_sec=0;
00736     timeout.tv_usec=100;
00737     int num=select(socket.handle+1,&s_,NULL,NULL,&timeout);
00738 
00739     if(num==0)
00740         return nul;
00741     sockaddr_in6 SockAddrIn;
00742     socklen_t i1=sizeof(SockAddrIn);
00743     SOCKET ec=accept(socket.handle,reinterpret_cast<sockaddr *>(&SockAddrIn),&i1);
00744 
00745     if(ec!=INVALID_SOCKET) {
00746         s_socket s;
00747         s.clear();
00748         s.handle=ec;
00749 
00750         if(getnameinfo(reinterpret_cast<sockaddr *>(&SockAddrIn),sizeof(SockAddrIn),s.host,sizeof(s.host),NULL,0,NI_NUMERICHOST)!=0)
00751             s.host[0]=0;
00752 
00753         sock_create_later(s,false);
00754         //sock_async(ec);
00755 
00756         return s;
00757     }
00758 
00759 #endif
00760     return nul;
00761 }

Here is the call graph for this function:

s_socket sock_bind in_addr  local,
unsigned short  port,
int &  error_code
 

Binds server socket and start listening.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
local Local IPv4 address to bind to
port Local port to bind
error_code Returns OS's socket error
Returns:
If error_code == zero, returns server socket handle, else undefined

Definition at line 498 of file win_sock.cpp.

References s_socket::clear(), s_socket::handle, ioctlsocket, PROTOENT, SOCKET_ERROR, and WSAGetLastError.

Referenced by dcc_make_server(), dcc_make_telnet_server(), and identd_startup().

00499 {
00500     sockaddr_in SockAddrIn;
00501 
00502     SockAddrIn.sin_family=AF_INET;
00503     SockAddrIn.sin_addr=local;
00504     SockAddrIn.sin_port=htons(port);
00505     PROTOENT* PProtoEnt=getprotobyname("tcp");
00506     if(PProtoEnt==0) {
00507         int la=error_code=WSAGetLastError();
00508         if(la!=0) {
00509             s_socket nul;
00510             nul.clear();
00511             return nul;
00512         }
00513     }
00514 
00515     s_socket Socket;
00516     Socket.handle=socket(PF_INET,SOCK_STREAM,PProtoEnt->p_proto);
00517     if(Socket.handle==SOCKET_ERROR) {
00518         error_code=WSAGetLastError();
00519         s_socket nul;
00520         nul.clear();
00521         if(error_code!=0)
00522             return nul;
00523     }
00524     int ErrorCode=0;
00525 #ifndef _WIN32
00526     // on Win32 this would be harmfull, only on POSIX it makes sense,
00527     // if socket is in TIME_WAIT state; on Win32 we would never get
00528     // "address already in use" error while socket is in TIME_WAIT,
00529     // but more dangerous on Win32 is that we will never get that error even
00530     // if address/port is really busy listening after setting socket option SO_REUSEADDR.
00531     int tmp=1;
00532     ErrorCode=setsockopt(Socket.handle,SOL_SOCKET,SO_REUSEADDR,(const char*)&tmp,sizeof(tmp));
00533     if(ErrorCode!=0) {
00534         error_code=WSAGetLastError();
00535         s_socket nul;
00536         nul.clear();
00537         if(error_code!=0)
00538             return nul;
00539     }
00540 #endif
00541     ErrorCode=bind(Socket.handle,reinterpret_cast<sockaddr*>(&SockAddrIn),sizeof(SockAddrIn));
00542     if(ErrorCode!=0) {
00543         error_code=WSAGetLastError();
00544         s_socket nul;
00545         nul.clear();
00546         if(error_code!=0)
00547             return nul;
00548     }
00549     socklen_t i1=sizeof(SockAddrIn);
00550     ErrorCode=getsockname(Socket.handle,reinterpret_cast<sockaddr*>(&SockAddrIn),&i1);
00551     if(ErrorCode!=0) {
00552         error_code=WSAGetLastError();
00553         s_socket nul;
00554         nul.clear();
00555         if(error_code!=0)
00556             return nul;
00557     }
00558     ErrorCode=listen(Socket.handle,5);
00559     if(ErrorCode!=0) {
00560         error_code=WSAGetLastError();
00561         s_socket nul;
00562         nul.clear();
00563         if(error_code!=0)
00564             return nul;
00565     }
00566 #ifdef _WIN32
00567     unsigned long non_blocking=1;
00568     ErrorCode=ioctlsocket(Socket.handle,FIONBIO,&non_blocking);
00569 #else
00570     int flags=fcntl(Socket.handle,F_GETFL,0);
00571     flags|=O_NONBLOCK;
00572     fcntl(Socket.handle,F_SETFL,flags);
00573     ErrorCode=0;
00574 #endif
00575     if(ErrorCode!=0) {
00576         error_code=WSAGetLastError();
00577         s_socket nul;
00578         nul.clear();
00579         if(error_code!=0)
00580             return nul;
00581     }
00582 
00583     return Socket;
00584 }

Here is the call graph for this function:

s_socket sock_bind6 char *  local,
unsigned short  port,
int &  error_code
 

Binds server socket and start listening.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
local Local IPv6 address to bind to
port Local port to bind
error_code Returns OS's socket error
Returns:
If error_code == zero, returns server socket handle, else undefined

Definition at line 596 of file win_sock.cpp.

References s_socket::clear(), closesocket, s_socket::handle, ioctlsocket, log_socket(), sock_error(), SOCKET_ERROR, and WSAGetLastError.

Referenced by dcc_make_server(), dcc_make_telnet_server(), and identd_startup().

00597 {
00598 #ifdef IPv6
00599     struct sockaddr_in6 saGNI;
00600     char servInfo[256];
00601 
00602     addrinfo Hints;
00603     addrinfo* AddrInfo=NULL;
00604 
00605     char port_name[64];
00606     sprintf(port_name,"%u",(unsigned int)port);
00607 
00608     memset(&Hints,0,sizeof(Hints));
00609     Hints.ai_family=PF_INET6;
00610     Hints.ai_socktype=SOCK_STREAM;
00611     Hints.ai_flags=AI_NUMERICHOST /*AI_PASSIVE*/;
00612 
00613     if(getaddrinfo(local,port_name,&Hints,&AddrInfo)) {
00614         int ec=WSAGetLastError();
00615         if(ec)
00616             log_socket(ec,sock_error(ec),"Cannot resolve addres and port, while calling getaddrinfo() in file " __FILE__ " in function " __FUNC__);
00617         freeaddrinfo(AddrInfo);
00618         s_socket nul;
00619         nul.clear();
00620         return nul;
00621     }
00622 
00623     addrinfo* AI;
00624     for(AI=AddrInfo; AI; AI=AI->ai_next) {
00625         if(AI->ai_family!=AF_INET6)
00626             continue;
00627 
00628         s_socket Socket;
00629 
00630         Socket.handle=socket(AI->ai_family,AI->ai_socktype,AI->ai_protocol);
00631         if(Socket.handle==SOCKET_ERROR)
00632             continue;
00633 
00634         int ErrorCode=bind(Socket.handle,AI->ai_addr,AI->ai_addrlen);
00635         if(ErrorCode!=0) {
00636             closesocket(Socket.handle);
00637             continue;
00638         }
00639 
00640         ErrorCode=listen(Socket.handle,5);
00641         if(ErrorCode!=0) {
00642             closesocket(Socket.handle);
00643             continue;
00644         }
00645     
00646 #ifdef _WIN32
00647         unsigned long non_blocking=1;
00648         ErrorCode=ioctlsocket(Socket.handle,FIONBIO,&non_blocking);
00649 #else
00650         int flags=fcntl(Socket.handle,F_GETFL,0);
00651         flags|=O_NONBLOCK;
00652         fcntl(Socket.handle,F_SETFL,flags);
00653         ErrorCode=0;
00654 #endif
00655         if(ErrorCode!=0) {
00656             closesocket(Socket.handle);
00657             continue;
00658         }
00659 
00660         error_code=0;
00661         return Socket;
00662     }
00663 
00664     error_code=WSAGetLastError();
00665     s_socket nul;
00666     nul.clear();
00667     return nul;
00668 
00669 #else
00670     error_code=-2;
00671     s_socket nul;
00672     nul.clear();
00673     return nul;
00674 #endif
00675 }

Here is the call graph for this function:

void sock_close s_socket socket  ) 
 

Closes connected socket.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
socket Socket handle of connected socket

Definition at line 411 of file win_sock.cpp.

References closesocket, s_socket::handle, sock_erase_later(), and sock_flush_later().

Referenced by botnet_loop(), botnet_receive(), dcc_check_limit(), dcc_close_all(), dcc_close_servers(), dcc_loop(), identd4_shutdown(), identd6_shutdown(), identd_check(), identd_renew(), identd_shutdown(), identd_startup(), irc_disconnect(), irc_loop_putserv(), and logic_exec().

00412 {
00413     sock_flush_later(socket);
00414     sock_erase_later(socket);
00415     closesocket(socket.handle);
00416 }

Here is the call graph for this function:

s_socket sock_connect char *  localIP,
char *  remote,
unsigned short  remote_port,
int &  error_code,
bool  async
 

Connects to remote host.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
localIP Local IPv4 address as text to bind, if we are using IPv4 as destination, otherwise ignored
remote Remote IPv4/IPv6 address as text, or IPv4/IPv6 host name
remote_port Remote port
error_code Returns OS's socket error
async If true, it calls WSAAsyncSelect and creates cache buffers
Returns:
If error_code == zero, returns socket handle of connected socket, else undefined

Definition at line 269 of file win_sock.cpp.

References s_socket::clear(), closesocket, s_socket::handle, INVALID_SOCKET, log_socket(), PROTOENT, sock_async(), sock_create_later(), sock_erase_later(), sock_error(), sock_resolve(), SOCKET_ERROR, and WSAGetLastError.

Referenced by botnet_loop(), dcc_loop(), irc_connect(), and irc_quoted_callback().

00270 {
00271     s_socket Socket;
00272 
00273 #ifdef IPv6
00274 
00275     addrinfo Hints;
00276     addrinfo* AddrInfo=NULL;
00277 
00278     char port_name[64];
00279     sprintf(port_name,"%u",(unsigned int)remote_port);
00280 
00281     memset(&Hints,0,sizeof(Hints));
00282     Hints.ai_family=PF_UNSPEC;
00283     Hints.ai_socktype=SOCK_STREAM;
00284     if(getaddrinfo(remote,port_name,&Hints,&AddrInfo)) {
00285         int ec=WSAGetLastError();
00286         if(ec)
00287             log_socket(ec,sock_error(ec),"Cannot resolve addres and port, while calling getaddrinfo() in file " __FILE__ " in function " __FUNC__);
00288         s_socket nul;
00289         nul.clear();
00290         freeaddrinfo(AddrInfo);
00291         return nul;
00292     }
00293 
00294     //
00295     // Try each address getaddrinfo returned, until we find one to which
00296     // we can sucessfully connect.
00297     //
00298     addrinfo* AI;
00299     for(AI=AddrInfo; AI; AI=AI->ai_next) {
00300 
00301         // Open a socket with the correct address family for this address.
00302         Socket.handle=socket(AI->ai_family,AI->ai_socktype,AI->ai_protocol);
00303         if(Socket.handle==INVALID_SOCKET) {
00304             int ec=WSAGetLastError();
00305             if(ec)
00306                 log_socket(ec,sock_error(ec),"Error opening socket, while calling socket() in file " __FILE__ " in function " __FUNC__);
00307             continue;
00308         }
00309 
00310         if(async)
00311             sock_create_later(Socket,true);
00312 
00313         if(connect(Socket.handle,AI->ai_addr,AI->ai_addrlen)==SOCKET_ERROR) {
00314             sock_erase_later(Socket);
00315             closesocket(Socket.handle);
00316             int ec=WSAGetLastError();
00317             if(ec)
00318                 log_socket(ec,sock_error(ec),"Error connecting socket, while calling connect() in file " __FILE__ " in function " __FUNC__);
00319             continue;
00320         }
00321         break;
00322     }
00323 
00324     freeaddrinfo(AddrInfo);
00325 
00326     if(!AI) {
00327         sock_erase_later(Socket);
00328         s_socket nul;
00329         nul.clear();
00330         return nul;
00331     }
00332 
00333 #else
00334 
00335     sockaddr_in SockAddrIn;
00336     SockAddrIn.sin_family=AF_INET;
00337     SockAddrIn.sin_addr=sock_resolve(remote,NULL);
00338     SockAddrIn.sin_port=htons(remote_port);
00339 
00340     PROTOENT* PProtoEnt=getprotobyname("tcp");
00341 
00342     if(PProtoEnt==0) {
00343         int la=error_code=WSAGetLastError();
00344         if(la!=0) {
00345             s_socket nul;
00346             nul.clear();
00347             return nul;
00348         }
00349     }
00350 
00351     Socket.handle=socket(PF_INET,SOCK_STREAM,PProtoEnt->p_proto);
00352     if(Socket.handle==SOCKET_ERROR) {
00353         int la=error_code=WSAGetLastError();
00354         if(la!=0) {
00355             s_socket nul;
00356             nul.clear();
00357             return nul;
00358         }
00359     }
00360 
00361     sockaddr_in LocalName;
00362     LocalName.sin_family=AF_INET;
00363     LocalName.sin_port=0;
00364 #ifdef _WIN32
00365     LocalName.sin_addr.S_un.S_addr=inet_addr(localIP);
00366 #else
00367     LocalName.sin_addr.s_addr=inet_addr(localIP);
00368 #endif
00369     int ErrorCode=bind(Socket.handle,reinterpret_cast<sockaddr *>(&LocalName),sizeof(LocalName));
00370     if(ErrorCode!=0) {
00371         int la=error_code=WSAGetLastError();
00372         if(la!=0) {
00373             s_socket nul;
00374             nul.clear();
00375             return nul;
00376         }
00377     }
00378 
00379     if(async)
00380         sock_create_later(Socket,true);
00381 
00382     ErrorCode=connect(Socket.handle,reinterpret_cast<sockaddr *>(&SockAddrIn),sizeof(SockAddrIn));
00383     if(ErrorCode!=0) {
00384         int la=error_code=WSAGetLastError();
00385         if(la!=0) {
00386             sock_erase_later(Socket);
00387             closesocket(Socket.handle);
00388 
00389             s_socket nul;
00390             nul.clear();
00391             return nul;
00392         }
00393     }
00394 
00395 #endif
00396 
00397     if(async)
00398         sock_async(Socket);
00399 
00400     error_code=0;
00401     return Socket;
00402 }

Here is the call graph for this function:

const char* sock_error int  code  ) 
 

Returns OS's error string from socket I/O error.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
code Socket I/O error
Returns:
OS's error string

Definition at line 103 of file win_sock.cpp.

References WSAEWOULDBLOCK.

Referenced by botnet_loop(), botnet_send_command(), botnet_send_control(), dcc_loop(), dcc_make_server(), dcc_make_telnet_server(), identd_error_log(), irc_connect(), irc_loop_putserv(), irc_putserv_immediately(), irc_quoted_callback(), sock_bind6(), sock_connect(), sock_resolve6(), and sock_reverse().

00104 {
00105     char* result=0;
00106 #ifdef _WIN32
00107     switch(code) {
00108         case WSAEACCES:
00109             result="Permission denied"; break;
00110         case WSAEADDRINUSE:
00111             result="Address already in use"; break;
00112         case WSAEADDRNOTAVAIL:
00113             result="Cannot assign requested address"; break;
00114         case WSAEAFNOSUPPORT:
00115             result="Address family not supported by protocol family"; break;
00116         case WSAEALREADY:
00117             result="Operation already in progress"; break;
00118         case WSAECONNABORTED:
00119             result="Software caused connection abort"; break;
00120         case WSAECONNREFUSED:
00121             result="Connection refused"; break;
00122         case WSAECONNRESET:
00123             result="Connection reset by peer"; break;
00124         case WSAEDESTADDRREQ:
00125             result="Destination address required"; break;
00126         case WSAEFAULT:
00127             result="Bad address"; break;
00128         case WSAEHOSTDOWN:
00129             result="Host is down"; break;
00130         case WSAEHOSTUNREACH:
00131             result="No route to host"; break;
00132         case WSAEINPROGRESS:
00133             result="Operation now in progress"; break;
00134         case WSAEINTR:
00135             result="Interrupted function call"; break;
00136         case WSAEINVAL:
00137             result="Invalid argument"; break;
00138         case WSAEISCONN:
00139             result="Socket is already connected"; break;
00140         case WSAEMFILE:
00141             result="Too many open files"; break;
00142         case WSAEMSGSIZE:
00143             result="Message too long"; break;
00144         case WSAENETDOWN:
00145             result="Network is down"; break;
00146         case WSAENETRESET:
00147             result="Network dropped connection on reset"; break;
00148         case WSAENETUNREACH:
00149             result="Network is unreachable"; break;
00150         case WSAENOBUFS:
00151             result="No buffer space available"; break;
00152         case WSAENOPROTOOPT:
00153             result="Bad protocol option"; break;
00154         case WSAENOTCONN:
00155             result="Socket is not connected"; break;
00156         case WSAENOTSOCK:
00157             result="Socket operation on non-socket"; break;
00158         case WSAEOPNOTSUPP:
00159             result="Operation not supported"; break;
00160         case WSAEPFNOSUPPORT:
00161             result="Protocol family not supported"; break;
00162         case WSAEPROCLIM:
00163             result="Too many processes"; break;
00164         case WSAEPROTONOSUPPORT:
00165             result="Protocol not supported"; break;
00166         case WSAEPROTOTYPE:
00167             result="Protocol wrong type for socket"; break;
00168         case WSAESHUTDOWN:
00169             result="Cannot send after socket shutdown"; break;
00170         case WSAESOCKTNOSUPPORT:
00171             result="Socket type not supported"; break;
00172         case WSAETIMEDOUT:
00173             result="Connection timed out"; break;
00174         case WSATYPE_NOT_FOUND:
00175             result="Class type not found"; break;
00176         case WSAEWOULDBLOCK:
00177             result="Resource temporarily unavailable"; break;
00178         case WSAHOST_NOT_FOUND:
00179             result="Host not found"; break;
00180         case WSANOTINITIALISED:
00181             result="Successful WSAStartup not yet performed"; break;
00182         case WSANO_DATA:
00183             result="Valid name, no data record of requested type"; break;
00184         case WSANO_RECOVERY:
00185             result="This is a non-recoverable error"; break;
00186         case WSASYSCALLFAILURE:
00187             result="System call failure"; break;
00188         case WSASYSNOTREADY:
00189             result="Network subsystem is unavailable"; break;
00190         case WSATRY_AGAIN:
00191             result="Non-authoritative host not found"; break;
00192         case WSAVERNOTSUPPORTED:
00193             result="WINSOCKDLL version out of range"; break;
00194         case WSAEDISCON:
00195             result="Graceful shutdown in progress"; break;
00196 
00197         case -1:
00198             result="(Unknown error, based on select() fuction)"; break;
00199             
00200         case -2:
00201             result="(Not compiled with IPv6 support)"; break;
00202 
00203         default:
00204             result="Unknown error"; break;
00205     }
00206 #else
00207     if(code!=-2)
00208         result=strerror(code);
00209     else
00210         result="(Not compiled with IPv6 support)";
00211 #endif
00212     return(result);
00213 }

size_t sock_get_receive_size s_socket socket  ) 
 

Get size of input buffer on socket.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
socket Socket handle
Returns:
Number of bytes waiting to receive

Definition at line 459 of file sock.cpp.

References s_socket::handle, s_socket::last_io, and receive_later.

Referenced by dcc_loop().

00460 {
00461     size_t result=0;
00462 
00463     vector<s_receive_later>::iterator i1;
00464     for(i1=receive_later.begin(); i1!=receive_later.end(); i1++) {
00465         if((*i1).socket.handle==socket.handle) {
00466             if((*i1).socket.last_io<socket.last_io)
00467                 (*i1).socket.last_io=socket.last_io;
00468             else
00469                 socket.last_io=(*i1).socket.last_io;
00470 
00471             list<s_data>::iterator i2;
00472             for(i2=(*i1).data.begin(); i2!=(*i1).data.end(); i2++) {
00473                 if(!(*i2).closed /*&& !(*i2).error*/)
00474                     result+=(*i2).len;
00475             }
00476             break;
00477         }
00478     }
00479     return result;
00480 }

in_addr sock_get_remote_addr s_socket socket,
int &  error_code
 

Gets remote IPv4 address of remote host.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
socket Socket handle of connected socket
error_code Returns OS's socket error
Returns:
If error_code == zero, returns IPv4 address of remote host, else undefined

Definition at line 772 of file win_sock.cpp.

References s_socket::handle.

Referenced by dcc_loop().

00773 {
00774     sockaddr_in addr;
00775     socklen_t i2=sizeof(addr);
00776     error_code=getpeername(socket.handle,reinterpret_cast<sockaddr*>(&addr),&i2);
00777     return addr.sin_addr;
00778 }

in_addr6_ sock_get_remote_addr6 s_socket socket  ) 
 

Gets remote IPv6 address of remote host.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
socket Socket handle of connected socket
Returns:
Returns IPv6 address of remote host, or zero

Definition at line 788 of file win_sock.cpp.

References s_socket::host, and inetpton().

00789 {
00790 #ifdef IPv6
00791     char tmp[2048];
00792     strcpy(tmp,socket.host);
00793     if(strstr(tmp,"%"))
00794         *strstr(tmp,"%")=0;
00795     in_addr6_ addr;
00796     inetpton(AF_INET6,(const char*)tmp,&addr);
00797     return addr;
00798 #else
00799     in_addr6_ nul;
00800     memset(&nul,0,sizeof(nul));
00801     return nul;
00802 #endif
00803 }

Here is the call graph for this function:

size_t sock_read s_socket socket,
char *  data,
size_t  size,
int &  error_code,
bool &  connection_closed
 

Reads data from connected socket.

Author:
VooDooMan
Version:
2
Date:
2005
Parameters:
socket Socket handle of connected socket
data Buffer to be filled up to "size" bytes
size Size of buffer (maximum bytes to be read)
error_code Returns OS's socket error
connection_closed Returns true if data was successfuly read, but remote host closed the connection (this is not error and read of data is working correctly)
Returns:
Returns amount of read data in bytes

Definition at line 354 of file sock.cpp.

References s_socket::handle, s_socket::last_io, and receive_later.

Referenced by botnet_loop(), botnet_receive(), dcc_loop(), identd_check(), and irc_loop_putserv().

00355 {
00356     error_code=0;
00357     connection_closed=false;
00358     {
00359         vector<s_receive_later>::iterator i1;
00360         for(i1=receive_later.begin(); i1!=receive_later.end(); i1++) {
00361             if((*i1).socket.handle==socket.handle) {
00362                 if((*i1).socket.last_io<socket.last_io)
00363                     (*i1).socket.last_io=socket.last_io;
00364                 else
00365                     socket.last_io=(*i1).socket.last_io;
00366 
00367                 if((*i1).data.begin()==(*i1).data.end() && (*i1).last_error) {
00368                     error_code=(*i1).last_error;
00369                     return 0;
00370                 }
00371 
00372                 list<s_data>::iterator i2;
00373                 connection_closed=false;
00374 again:
00375                 for(i2=(*i1).data.begin(); i2!=(*i1).data.end(); i2++) {
00376                     (*i1).last_recv=time(NULL);
00377 
00378                     error_code=0;
00379                     if((*i2).closed) {
00380                         connection_closed=true;
00381                         (*i1).data.pop_front();
00382                         goto again;
00383                     }
00384                     connection_closed=false;
00385                     /*error_code=(*i2).error;
00386                     if(error_code || connection_closed)
00387                         return 0;*/
00388 
00389                     size_t l=size;
00390                     if((*i2).len<=l) {
00391                         l=(*i2).len;
00392                         if((*i2).len)
00393                             memcpy(data,(*i2).data,l);
00394                         delete[] (*i2).data;
00395                         (*i1).data.pop_front();
00396                         error_code=0;
00397                         return l;
00398                     }
00399                     if((*i2).len>l) {
00400                         memcpy(data,(*i2).data,l);
00401                         char* buf=new char[(*i2).len-l];
00402                         memcpy(buf,&((*i2).data[l]),(*i2).len-l);
00403                         delete[] (*i2).data;
00404                         (*i2).len-=l;
00405                         (*i2).data=buf;
00406                         error_code=0;
00407                         return l;
00408                     }
00409                 }
00410                 error_code=0;
00411                 //connection_closed=false;
00412                 return 0;
00413             }
00414         }
00415     }
00416 
00417     return 0;
00418 }

in_addr sock_resolve const char *  domain,
char *  ip_string
 

Definition at line 427 of file win_sock.cpp.

References sock_get_address_type(), and WSAGetLastError.

Referenced by dcc_file_has_been_read(), dcc_send_file(), irc_await_dcc_chat(), irc_loop_process_input(), irc_quoted_callback(), irc_RPL_ENDOFWHOIS(), logic_resolve(), and sock_connect().

00428 {
00429     in_addr ip;
00430 
00431     hostent* remoteHost=NULL;
00432     unsigned int addr;
00433 
00434     // If the user input is an alpha name for the host, use gethostbyname()
00435     // If not, get host by addr (assume IPv4)
00436     int type=sock_get_address_type(domain);
00437     //if(!(domain[0]>='0' && domain[0]<='9')) {   // host address is a name
00438     if(type==0) {   // host address is a name
00439         remoteHost=gethostbyname(domain);
00440 
00441         memset(&ip,0,sizeof(ip));
00442         if(WSAGetLastError()!=0 || remoteHost==NULL) {
00443             if(WSAGetLastError()==11001) {
00444                 // Win32: host not found
00445             }
00446         } else
00447             memcpy(&ip,remoteHost->h_addr_list[0],4);
00448     } else {
00449         addr=inet_addr(domain);
00450         //remoteHost=gethostbyaddr((char *)&addr, 4, AF_INET);
00451         memcpy(&ip,&addr,4);
00452     }
00453 
00454 #ifdef _WIN32
00455     if(ip_string)
00456         sprintf(ip_string,"%u.%u.%u.%u",ip.S_un.S_un_b.s_b1,ip.S_un.S_un_b.s_b2,ip.S_un.S_un_b.s_b3,ip.S_un.S_un_b.s_b4);
00457 #else
00458     if(ip_string) {
00459         unsigned char* x=(unsigned char*)&ip.s_addr;
00460         sprintf(ip_string,"%u.%u.%u.%u",x[0],x[1],x[2],x[3]);
00461     }
00462 #endif
00463     return ip;
00464 }

Here is the call graph for this function:

in_addr6_ sock_resolve6 char *  domain  ) 
 

Definition at line 870 of file win_sock.cpp.

References inetpton(), log_socket(), sock_error(), and WSAGetLastError.

Referenced by dcc_send_file(), irc_loop_process_input(), irc_quoted_callback(), logic_check_mask(), and logic_resolve6().

00871 {
00872 #ifdef IPv6
00873     struct sockaddr_in6 saGNI;
00874     char servInfo[256];
00875 
00876     addrinfo Hints;
00877     addrinfo* AddrInfo=NULL;
00878 
00879     char port_name[64];
00880     sprintf(port_name,"%u",(unsigned int)80); // something must be set as port...
00881 
00882     memset(&Hints,0,sizeof(Hints));
00883     Hints.ai_family=PF_INET6;
00884     Hints.ai_socktype=SOCK_STREAM;
00885     Hints.ai_flags=AI_PASSIVE;
00886 
00887     if(getaddrinfo(domain,port_name,&Hints,&AddrInfo)) {
00888         int ec=WSAGetLastError();
00889         if(ec)
00890             log_socket(ec,sock_error(ec),"Cannot resolve addres and port, while calling getaddrinfo() in file " __FILE__ " in function " __FUNC__);
00891         in_addr6_ a6;
00892         memset(&a6,0,sizeof(a6));
00893         freeaddrinfo(AddrInfo);
00894         return a6;
00895     }
00896 
00897     char host[2048];
00898 
00899     addrinfo* AI;
00900     for(AI=AddrInfo; AI; AI=AI->ai_next) {
00901         if(AI->ai_family!=AF_INET6)
00902             continue;
00903 
00904         if(getnameinfo(AI->ai_addr,AI->ai_addrlen, host, sizeof(host), servInfo, sizeof(servInfo), NI_NUMERICHOST)!=0) {
00905             int ec=WSAGetLastError();
00906             if(ec)
00907                 log_socket(ec,sock_error(ec),"Cannot resolve addres and port, while calling getnameinfo() in file " __FILE__ " in function " __FUNC__);
00908             continue;
00909         }
00910         break;
00911     }
00912     if(!AI) {
00913         in_addr6_ a6;
00914         memset(&a6,0,sizeof(a6));
00915         freeaddrinfo(AddrInfo);
00916         return a6;
00917     } else {
00918         in_addr6_ a6;
00919         if(inetpton(AF_INET6,(const char*)host,&a6)!=1) {
00920             memset(&a6,0,sizeof(a6));
00921             freeaddrinfo(AddrInfo);
00922             return a6;
00923         }
00924         freeaddrinfo(AddrInfo);
00925         return a6;
00926     }
00927 #else
00928     in_addr6_ a6;
00929     memset(&a6,0,sizeof(a6));
00930     return a6;
00931 #endif
00932 }

Here is the call graph for this function:

void sock_reverse char *  address,
char *  dns_name,
int  dns_max_size
 

Resolves IPv4 address to DNS name via DNS (reverse lookup).

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
address IPv4 address as text (dotted format)
dns_name Buffer that receives DNS name (should be long enough)
dns_max_size Length of dns_name buffer

Definition at line 475 of file win_sock.cpp.

References WSAGetLastError.

00476 {
00477     hostent* remoteHost;
00478     unsigned int addr=inet_addr(address);
00479     remoteHost=gethostbyaddr((char *)&addr, 4, AF_INET);
00480 
00481     if(WSAGetLastError()==0 && remoteHost) {
00482         strncpy(dns_name,remoteHost->h_name,dns_max_size);
00483         dns_name[dns_max_size-1]=0;
00484     } else
00485         dns_name[0]=0;
00486 }

size_t sock_send s_socket socket,
const char *  data,
size_t  size,
int &  error_code
 

void sock_set_blocking s_socket socket,
bool  non_blocking
 

Sets or unsets the non-blocking mode for the socket.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
socket Socket handle
non_blocking true if to set non-blocking mode

Definition at line 977 of file win_sock.cpp.

References s_socket::handle, and ioctlsocket.

Referenced by botnet_loop(), dcc_loop(), and sock_async().

00978 {
00979     /*unsigned long non_blocking_=non_blocking?1:0;
00980     ioctlsocket(socket.handle,FIONBIO,&non_blocking_);*/
00981 #ifdef _WIN32
00982     unsigned long non_blocking_=non_blocking?1:0;
00983     ioctlsocket(socket.handle,FIONBIO,&non_blocking_);
00984 #else
00985     int flags=fcntl(socket.handle,F_GETFL,0);
00986     flags&=~O_NONBLOCK;
00987     flags|=non_blocking?O_NONBLOCK:0;
00988     fcntl(socket.handle,F_SETFL,flags);
00989 #endif
00990 }

int sock_shutdown  ) 
 

Shuts down WinSock.

Author:
VooDooMan
Version:
1
Date:
2004
Returns:
OS's socket error
Warning:
Only for Windows(TM)

Definition at line 244 of file win_sock.cpp.

References WSAGetLastError.

Referenced by main().

00245 {
00246 #ifdef _WIN32
00247     int ec=WSACleanup();
00248     int la=WSAGetLastError();
00249     if(ec==0)
00250         la=0;
00251     return la;
00252 #else
00253     return 0;
00254 #endif
00255 }

int sock_startup  ) 
 

Initializes WinSock.

Author:
VooDooMan
Version:
1
Date:
2004
Returns:
OS's socket error
Warning:
Only for Windows(TM)

Definition at line 223 of file win_sock.cpp.

References WSAGetLastError.

Referenced by main().

00224 {
00225 #ifdef _WIN32
00226     int ec=WSAStartup(WINSOCK_VERSION,&WSAData);
00227     int la=WSAGetLastError();
00228     if(ec==0)
00229         la=0;
00230     return la;
00231 #else
00232     return 0;
00233 #endif
00234 }


Generated on Sun Jul 10 05:45:14 2005 for VooDoo cIRCle by doxygen 1.4.3

Hosted by SourceForge.net Logo