1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/******************************************************************
* $Id: nanohttp-socket.h,v 1.38 2006/12/11 08:13:19 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Email: ayaz@jprogrammer.net
******************************************************************/
#ifndef __nanohttp_socket_h
#define __nanohttp_socket_h
/** @defgroup socket_errors Socket errors
*
*/
/*@{*/
/**
*
* Generic socket error
*
*/
#define HSOCKET_ERROR 1000
#define HSOCKET_ERROR_CREATE (HSOCKET_ERROR + 1)
#define HSOCKET_ERROR_GET_HOSTNAME (HSOCKET_ERROR + 2)
#define HSOCKET_ERROR_CONNECT (HSOCKET_ERROR + 3)
#define HSOCKET_ERROR_SEND (HSOCKET_ERROR + 4)
#define HSOCKET_ERROR_RECEIVE (HSOCKET_ERROR + 5)
#define HSOCKET_ERROR_BIND (HSOCKET_ERROR + 6)
#define HSOCKET_ERROR_LISTEN (HSOCKET_ERROR + 7)
#define HSOCKET_ERROR_ACCEPT (HSOCKET_ERROR + 8)
#define HSOCKET_ERROR_NOT_INITIALIZED (HSOCKET_ERROR + 9)
#define HSOCKET_ERROR_IOCTL (HSOCKET_ERROR + 10)
/*@}*/
/**
*
* @todo move the next two items to nanohttp-ssl.h
*
*/
#define HSOCKET_ERROR_SSLCLOSE (HSOCKET_ERROR + 11)
#define HSOCKET_ERROR_SSLCTX (HSOCKET_ERROR + 11)
/*}*/
/**
*
* Socket definition
*
*/
struct hsocket_t
{
#ifdef WIN32
SOCKET sock;
#else
int sock;
#endif
struct sockaddr_in addr;
size_t bytes_transmitted;
size_t bytes_received;
void *ssl;
};
#ifdef __cplusplus
extern "C"
{
#endif
/**
*
* Initializes the socket modul. This should be called only once for an
* application.
*
* @return This function should always return H_OK.
*
*/
extern herror_t hsocket_module_init(int argc, char **argv);
/**
*
* Destroys the socket modul. This should be called after finishing an
* application.
*
* @see hssl_module_destroy
*
*/
extern void hsocket_module_destroy(void);
/**
*
* Initializes a given socket object. This function (or hsokcet_init_ssl) should
* be called for every socket before using it.
*
* @param sock the destination socket to initialize.
*
* @return This function should always return H_OK.
*
* @see hssl_module_init
*
*/
extern herror_t hsocket_init(struct hsocket_t * sock);
/**
*
* Destroys and releases a given socket.
*
* @param sock the socket to destroy
*
*/
extern void hsocket_free(struct hsocket_t * sock);
/**
*
* Connects to a given host. The hostname can be an IP number or a humen
* readable hostname.
*
* @param sock the destonation socket object to use
* @param host hostname
* @param port port number to connect to
* @param ssl whether to open a SSL connection
*
* @return H_OK if success. One of the followings if fails:
* - HSOCKET_ERROR_CREATE
* - HSOCKET_ERROR_GET_HOSTNAME
* - HSOCKET_ERROR_CONNECT
*
*/
extern herror_t hsocket_open(struct hsocket_t *sock, const char *host, int port, int ssl);
/**
e
* Close a socket connection.
*
* @param sock the socket to close
*
*/
extern void hsocket_close(struct hsocket_t *sock);
/**
*
* Binds a socket to a given port number. After bind you can call
* hsocket_listen() to listen to the port.
*
* @param sock socket to use.
* @param port port number to bind to
*
* @return H_OK on success. One of the followings if fails:
* - HSOCKET_ERROR_CREATE
* - HSOCKET_ERROR_BIND
*
* @see hsocket_listen
*
*/
extern herror_t hsocket_bind(struct hsocket_t *sock, unsigned short port);
/**
*
* Set the socket to the listen mode. You must bind the socket to a port with
* hsocket_bind() before you can listen to the port.
*
* @param sock the socket to use
*
* @return H_OK if success. One of the followings if fails:<
* - HSOCKET_ERROR_NOT_INITIALIZED
* - HSOCKET_ERROR_LISTEN
*
*/
extern herror_t hsocket_listen(struct hsocket_t *sock);
/**
*
* Accepts an incoming socket request. Note that this function
* will not return until a socket connection is ready.
*
* @param sock the socket which listents to a port
* @param dest the destination socket which will be created
*
* @return H_OK if success. One of the followings if fails:<P>
* - HSOCKET_ERROR_NOT_INITIALIZED
* - HSOCKET_ERROR_ACCEPT
*/
extern herror_t hsocket_accept(struct hsocket_t *sock, struct hsocket_t *dest);
/**
*
* Sends data throught the socket.
*
* @param sock the socket to use to send the data
* @param bytes bytes to send
* @param size size of memory to sent pointed by bytes.
*
* @return H_OK if success. One of the followings if fails:
* - HSOCKET_ERROR_NOT_INITIALIZED
* - HSOCKET_ERROR_SEND
*/
extern herror_t hsocket_send(struct hsocket_t *sock, const unsigned char *bytes, int size);
/**
*
* Sends a string throught the socket
*
* @param sock the socket to use to send the data
* @param str the null terminated string to sent
*
* @return H_OK on success. One of the followings if fails:
* - HSOCKET_ERROR_NOT_INITIALIZED
* - HSOCKET_ERROR_SEND
*
*/
extern herror_t hsocket_send_string(struct hsocket_t *sock, const char *str);
extern int hsocket_select_recv(int sock, char *buf, size_t len);
/**
*
* Reads data from the socket.
*
* @param sock the socket to read data from
* @param buffer the buffer to use to save the readed bytes
* @param size the maximum size of bytes to read
* @param force if force is 1 then hsocket_read() will wait until maximum size
* of bytes (size parameter) was readed. Otherwise this function will not
* wait and will return with the bytes quequed on the socket.
*
* @return This function will return -1 if an read error was occured. Otherwise
* the return value is the size of bytes readed from the socket.
*
*/
extern herror_t hsocket_recv(struct hsocket_t * sock, unsigned char *buffer, int size, int force, int *len);
/**
*
* Get the socket read/write timeout.
*
* @return The socket timeout in seconds.
*
* @see hsocket_set_timeout
*
*/
extern int hsocket_get_timeout(void);
/**
*
* Set the socket read/write timeout.
*
* @param secs Timeout in seconds.
*
* @see hsocket_get_timeout
*
*/
extern void hsocket_set_timeout(int secs);
#ifdef __cplusplus
}
#endif
#endif
|