23 |
#include <string.h> |
#include <string.h> |
24 |
#endif |
#endif |
25 |
|
|
26 |
|
#ifdef HAVE_SYS_SOCKET_H |
27 |
|
#include <sys/socket.h> |
28 |
|
#endif |
29 |
|
|
30 |
#ifdef HAVE_STRINGS_H |
#ifdef HAVE_STRINGS_H |
31 |
#include <strings.h> |
#include <strings.h> |
32 |
#endif |
#endif |
56 |
|
|
57 |
|
|
58 |
struct Socket_T { |
struct Socket_T { |
|
int fd; |
|
59 |
int port; |
int port; |
60 |
|
int type; |
61 |
|
int socket; |
62 |
char *host; |
char *host; |
|
int protocol; |
|
63 |
ssl_connection *ssl; |
ssl_connection *ssl; |
64 |
}; |
}; |
65 |
|
|
69 |
|
|
70 |
/** |
/** |
71 |
* Create a new Socket opened against host:port. The returned Socket |
* Create a new Socket opened against host:port. The returned Socket |
72 |
* is a connected socket. If the use_ssl parameter is TRUE the socket |
* is a connected socket. This method can be used to create either TCP |
73 |
* is created using SSL. |
* or UDP sockets and the type parameter is used to select the socket |
74 |
|
* type. If the use_ssl parameter is TRUE the socket is created using |
75 |
|
* SSL. Only TCP sockets may use SSL. |
76 |
* @param host The remote host to open the Socket against. The host |
* @param host The remote host to open the Socket against. The host |
77 |
* may be a hostname found in the DNS or an IP address string. |
* may be a hostname found in the DNS or an IP address string. |
78 |
* @param port The port number to connect to |
* @param port The port number to connect to |
79 |
* @param protocol The socket protocol to use (SOCK_STREAM or SOCK_DGRAM) |
* @param type The socket type to use (SOCKET_TCP or SOCKET_UPD) |
80 |
* @param use_ssl if TRUE the socket is created supporting SSL |
* @param use_ssl if TRUE the socket is created supporting SSL |
81 |
* @return The connected Socket or NULL if an error occurred |
* @return The connected Socket or NULL if an error occurred |
82 |
*/ |
*/ |
83 |
Socket_T socket_new(char *host, int port, int protocol, int use_ssl) { |
Socket_T socket_new(char *host, int port, int type, int use_ssl) { |
84 |
|
|
85 |
int s; |
int s; |
86 |
Socket_T S; |
Socket_T S; |
87 |
|
int proto= type==SOCKET_UDP?SOCK_DGRAM:SOCK_STREAM; |
88 |
|
|
89 |
ASSERT(host); |
ASSERT(host); |
90 |
|
ASSERT((type==SOCKET_UDP)||(type==SOCKET_TCP)); |
91 |
|
if(use_ssl) { |
92 |
|
ASSERT(type==SOCKET_TCP); |
93 |
|
} |
94 |
|
|
95 |
if((s= create_socket(host, port, protocol)) != -1) { |
if((s= create_socket(host, port, proto)) != -1) { |
96 |
|
|
97 |
NEW(S); |
NEW(S); |
98 |
S->fd= s; |
S->socket= s; |
99 |
S->port= port; |
S->port= port; |
100 |
S->protocol= protocol; |
S->type= type; |
101 |
S->host= xstrdup(host); |
S->host= xstrdup(host); |
102 |
|
|
103 |
if(use_ssl) { |
if(use_ssl) { |
104 |
|
|
105 |
if(! (S->ssl= new_ssl_connection(Run.httpsslpem, SSL_VERSION_AUTO))) { |
if(! (S->ssl= new_ssl_connection(NULL, SSL_VERSION_AUTO))) { |
106 |
goto ssl_error; |
goto ssl_error; |
107 |
} |
} |
108 |
|
|
109 |
if(!embed_ssl_socket(S->ssl, S->fd)) { |
if(! embed_ssl_socket(S->ssl, S->socket)) { |
110 |
goto ssl_error; |
goto ssl_error; |
111 |
} |
} |
112 |
|
|
128 |
|
|
129 |
|
|
130 |
/** |
/** |
131 |
|
* Factory method for creating a new Socket from a monit Port object |
132 |
|
* @param port The port object to create a socket from |
133 |
|
* @return The connected Socket or NULL if an error occurred |
134 |
|
*/ |
135 |
|
Socket_T socket_create(void *port) { |
136 |
|
|
137 |
|
int s; |
138 |
|
Socket_T S; |
139 |
|
Port_T p= port; |
140 |
|
|
141 |
|
ASSERT(port); |
142 |
|
|
143 |
|
if((s= create_generic_socket(p)) != -1) { |
144 |
|
|
145 |
|
NEW(S); |
146 |
|
S->socket= s; |
147 |
|
S->type= p->type; |
148 |
|
S->port= p->port; |
149 |
|
S->host= xstrdup(p->hostname); |
150 |
|
|
151 |
|
if(p->SSL.use_ssl) { |
152 |
|
if(! (S->ssl= new_ssl_connection(NULL, p->SSL.version))) { |
153 |
|
goto ssl_error; |
154 |
|
} |
155 |
|
if(! embed_ssl_socket(S->ssl, S->socket)) { |
156 |
|
goto ssl_error; |
157 |
|
} |
158 |
|
if(p->SSL.certmd5) { |
159 |
|
if(! check_ssl_md5sum(S->ssl, p->SSL.certmd5)) { |
160 |
|
goto ssl_error; |
161 |
|
} |
162 |
|
} |
163 |
|
} |
164 |
|
|
165 |
|
return S; |
166 |
|
|
167 |
|
ssl_error: |
168 |
|
socket_free(&S); |
169 |
|
return NULL; |
170 |
|
|
171 |
|
} |
172 |
|
|
173 |
|
return NULL; |
174 |
|
|
175 |
|
} |
176 |
|
|
177 |
|
/** |
178 |
* Destroy a Socket object. Close the socket and release allocated |
* Destroy a Socket object. Close the socket and release allocated |
179 |
* resources. |
* resources. |
180 |
* @param S A Socket object reference |
* @param S A Socket object reference |
187 |
close_ssl_socket((*S)->ssl); |
close_ssl_socket((*S)->ssl); |
188 |
delete_ssl_socket((*S)->ssl); |
delete_ssl_socket((*S)->ssl); |
189 |
} |
} |
190 |
close((*S)->fd); |
close((*S)->socket); |
191 |
free((*S)->host); |
free((*S)->host); |
192 |
free(*S); |
free(*S); |
193 |
(*S)= NULL; |
(*S)= NULL; |
195 |
} |
} |
196 |
|
|
197 |
|
|
198 |
|
/* -------------------------------------------------------------- Properties */ |
199 |
|
|
200 |
|
|
201 |
|
/** |
202 |
|
* Returns TRUE if the socket is ready for i|o |
203 |
|
* @param S A Socket object reference |
204 |
|
* @return TRUE if the socket is ready otherwise FALSE |
205 |
|
*/ |
206 |
|
int socket_is_ready(Socket_T s) { |
207 |
|
|
208 |
|
ASSERT(s); |
209 |
|
|
210 |
|
switch(s->type) { |
211 |
|
case SOCK_STREAM: return check_socket(s->socket); |
212 |
|
case SOCK_DGRAM: return check_udp_socket(s->socket); |
213 |
|
default: break; |
214 |
|
} |
215 |
|
|
216 |
|
return FALSE; |
217 |
|
|
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
/** |
222 |
|
* Get the underlying socket descriptor |
223 |
|
* @param s A Socket object |
224 |
|
* @return The socket descriptor |
225 |
|
*/ |
226 |
|
int socket_get_socket(Socket_T s) { |
227 |
|
|
228 |
|
ASSERT(s); |
229 |
|
|
230 |
|
return s->socket; |
231 |
|
|
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
/** |
236 |
|
* Get the remote port number the socket is connected to |
237 |
|
* @param s A Socket object |
238 |
|
* @return The remote host's port number |
239 |
|
*/ |
240 |
|
int socket_get_remote_port(Socket_T s) { |
241 |
|
|
242 |
|
ASSERT(s); |
243 |
|
|
244 |
|
return s->port; |
245 |
|
|
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
|
/** |
250 |
|
* Get the remote host name this socket is connected to |
251 |
|
* @param s A Socket object |
252 |
|
* @return The remote hostname |
253 |
|
*/ |
254 |
|
const char *Socket_get_remote_hostname(Socket_T s) { |
255 |
|
|
256 |
|
ASSERT(s); |
257 |
|
|
258 |
|
return s->host; |
259 |
|
|
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
|
/* ------------------------------------------------------------------ Public */ |
264 |
|
|
265 |
|
|
266 |
/** |
/** |
267 |
* Writes a character string. Use this function to send text based |
* Writes a character string. Use this function to send text based |
268 |
* messages to a client. |
* messages to a client. |
305 |
if(s->ssl) |
if(s->ssl) |
306 |
return send_ssl_socket(s->ssl, b, size); |
return send_ssl_socket(s->ssl, b, size); |
307 |
else |
else |
308 |
return sock_send(s->fd, b, size, 0); |
return sock_send(s->socket, b, size, 0); |
309 |
|
|
310 |
} |
} |
311 |
|
|
324 |
if(s->ssl) |
if(s->ssl) |
325 |
return recv_ssl_socket(s->ssl, b, size); |
return recv_ssl_socket(s->ssl, b, size); |
326 |
else |
else |
327 |
return sock_recv(s->fd, b, size, 0); |
return sock_recv(s->socket, b, size, 0); |
328 |
|
|
329 |
} |
} |
330 |
|
|