17 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
*/ |
*/ |
19 |
|
|
20 |
|
#include "config.h" |
21 |
|
|
22 |
|
#ifdef HAVE_STRING_H |
23 |
|
#include <string.h> |
24 |
|
#endif |
25 |
|
|
26 |
|
#ifdef HAVE_STRINGS_H |
27 |
|
#include <strings.h> |
28 |
|
#endif |
29 |
|
|
30 |
|
#ifdef HAVE_UNISTD_H |
31 |
#include <unistd.h> |
#include <unistd.h> |
32 |
|
#endif |
33 |
|
|
|
#include "config.h" |
|
34 |
#include "net.h" |
#include "net.h" |
35 |
#include "ssl.h" |
#include "ssl.h" |
36 |
#include "monitor.h" |
#include "monitor.h" |
56 |
int port; |
int port; |
57 |
char *host; |
char *host; |
58 |
int protocol; |
int protocol; |
|
#ifdef HAVE_OPENSSL |
|
59 |
ssl_connection *ssl; |
ssl_connection *ssl; |
|
#endif |
|
60 |
}; |
}; |
61 |
|
|
62 |
|
|
101 |
return S; |
return S; |
102 |
|
|
103 |
ssl_error: |
ssl_error: |
104 |
socket_close(&S); |
socket_free(&S); |
105 |
return NULL; |
return NULL; |
106 |
|
|
107 |
} |
} |
120 |
* resources. |
* resources. |
121 |
* @param S A Socket object reference |
* @param S A Socket object reference |
122 |
*/ |
*/ |
123 |
void socket_close(Socket_T *S) { |
void socket_free(Socket_T *S) { |
124 |
|
|
125 |
ASSERT(S && *S); |
ASSERT(S && *S); |
126 |
|
|
137 |
|
|
138 |
|
|
139 |
/** |
/** |
140 |
* Write the message 'msg' to the socket |
* Writes a character string. Use this function to send text based |
141 |
|
* messages to a client. |
142 |
|
* @param s A Socket_T object |
143 |
|
* @param m A String to send to the client |
144 |
|
* @return The bytes sent or -1 if an error occured |
145 |
|
*/ |
146 |
|
int socket_print(Socket_T s, const char *m, ...) { |
147 |
|
|
148 |
|
int rv; |
149 |
|
va_list ap; |
150 |
|
char *buf= NULL; |
151 |
|
|
152 |
|
ASSERT(s); |
153 |
|
ASSERT(m); |
154 |
|
|
155 |
|
va_start(ap, m); |
156 |
|
buf= format(m, ap); |
157 |
|
va_end(ap); |
158 |
|
|
159 |
|
rv= socket_write(s, buf, strlen(buf)); |
160 |
|
free(buf); |
161 |
|
|
162 |
|
return rv; |
163 |
|
|
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
|
/** |
168 |
|
* Write size bytes from the buffer b. |
169 |
* @param s A Socket_T object |
* @param s A Socket_T object |
170 |
* @param msg The message buffer to send |
* @param b The data to be written |
171 |
* @param len The length of the message in bytes |
* @param size The size of the data in b |
172 |
* @return The bytes sent or -1 if an error occured |
* @return The bytes sent or -1 if an error occured |
173 |
*/ |
*/ |
174 |
int socket_write(Socket_T s, void *msg, int len) { |
int socket_write(Socket_T s, void *b, int size) { |
175 |
|
|
176 |
ASSERT(s); |
ASSERT(s); |
177 |
|
|
178 |
if(s->ssl) |
if(s->ssl) |
179 |
return send_ssl_socket(s->ssl, msg, len); |
return send_ssl_socket(s->ssl, b, size); |
180 |
else |
else |
181 |
return sock_send(s->fd, msg, len, 0); |
return sock_send(s->fd, b, size, 0); |
182 |
|
|
183 |
} |
} |
184 |
|
|
185 |
|
|
186 |
/** |
/** |
187 |
* Read a message from the socket. |
* Reads size bytes and stores them into the byte buffer pointed to by b. |
188 |
* @param s A Socket_T object |
* @param s A Socket_T object |
189 |
* @param buf The buffer to write the message to |
* @param b A Byte buffer |
190 |
* @param len The length of the buffer in bytes |
* @param size The size of the buffer b |
191 |
* @return The bytes read or -1 if an error occured |
* @return The bytes read or -1 if an error occured |
192 |
*/ |
*/ |
193 |
int socket_read(Socket_T s, void *buf, int len) { |
int socket_read(Socket_T s, void *b, int size) { |
194 |
|
|
195 |
ASSERT(s); |
ASSERT(s); |
196 |
|
|
197 |
if(s->ssl) |
if(s->ssl) |
198 |
return recv_ssl_socket(s->ssl, buf, len); |
return recv_ssl_socket(s->ssl, b, size); |
199 |
else |
else |
200 |
return sock_recv(s->fd, buf, len, 0); |
return sock_recv(s->fd, b, size, 0); |
201 |
|
|
202 |
} |
} |