91 |
*/ |
*/ |
92 |
Socket_T socket_new(const char *host, int port, int type, int use_ssl) { |
Socket_T socket_new(const char *host, int port, int type, int use_ssl) { |
93 |
|
|
94 |
int s; |
return socket_create_t(host, port, type, use_ssl, NET_TIMEOUT); |
|
Socket_T S; |
|
|
int proto= type==SOCKET_UDP?SOCK_DGRAM:SOCK_STREAM; |
|
|
|
|
|
ASSERT(host); |
|
|
ASSERT((type==SOCKET_UDP)||(type==SOCKET_TCP)); |
|
|
if(use_ssl) { |
|
|
ASSERT(type==SOCKET_TCP); |
|
|
} |
|
|
|
|
|
if((s= create_socket(host, port, proto, NET_TIMEOUT)) != -1) { |
|
|
|
|
|
NEW(S); |
|
|
S->socket= s; |
|
|
S->port= port; |
|
|
S->type= proto; |
|
|
S->host= xstrdup(host); |
|
|
S->timeout= NET_TIMEOUT; |
|
|
S->connection_type= TYPE_LOCAL; |
|
|
|
|
|
if(use_ssl) { |
|
|
if(! (S->ssl= new_ssl_connection(NULL, SSL_VERSION_AUTO))) { |
|
|
goto ssl_error; |
|
|
} |
|
|
if(! embed_ssl_socket(S->ssl, S->socket)) { |
|
|
goto ssl_error; |
|
|
} |
|
|
} |
|
|
|
|
|
return S; |
|
|
|
|
|
ssl_error: |
|
|
socket_free(&S); |
|
|
return NULL; |
|
|
|
|
|
} |
|
|
|
|
|
return NULL; |
|
95 |
|
|
96 |
} |
} |
97 |
|
|
151 |
return NULL; |
return NULL; |
152 |
|
|
153 |
} |
} |
154 |
|
|
155 |
|
|
156 |
|
/** |
157 |
|
* Create a new Socket opened against host:port with an explicit |
158 |
|
* timeout value for connect and read. Otherwise, same as socket_new() |
159 |
|
* @param host The remote host to open the Socket against. The host |
160 |
|
* may be a hostname found in the DNS or an IP address string. |
161 |
|
* @param port The port number to connect to |
162 |
|
* @param type The socket type to use (SOCKET_TCP or SOCKET_UPD) |
163 |
|
* @param use_ssl if TRUE the socket is created supporting SSL |
164 |
|
* @param timeout The timeout value in seconds |
165 |
|
* @return The connected Socket or NULL if an error occurred |
166 |
|
*/ |
167 |
|
Socket_T socket_create_t(const char *host, int port, int type, int use_ssl, |
168 |
|
int timeout) { |
169 |
|
|
170 |
|
int s; |
171 |
|
Socket_T S; |
172 |
|
int proto= type==SOCKET_UDP?SOCK_DGRAM:SOCK_STREAM; |
173 |
|
|
174 |
|
ASSERT(host); |
175 |
|
ASSERT((type==SOCKET_UDP)||(type==SOCKET_TCP)); |
176 |
|
if(use_ssl) { |
177 |
|
ASSERT(type==SOCKET_TCP); |
178 |
|
} |
179 |
|
ASSERT(timeout>0); |
180 |
|
|
181 |
|
if((s= create_socket(host, port, proto, timeout)) != -1) { |
182 |
|
|
183 |
|
NEW(S); |
184 |
|
S->socket= s; |
185 |
|
S->port= port; |
186 |
|
S->type= proto; |
187 |
|
S->timeout= timeout; |
188 |
|
S->host= xstrdup(host); |
189 |
|
S->connection_type= TYPE_LOCAL; |
190 |
|
|
191 |
|
if(use_ssl) { |
192 |
|
if(! (S->ssl= new_ssl_connection(NULL, SSL_VERSION_AUTO))) { |
193 |
|
goto ssl_error; |
194 |
|
} |
195 |
|
if(! embed_ssl_socket(S->ssl, S->socket)) { |
196 |
|
goto ssl_error; |
197 |
|
} |
198 |
|
} |
199 |
|
|
200 |
|
return S; |
201 |
|
|
202 |
|
ssl_error: |
203 |
|
socket_free(&S); |
204 |
|
return NULL; |
205 |
|
|
206 |
|
} |
207 |
|
|
208 |
|
return NULL; |
209 |
|
|
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
/** |
/** |