There is nothing to say about this...
[reactos.git] / irc / ArchBlackmann / SockUtils.h
1 // SockUtils.h - Declarations for the Winsock utility functions module.
2 // (C) 2002-2004 Royce Mitchell III
3 // This file is under the BSD & LGPL licenses
4
5 #ifndef __SOCKUTILS_H
6 #define __SOCKUTILS_H
7
8 #include <string>
9 #ifdef WIN32
10 # include <winsock2.h>
11 # define in_addr_t u_long
12 # define SUERRNO WSAGetLastError()
13 # define EADDRINUSE WSAEADDRINUSE
14 # define ENOTSOCK WSAENOTSOCK
15 # define socklen_t int
16 #elif defined(UNIX)
17 # include <sys/types.h>
18 # include <sys/socket.h>
19 # include <netinet/in.h>
20 # include <unistd.h>
21 # include <errno.h>
22 # define closesocket(so) close(so)
23 # define SOCKET int
24 # define INVALID_SOCKET -1
25 # define SOCKET_ERROR -1
26 # define SUERRNO errno
27 # ifdef MACOSX
28 # define socklen_t int //Stupid mac
29 # endif
30 #else
31 # error unrecognized target
32 #endif
33
34 #include <assert.h>
35
36 extern bool suStartup();
37 extern SOCKET suTcpSocket();
38 extern SOCKET suUdpSocket();
39 extern bool suShutdownConnection(SOCKET sd);
40 extern in_addr_t suLookupAddress ( const char* pcHost );
41 extern bool suConnect ( SOCKET so, in_addr_t iAddress, u_short iPort );
42 extern bool suConnect ( SOCKET so, const char* szAddress, u_short iPort );
43 extern SOCKET suEstablishConnection ( in_addr_t iAddress, u_short iPort, int type );
44 extern SOCKET suEstablishConnection ( const char* szAddress, u_short iPort, int type );
45 extern bool suBroadcast ( SOCKET so, u_short port, const char* buf, int len = -1 );
46 extern int suRecv ( SOCKET so, char* buf, int buflen, int timeout );
47 extern int suRecvFrom ( SOCKET so, char* buf, int buflen, int timeout, sockaddr_in* from, socklen_t* fromlen );
48 extern bool suBind ( SOCKET so, in_addr_t iInterfaceAddress, u_short iListenPort, bool bReuseAddr = false );
49 extern bool suBind ( SOCKET so, const char* szInterfaceAddress, u_short iListenPort, bool bReuseAddr = false );
50 extern bool suEnableBroadcast ( SOCKET so, bool bEnable = true );
51 extern const char* suErrDesc ( int err );
52
53 #if defined(UNICODE) || defined(_UNICODE)
54 extern in_addr_t suLookupAddress ( const wchar_t* pcHost );
55 extern bool suBroadcast ( SOCKET so, u_short port, const wchar_t* buf, int len = -1 );
56 extern int suRecv ( SOCKET so, wchar_t* buf, int buflen, int timeout );
57 extern int suRecvFrom ( SOCKET so, wchar_t* buf, int buflen, int timeout, sockaddr_in* from, int* fromlen );
58 extern bool suBind ( SOCKET so, const wchar_t* szInterfaceAddress, u_short iListenPort, bool bReuseAddr = false );
59 #endif//UNICODE
60
61 class suSocket
62 {
63 SOCKET _so;
64 public:
65 suSocket ( SOCKET so = INVALID_SOCKET ) : _so(so)
66 {
67 }
68 const suSocket& operator = ( SOCKET so )
69 {
70 assert ( _so == INVALID_SOCKET ); // must Detach() or Close() existing socket first
71 _so = so;
72 return *this;
73 }
74 virtual ~suSocket()
75 {
76 Close();
77 }
78 void Close()
79 {
80 if ( _so != INVALID_SOCKET )
81 {
82 //suShutdownConnection ( _so ); // TODO - only valid on TCP sockets
83 closesocket ( _so );
84 _so = INVALID_SOCKET;
85 }
86 }
87 operator SOCKET() const
88 {
89 return _so;
90 }
91 SOCKET Attach ( SOCKET so )
92 {
93 SOCKET old = Detach();
94 _so = so;
95 return old;
96 }
97 SOCKET Detach()
98 {
99 SOCKET so = _so;
100 _so = INVALID_SOCKET;
101 return so;
102 }
103
104 private:
105 // disable copy semantics
106 suSocket ( const suSocket& );
107 const suSocket& operator = ( const suSocket& );
108 };
109
110 class suBufferedRecvSocket : public suSocket
111 {
112 char _buf[2048];
113 int _off;
114 int _len;
115 public:
116 suBufferedRecvSocket ( SOCKET so = INVALID_SOCKET );
117 int recvUntil ( std::string& buf, char until, int timeout );
118 void recvPending();
119 bool recvInStr ( char c );
120 };
121
122 class SockAddrIn : public sockaddr_in
123 {
124 public:
125 SockAddrIn(); // creates broadcast address
126 SockAddrIn ( const char* szAddr, u_short iPort );
127 SockAddrIn ( in_addr_t iAddr, u_short iPort );
128 operator sockaddr* () { return (sockaddr*)this; }
129 operator sockaddr_in* () { return (sockaddr_in*)this; }
130 };
131
132 #endif//__SOCKUTILS_H