2
* innotek Portable Runtime - TCP/IP.
6
* Copyright (C) 2006-2007 innotek GmbH
8
* This file is part of VirtualBox Open Source Edition (OSE), as
9
* available from http://www.virtualbox.org. This file is free software;
10
* you can redistribute it and/or modify it under the terms of the GNU
11
* General Public License as published by the Free Software Foundation,
12
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13
* distribution. VirtualBox OSE is distributed in the hope that it will
14
* be useful, but WITHOUT ANY WARRANTY of any kind.
20
#include <iprt/cdefs.h>
21
#include <iprt/types.h>
22
#include <iprt/thread.h>
25
# error "There are no RTFile APIs available Ring-0 Host Context!"
31
/** @defgroup grp_rt_tcp RTTcp - TCP/IP
38
* Serve a TCP Server connection.
40
* @returns iprt status code.
41
* @returns VERR_TCP_SERVER_STOP to terminate the server loop forcing
42
* the RTTcpCreateServer() call to return.
43
* @param Sock The socket which the client is connected to.
44
* The call will close this socket.
45
* @param pvUser User argument.
47
typedef DECLCALLBACK(int) FNRTTCPSERVE(RTSOCKET Sock, void *pvUser);
48
/** Pointer to a RTTCPSERVE(). */
49
typedef FNRTTCPSERVE *PFNRTTCPSERVE;
51
/** Pointer to a RTTCPSERVER handle. */
52
typedef struct RTTCPSERVER *PRTTCPSERVER;
53
/** Pointer to a RTTCPSERVER handle. */
54
typedef PRTTCPSERVER *PPRTTCPSERVER;
57
* Create single connection at a time TCP Server in a separate thread.
59
* The thread will loop accepting connections and call pfnServe for
60
* each of the incoming connections in turn. The pfnServe function can
61
* return VERR_TCP_SERVER_STOP too terminate this loop. RTTcpServerDestroy()
62
* should be used to terminate the server.
64
* @returns iprt status code.
65
* @param pszAddress The address for creating a listening socket.
66
* If NULL or empty string the server is bound to all interfaces.
67
* @param uPort The port for creating a listening socket.
68
* @param enmType The thread type.
69
* @param pszThrdName The name of the worker thread.
70
* @param pfnServe The function which will serve a new client connection.
71
* @param pvUser User argument passed to pfnServe.
72
* @param ppServer Where to store the serverhandle.
74
RTR3DECL(int) RTTcpServerCreate(const char *pszAddress, unsigned uPort, RTTHREADTYPE enmType, const char *pszThrdName,
75
PFNRTTCPSERVE pfnServe, void *pvUser, PPRTTCPSERVER ppServer);
78
* Create single connection at a time TCP Server.
79
* The caller must call RTTcpServerListen() to actually start the server.
81
* @returns iprt status code.
82
* @param pszAddress The address for creating a listening socket.
83
* If NULL the server is bound to all interfaces.
84
* @param uPort The port for creating a listening socket.
85
* @param ppServer Where to store the serverhandle.
87
RTR3DECL(int) RTTcpServerCreateEx(const char *pszAddress, uint32_t uPort, PPRTTCPSERVER ppServer);
90
* Closes down and frees a TCP Server.
91
* This will also terminate any open connections to the server.
93
* @returns iprt status code.
94
* @param pServer Handle to the server.
96
RTR3DECL(int) RTTcpServerDestroy(PRTTCPSERVER pServer);
99
* Listen for incoming connections.
101
* The function will loop accepting connections and call pfnServe for
102
* each of the incoming connections in turn. The pfnServe function can
103
* return VERR_TCP_SERVER_STOP too terminate this loop. A stopped server
104
* can only be destroyed.
106
* @returns iprt status code.
107
* @param pServer The server handle as returned from RTTcpServerCreateEx().
108
* @param pfnServe The function which will serve a new client connection.
109
* @param pvUser User argument passed to pfnServe.
111
RTR3DECL(int) RTTcpServerListen(PRTTCPSERVER pServer, PFNRTTCPSERVE pfnServe, void *pvUser);
114
* Terminate the open connection to the server.
116
* @returns iprt status code.
117
* @param pServer Handle to the server.
119
RTR3DECL(int) RTTcpServerDisconnectClient(PRTTCPSERVER pServer);
122
* Connect (as a client) to a TCP Server.
124
* @returns iprt status code.
125
* @param pszAddress The address to connect to.
126
* @param uPort The port to connect to.
127
* @param pSock Where to store the handle to the established connection.
129
RTR3DECL(int) RTTcpClientConnect(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock);
132
* Close a socket returned by RTTcpClientConnect().
134
* @returns iprt status code.
135
* @param Sock Socket descriptor.
137
RTR3DECL(int) RTTcpClientClose(RTSOCKET Sock);
140
* Receive data from a socket.
142
* @returns iprt status code.
143
* @param Sock Socket descriptor.
144
* @param pvBuffer Where to put the data we read.
145
* @param cbBuffer Read buffer size.
146
* @param pcbRead Number of bytes read.
147
* If NULL the entire buffer will be filled upon successful return.
148
* If not NULL a partial read can be done successfully.
150
RTR3DECL(int) RTTcpRead(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
153
* Send data from a socket.
155
* @returns iprt status code.
156
* @param Sock Socket descriptor.
157
* @param pvBuffer Buffer to write data to socket.
158
* @param cbBuffer How much to write.
160
RTR3DECL(int) RTTcpWrite(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer);
163
* Flush socket write buffers.
165
* @returns iprt status code.
166
* @param Sock Socket descriptor.
168
RTR3DECL(int) RTTcpFlush(RTSOCKET Sock);
171
* Socket I/O multiplexing.
172
* Checks if the socket is ready for reading.
174
* @returns iprt status code.
175
* @param Sock Socket descriptor.
176
* @param cMillies Number of milliseconds to wait for the socket.
177
* Use RT_INDEFINITE_WAIT to wait for ever.
179
RTR3DECL(int) RTTcpSelectOne(RTSOCKET Sock, unsigned cMillies);
182
#if 0 /* skipping these for now - RTTcpServer* handles this. */
184
* Listen for connection on a socket.
186
* @returns iprt status code.
187
* @param Sock Socket descriptor.
188
* @param cBackLog The maximum length the queue of pending connections
191
RTR3DECL(int) RTTcpListen(RTSOCKET Sock, int cBackLog);
194
* Accept a connection on a socket.
196
* @returns iprt status code.
197
* @param Sock Socket descriptor.
198
* @param uPort The port for accepting connection.
199
* @param pSockAccepted Where to store the handle to the accepted connection.
201
RTR3DECL(int) RTTcpAccept(RTSOCKET Sock, unsigned uPort, PRTSOCKET pSockAccepted);