57 |
|
|
58 |
/* ------------------------------------------------------------- Definitions */ |
/* ------------------------------------------------------------- Definitions */ |
59 |
|
|
60 |
|
#define TYPE_LOCAL 0 |
61 |
|
#define TYPE_ACCEPT 1 |
62 |
|
|
63 |
struct Socket_T { |
struct Socket_T { |
64 |
int port; |
int port; |
67 |
char *host; |
char *host; |
68 |
Port_T Port; |
Port_T Port; |
69 |
int timeout; |
int timeout; |
70 |
|
int connection_type; |
71 |
ssl_connection *ssl; |
ssl_connection *ssl; |
72 |
|
ssl_server_connection *sslserver; |
73 |
}; |
}; |
74 |
|
|
75 |
|
|
109 |
S->type= proto; |
S->type= proto; |
110 |
S->host= xstrdup(host); |
S->host= xstrdup(host); |
111 |
S->timeout= NET_TIMEOUT; |
S->timeout= NET_TIMEOUT; |
112 |
|
S->connection_type= TYPE_LOCAL; |
113 |
|
|
114 |
if(use_ssl) { |
if(use_ssl) { |
115 |
if(! (S->ssl= new_ssl_connection(NULL, SSL_VERSION_AUTO))) { |
if(! (S->ssl= new_ssl_connection(NULL, SSL_VERSION_AUTO))) { |
153 |
S->type= p->type; |
S->type= p->type; |
154 |
S->port= p->port; |
S->port= p->port; |
155 |
S->timeout= p->timeout; |
S->timeout= p->timeout; |
156 |
|
S->connection_type= TYPE_LOCAL; |
157 |
|
|
158 |
if(p->family==AF_UNIX) { |
if(p->family==AF_UNIX) { |
159 |
S->host= xstrdup(LOCALHOST); |
S->host= xstrdup(LOCALHOST); |
160 |
} else { |
} else { |
191 |
|
|
192 |
|
|
193 |
/** |
/** |
194 |
|
* Factory method for creating a Socket object from an accepted |
195 |
|
* socket. The given socket must be a socket created from accept(2). |
196 |
|
* If the sslserver context is non-null the socket will support |
197 |
|
* ssl. This method does only support TCP sockets. |
198 |
|
* @param socket The accepted socket |
199 |
|
* @param remote_host The remote host from where the socket connection |
200 |
|
* originated |
201 |
|
* @param port The localhost port number from where the connection |
202 |
|
* arrived. |
203 |
|
* @param sslserver A ssl server connection context, may be NULL |
204 |
|
* @return A Socket or NULL if an error occurred |
205 |
|
*/ |
206 |
|
Socket_T socket_create_a(int socket, const char *remote_host, |
207 |
|
int port, void *sslserver) { |
208 |
|
|
209 |
|
Socket_T S; |
210 |
|
|
211 |
|
ASSERT(socket>=0); |
212 |
|
ASSERT(remote_host); |
213 |
|
|
214 |
|
NEW(S); |
215 |
|
S->port= port; |
216 |
|
S->socket= socket; |
217 |
|
S->type= SOCK_STREAM; |
218 |
|
S->timeout= NET_TIMEOUT; |
219 |
|
S->host= xstrdup(remote_host); |
220 |
|
S->connection_type= TYPE_ACCEPT; |
221 |
|
|
222 |
|
if(sslserver) { |
223 |
|
S->sslserver= sslserver; |
224 |
|
if(! (S->ssl= insert_accepted_ssl_socket(S->sslserver))) { |
225 |
|
goto ssl_error; |
226 |
|
} |
227 |
|
if(! embed_accepted_ssl_socket(S->ssl, S->socket)) { |
228 |
|
goto ssl_error; |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
return S; |
233 |
|
|
234 |
|
ssl_error: |
235 |
|
socket_free(&S); |
236 |
|
return NULL; |
237 |
|
|
238 |
|
} |
239 |
|
|
240 |
|
|
241 |
|
/** |
242 |
* Destroy a Socket object. Close the socket and release allocated |
* Destroy a Socket object. Close the socket and release allocated |
243 |
* resources. |
* resources. |
244 |
* @param S A Socket object reference |
* @param S A Socket object reference |
249 |
|
|
250 |
#ifdef HAVE_OPENSSL |
#ifdef HAVE_OPENSSL |
251 |
if((*S)->ssl && (*S)->ssl->handler) { |
if((*S)->ssl && (*S)->ssl->handler) { |
252 |
close_ssl_socket((*S)->ssl); |
if((*S)->connection_type==TYPE_LOCAL) { |
253 |
delete_ssl_socket((*S)->ssl); |
close_ssl_socket((*S)->ssl); |
254 |
|
delete_ssl_socket((*S)->ssl); |
255 |
|
} else if((*S)->connection_type==TYPE_ACCEPT && (*S)->sslserver) { |
256 |
|
close_accepted_ssl_socket((*S)->sslserver, (*S)->ssl); |
257 |
|
} |
258 |
} |
} |
259 |
#endif |
#endif |
260 |
|
|
333 |
|
|
334 |
|
|
335 |
/** |
/** |
336 |
* Get the remote host name this socket is connected to |
* Get the remote host this socket is connected to. The host is either |
337 |
|
* a host name in DNS or an IP address string. |
338 |
* @param S A Socket object |
* @param S A Socket object |
339 |
* @return The remote hostname |
* @return The remote host |
340 |
*/ |
*/ |
341 |
const char *socket_get_remote_hostname(Socket_T S) { |
const char *socket_get_remote_host(Socket_T S) { |
342 |
|
|
343 |
ASSERT(S); |
ASSERT(S); |
344 |
|
|
360 |
int socket_print(Socket_T S, const char *m, ...) { |
int socket_print(Socket_T S, const char *m, ...) { |
361 |
|
|
362 |
int n; |
int n; |
363 |
|
long l; |
364 |
va_list ap; |
va_list ap; |
365 |
char *buf= NULL; |
char *buf= NULL; |
366 |
|
|
368 |
ASSERT(m); |
ASSERT(m); |
369 |
|
|
370 |
va_start(ap, m); |
va_start(ap, m); |
371 |
buf= format(m, ap); |
buf= format(m, ap, &l); |
372 |
va_end(ap); |
va_end(ap); |
373 |
|
|
374 |
n= socket_write(S, buf, strlen(buf)); |
n= socket_write(S, buf, strlen(buf)); |
478 |
|
|
479 |
/* |
/* |
480 |
* TODO: We should use an internal buffer and read a large chunk |
* TODO: We should use an internal buffer and read a large chunk |
481 |
* from the socket not just one by one byte, which is hopeless |
* from the socket not just one by one byte, this is horribly |
482 |
* ineffective. |
* ineffective. |
483 |
*/ |
*/ |
484 |
while(--size && ((socket_read(S, &c, 1)) > 0)) { |
while(--size && ((socket_read(S, &c, 1)) > 0)) { |