1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guilhem Lavaux
5 * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer)
6 * Purpose: GSocket include file (system independent)
8 * -------------------------------------------------------------------------
14 #ifndef __GSOCKET_STANDALONE__
17 /* kludge for GTK.. gsockgtk.c craps out miserably if we include
18 defs.h ... no idea how other files get away with it.. */
20 #if !defined( __WXMSW__ ) && !defined( WXDLLIMPEXP_NET )
21 #define WXDLLIMPEXP_NET
26 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
31 Including sys/types.h under cygwin results in the warnings about "fd_set
32 having been defined in sys/types.h" when winsock.h is included later and
33 doesn't seem to be necessary anyhow. It's not needed under Mac neither.
35 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
36 #include <sys/types.h>
47 typedef struct _GSocket GSocket
;
48 typedef struct _GAddress GAddress
;
75 /* See below for an explanation on how events work.
86 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
87 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
88 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
89 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
92 typedef int GSocketEventFlags
;
94 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
98 /* Functions tables for internal use by GSocket code: */
101 struct GSocketBaseFunctionsTable
103 void (*Detected_Read
)(GSocket
*socket
);
104 void (*Detected_Write
)(GSocket
*socket
);
108 struct GSocketGUIFunctionsTable
110 int (*GUI_Init
)(void);
111 void (*GUI_Cleanup
)(void);
112 int (*GUI_Init_Socket
)(GSocket
*socket
);
113 void (*GUI_Destroy_Socket
)(GSocket
*socket
);
115 void (*Install_Callback
)(GSocket
*socket
, GSocketEvent event
);
116 void (*Uninstall_Callback
)(GSocket
*socket
, GSocketEvent event
);
118 void (*Enable_Events
)(GSocket
*socket
);
119 void (*Disable_Events
)(GSocket
*socket
);
123 /* Global initializers */
125 /* Sets GUI functions callbacks. Must be called *before* GSocket_Init
126 if the app uses async sockets. */
127 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
);
129 /* GSocket_Init() must be called at the beginning */
130 int GSocket_Init(void);
132 /* GSocket_Cleanup() must be called at the end */
133 void GSocket_Cleanup(void);
136 /* Constructors / Destructors */
138 GSocket
*GSocket_new(void);
139 void GSocket_destroy(GSocket
*socket
);
144 * Disallow further read/write operations on this socket, close
145 * the fd and disable all callbacks.
147 void GSocket_Shutdown(GSocket
*socket
);
149 /* Address handling */
155 * Set or get the local or peer address for this socket. The 'set'
156 * functions return GSOCK_NOERROR on success, an error code otherwise.
157 * The 'get' functions return a pointer to a GAddress object on success,
158 * or NULL otherwise, in which case they set the error code of the
159 * corresponding GSocket.
162 * GSOCK_INVSOCK - the socket is not valid.
163 * GSOCK_INVADDR - the address is not valid.
165 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
166 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
167 GAddress
*GSocket_GetLocal(GSocket
*socket
);
168 GAddress
*GSocket_GetPeer(GSocket
*socket
);
170 /* Server specific parts */
172 /* GSocket_SetServer:
173 * Sets up this socket as a server. The local address must have been
174 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
175 * Returns GSOCK_NOERROR on success, one of the following otherwise:
178 * GSOCK_INVSOCK - the socket is in use.
179 * GSOCK_INVADDR - the local address has not been set.
180 * GSOCK_IOERR - low-level error.
182 GSocketError
GSocket_SetServer(GSocket
*socket
);
184 /* GSocket_WaitConnection:
185 * Waits for an incoming client connection. Returns a pointer to
186 * a GSocket object, or NULL if there was an error, in which case
187 * the last error field will be updated for the calling GSocket.
189 * Error codes (set in the calling GSocket)
190 * GSOCK_INVSOCK - the socket is not valid or not a server.
191 * GSOCK_TIMEDOUT - timeout, no incoming connections.
192 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
193 * GSOCK_MEMERR - couldn't allocate memory.
194 * GSOCK_IOERR - low-level error.
196 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
199 /* Client specific parts */
202 * For stream (connection oriented) sockets, GSocket_Connect() tries
203 * to establish a client connection to a server using the peer address
204 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
205 * connection has been succesfully established, or one of the error
206 * codes listed below. Note that for nonblocking sockets, a return
207 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
208 * request can be completed later; you should use GSocket_Select()
209 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
210 * corresponding asynchronous events.
212 * For datagram (non connection oriented) sockets, GSocket_Connect()
213 * just sets the peer address established with GSocket_SetPeer() as
214 * default destination.
217 * GSOCK_INVSOCK - the socket is in use or not valid.
218 * GSOCK_INVADDR - the peer address has not been established.
219 * GSOCK_TIMEDOUT - timeout, the connection failed.
220 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
221 * GSOCK_MEMERR - couldn't allocate memory.
222 * GSOCK_IOERR - low-level error.
224 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
227 /* Datagram sockets */
229 /* GSocket_SetNonOriented:
230 * Sets up this socket as a non-connection oriented (datagram) socket.
231 * Before using this function, the local address must have been set
232 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
233 * on success, or one of the following otherwise.
236 * GSOCK_INVSOCK - the socket is in use.
237 * GSOCK_INVADDR - the local address has not been set.
238 * GSOCK_IOERR - low-level error.
240 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
245 /* Like recv(), send(), ... */
247 /* For datagram sockets, the incoming / outgoing addresses
248 * are stored as / read from the 'peer' address field.
250 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
251 int GSocket_Write(GSocket
*socket
, const char *buffer
,
255 * Polls the socket to determine its status. This function will
256 * check for the events specified in the 'flags' parameter, and
257 * it will return a mask indicating which operations can be
258 * performed. This function won't block, regardless of the
259 * mode (blocking | nonblocking) of the socket.
261 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
266 /* GSocket_SetNonBlocking:
267 * Sets the socket to non-blocking mode. All IO calls will return
270 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
272 /* GSocket_SetTimeout:
273 * Sets the timeout for blocking calls. Time is expressed in
276 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
279 * Returns the last error occured for this socket. Note that successful
280 * operations do not clear this back to GSOCK_NOERROR, so use it only
283 GSocketError WXDLLIMPEXP_NET
GSocket_GetError(GSocket
*socket
);
289 * There is data to be read in the input buffer. If, after a read
290 * operation, there is still data available, the callback function will
293 * The socket is available for writing. That is, the next write call
294 * won't block. This event is generated only once, when the connection is
295 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
296 * when the output buffer empties again. This means that the app should
297 * assume that it can write since the first OUTPUT event, and no more
298 * OUTPUT events will be generated unless an error occurs.
300 * Connection succesfully established, for client sockets, or incoming
301 * client connection, for server sockets. Wait for this event (also watch
302 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
304 * The connection is lost (or a connection request failed); this could
305 * be due to a failure, or due to the peer closing it gracefully.
308 /* GSocket_SetCallback:
309 * Enables the callbacks specified by 'flags'. Note that 'flags'
310 * may be a combination of flags OR'ed toghether, so the same
311 * callback function can be made to accept different events.
312 * The callback function must have the following prototype:
314 * void function(GSocket *socket, GSocketEvent event, char *cdata)
316 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
317 GSocketCallback fallback
, char *cdata
);
319 /* GSocket_UnsetCallback:
320 * Disables all callbacks specified by 'flags', which may be a
321 * combination of flags OR'ed toghether.
323 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
328 GAddress
*GAddress_new(void);
329 GAddress
*GAddress_copy(GAddress
*address
);
330 void GAddress_destroy(GAddress
*address
);
332 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
333 GAddressType
GAddress_GetFamily(GAddress
*address
);
335 /* The use of any of the next functions will set the address family to
336 * the specific one. For example if you use GAddress_INET_SetHostName,
337 * address family will be implicitly set to AF_INET.
340 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
341 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
342 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
343 unsigned long hostaddr
);
344 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
345 const char *protocol
);
346 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
348 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
350 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
351 unsigned short GAddress_INET_GetPort(GAddress
*address
);
353 /* TODO: Define specific parts (INET6, UNIX) */
355 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
356 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
360 #endif /* __cplusplus */
363 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
365 #endif /* __GSOCKET_H */