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
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
17 # include <sys/types.h>
18 # include <sys/socket.h>
19 # include <netinet/in.h>
22 # define closesocket(so) close(so)
24 # define INVALID_SOCKET -1
25 # define SOCKET_ERROR -1
26 # define SUERRNO errno
28 # define socklen_t int //Stupid mac
31 # error unrecognized target
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
);
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 );
65 suSocket ( SOCKET so
= INVALID_SOCKET
) : _so(so
)
68 const suSocket
& operator = ( SOCKET so
)
70 assert ( _so
== INVALID_SOCKET
); // must Detach() or Close() existing socket first
80 if ( _so
!= INVALID_SOCKET
)
82 //suShutdownConnection ( _so ); // TODO - only valid on TCP sockets
87 operator SOCKET() const
91 SOCKET
Attach ( SOCKET so
)
93 SOCKET old
= Detach();
100 _so
= INVALID_SOCKET
;
105 // disable copy semantics
106 suSocket ( const suSocket
& );
107 const suSocket
& operator = ( const suSocket
& );
110 class suBufferedRecvSocket
: public suSocket
116 suBufferedRecvSocket ( SOCKET so
= INVALID_SOCKET
);
117 int recvUntil ( std::string
& buf
, char until
, int timeout
);
119 bool recvInStr ( char c
);
122 class SockAddrIn
: public sockaddr_in
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; }
132 #endif//__SOCKUTILS_H