rename rocksock_ssl.c to rocksock_openssl.c
[rofl0r-rocksock.git] / rocksock_add_proxy.c
blobd078ca6f87ab1413b03195a81e781b7274d98247
1 #undef _POSIX_C_SOURCE
2 #define _POSIX_C_SOURCE 200809L
3 #undef _GNU_SOURCE
4 #define _GNU_SOURCE
6 #include <string.h>
8 #include "rocksock.h"
9 #include "rocksock_internal.h"
11 #ifndef ROCKSOCK_FILENAME
12 #define ROCKSOCK_FILENAME __FILE__
13 #endif
15 int rocksock_add_proxy(rocksock* sock, rs_proxyType proxytype, const char* host, unsigned short port, const char* username, const char* password) {
16 rs_proxy* prx;
17 if (!sock)
18 return RS_E_NULL;
19 if(!host)
20 return rocksock_seterror(sock, RS_ET_OWN, RS_E_NULL, ROCKSOCK_FILENAME, __LINE__);
21 if(proxytype == RS_PT_SOCKS4 && (username || password))
22 return rocksock_seterror(sock, RS_ET_OWN, RS_E_SOCKS4_NOAUTH, ROCKSOCK_FILENAME, __LINE__);
23 if(proxytype == RS_PT_SOCKS5 && ((username && strlen(username) > 255) || (password && strlen(password) > 255)))
24 return rocksock_seterror(sock, RS_ET_OWN, RS_E_SOCKS5_AUTH_EXCEEDSIZE, ROCKSOCK_FILENAME, __LINE__);
25 if(!sock->proxies)
26 return rocksock_seterror(sock, RS_ET_OWN, RS_E_NO_PROXYSTORAGE, ROCKSOCK_FILENAME, __LINE__);
27 size_t l = strlen(host);
28 if(l > 255)
29 return rocksock_seterror(sock, RS_ET_OWN, RS_E_HOSTNAME_TOO_LONG, ROCKSOCK_FILENAME, __LINE__);
30 sock->lastproxy++;
31 prx = &sock->proxies[sock->lastproxy];
32 prx->hostinfo.port = port;
33 prx->proxytype = proxytype;
34 memcpy(prx->hostinfo.host, host, l+1);
35 memcpy(prx->username, username?username:"", username?strlen(username)+1:1);
36 memcpy(prx->password, password?password:"", password?strlen(password)+1:1);
37 return rocksock_seterror(sock, RS_ET_OWN, 0, NULL, 0);