ds.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include <malloc.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. //#include <sys/select.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <netinet/in.h>
  10. #include <errno.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/err.h>
  14. #include "ds.h"
  15. int openSslLoaded = 0;
  16. void *clear(void *ptr){
  17. int e = errno;
  18. if(ptr){
  19. free(ptr);
  20. }
  21. errno = e;
  22. return NULL;
  23. }
  24. void loadOpenSSL(){
  25. if(!openSslLoaded){
  26. openSslLoaded = 1;
  27. SSL_load_error_strings();
  28. ERR_load_BIO_strings();
  29. ERR_load_crypto_strings();
  30. SSL_library_init();
  31. OpenSSL_add_all_algorithms();
  32. }
  33. }
  34. void copy6addr(unsigned char d[16], const unsigned char s[16]){
  35. int i;
  36. for(i = 0; i < 16; i++)
  37. d[i] = s[i];
  38. }
  39. void zero6addr(unsigned char d[16]){
  40. int i;
  41. for(i = 0; i < 16; i++)
  42. d[i] = 0;
  43. }
  44. nethandler getNethandler(const int ipv6, const int port){
  45. nethandler h = (nethandler)malloc(sizeof(s_nethandler));
  46. h->ipv6 = ipv6;
  47. if(ipv6){
  48. h->s = socket(AF_INET6, SOCK_STREAM, 0);
  49. }else{
  50. h->s = socket(AF_INET, SOCK_STREAM, 0);
  51. }
  52. int optval = 1;
  53. setsockopt(h->s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
  54. int e, en;
  55. if(ipv6){
  56. struct sockaddr_in6 add;
  57. add.sin6_family = AF_INET6;
  58. zero6addr(add.sin6_addr.s6_addr);
  59. add.sin6_port = htons(port);
  60. e = bind(h->s, (struct sockaddr*) &add, sizeof(add));
  61. }else{
  62. struct sockaddr_in add;
  63. add.sin_family = AF_INET;
  64. add.sin_addr.s_addr = INADDR_ANY;
  65. add.sin_port = htons(port);
  66. e = bind(h->s, (struct sockaddr*) &add, sizeof(add));
  67. }
  68. if(e)
  69. return clear(h);
  70. e = listen(h->s, DEFAULT_LISTENNING_QUEUE);
  71. if(e)
  72. return clear(h);
  73. return h;
  74. }
  75. nethandler getIPv4Port(const int port){
  76. return getNethandler(0, port);
  77. }
  78. nethandler getPort(const int port){
  79. return getNethandler(1, port);
  80. }
  81. fileDs createFromFile(int f){
  82. fileDs d = (fileDs)malloc(sizeof(s_fileDs));
  83. d->f = f;
  84. return d;
  85. }
  86. fileDs createFromFileName(const char *f){
  87. int fd = open(f, O_CREAT | O_RDWR);
  88. if(fd == -1){
  89. return NULL;
  90. }
  91. return createFromFile(fd);
  92. }
  93. sockDs createFromHandler(nethandler h){
  94. sockDs d = (sockDs)malloc(sizeof(s_sockDs));
  95. unsigned int s = sizeof(d->peer);
  96. d->s = accept(h->s, (struct sockaddr*)&(d->peer), &s);
  97. if(d->s <= 0)
  98. return clear(d);
  99. d->ipv6 = d->peer.ss_family == AF_INET6;
  100. d->server = 1;
  101. return d;
  102. }
  103. sockDs createToHost(struct sockaddr *add, const int add_size, const int ipv6){
  104. sockDs d = (sockDs)malloc(sizeof(s_sockDs));
  105. if(ipv6){
  106. d->s = socket(AF_INET6, SOCK_STREAM, 0);
  107. }else{
  108. d->s = socket(AF_INET, SOCK_STREAM, 0);
  109. }
  110. if(connect(d->s, add, add_size) < 0){
  111. int e = errno;
  112. free(d);
  113. errno = e;
  114. return NULL;
  115. }
  116. d->server = 0;
  117. return d;
  118. }
  119. sockDs createToIPv4Host(const unsigned long host, const int port){
  120. struct sockaddr_in add;
  121. add.sin_family = AF_INET;
  122. add.sin_port = htons(port);
  123. add.sin_addr.s_addr = host;
  124. return createToHost((struct sockaddr*) &add, sizeof(add), 0);
  125. }
  126. sockDs createToIPv6Host(const unsigned char host[16], const int port){
  127. struct sockaddr_in6 add;
  128. add.sin6_family = AF_INET6;
  129. add.sin6_port = htons(port);
  130. add.sin6_flowinfo = 0;
  131. copy6addr(add.sin6_addr.s6_addr, host);
  132. add.sin6_scope_id = 0;
  133. return createToHost((struct sockaddr*) &add, sizeof(add), 1);
  134. }
  135. int getPeer(sockDs d, unsigned long *ipv4peer, unsigned char ipv6peer[16], int *ipv6){
  136. int port = 0;
  137. struct sockaddr_storage peer;
  138. int peer_size = sizeof(peer);
  139. if(getpeername(d->s, (struct sockaddr*)&peer, &peer_size)){
  140. return 0;
  141. }
  142. if(peer.ss_family == AF_INET){
  143. struct sockaddr_in *a = (struct sockaddr_in*)&(peer);
  144. zero6addr(ipv6peer);
  145. *ipv6 = -1;
  146. *ipv4peer = a->sin_addr.s_addr;
  147. port = a->sin_port;
  148. }else{
  149. struct sockaddr_in6 *a = (struct sockaddr_in6*)&(peer);
  150. *ipv4peer = 0;
  151. *ipv6 = 1;
  152. copy6addr(ipv6peer, a->sin6_addr.s6_addr);
  153. port = a->sin6_port;
  154. }
  155. return port;
  156. }
  157. int fileDsSend(fileDs d, const char *b, const int s){
  158. return write(d->f, b, s);
  159. }
  160. int sockDsSend(sockDs d, const char *b, const int s){
  161. return write(d->s, b, s);
  162. }
  163. int tlsDsSend(tlsDs d, const char *b, const int s){
  164. return SSL_write(d->s, b, s);
  165. }
  166. int stdDsSend(const char *b, const int s){
  167. return write(1, b, s);
  168. }
  169. int fileDsRecv(fileDs d, char *b, const int s){
  170. return read(d->f, b, s);
  171. }
  172. int sockDsRecv(sockDs d, char *b, const int s){
  173. return read(d->s, b, s);
  174. }
  175. int tlsDsRecv(tlsDs d, char *b, const int s){
  176. return SSL_read(d->s, b, s);
  177. }
  178. int stdDsRecv(char *b, const int s){
  179. return read(0, b, s);
  180. }
  181. void closeFileDs(fileDs d){
  182. close(d->f);
  183. free(d);
  184. }
  185. void closeSockDs(sockDs d){
  186. close(d->s);
  187. free(d);
  188. }
  189. void closeTlsDs(tlsDs d){
  190. SSL_shutdown(d->s);
  191. SSL_shutdown(d->s);
  192. SSL_free(d->s);
  193. switch(d->tp){
  194. case file:
  195. closeFileDs(d->original);
  196. break;
  197. case sock:
  198. closeSockDs(d->original);
  199. break;
  200. }
  201. free(d);
  202. }
  203. void closeHandler(nethandler h){
  204. close(h->s);
  205. free(h);
  206. }
  207. tlsDs startSockTls(sockDs d, const char *cert, const char *key){
  208. loadOpenSSL();
  209. SSL_CTX * ctx = NULL;
  210. if(d->server)
  211. ctx = SSL_CTX_new(TLSv1_server_method());
  212. else
  213. ctx = SSL_CTX_new(TLSv1_client_method());
  214. if(!ctx)
  215. return NULL;
  216. SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
  217. if(cert)
  218. if(SSL_CTX_use_certificate_chain_file(ctx, cert) != 1){
  219. closeSockDs(d);
  220. return clear(ctx);
  221. }
  222. if(key)
  223. if(SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) != 1){
  224. closeSockDs(d);
  225. return clear(ctx);
  226. }
  227. tlsDs t = (tlsDs)malloc(sizeof(s_tlsDs));
  228. t->original = d;
  229. if(!(t->s = SSL_new(ctx))){
  230. closeSockDs(d);
  231. clear(ctx);
  232. return clear(t);
  233. }
  234. if(!SSL_set_fd(t->s, d->s)){
  235. closeTlsDs(t);
  236. return NULL;
  237. }
  238. printf("Starting handshake\n");
  239. int retry = 1;
  240. int e;
  241. while(retry){
  242. retry = 0;
  243. if(d->server)
  244. e = SSL_accept(t->s);
  245. else
  246. e = SSL_connect(t->s);
  247. if(e <= 0){
  248. retry = 1;
  249. int erval = SSL_get_error(t->s, e);
  250. if((erval == SSL_ERROR_WANT_READ) || (erval == SSL_ERROR_WANT_WRITE)){
  251. }else{
  252. printf("Error\n");
  253. ERR_print_errors(t->s->bbio);
  254. closeTlsDs(t);
  255. return NULL;
  256. }
  257. }
  258. }
  259. printf("Success\n");
  260. return t;
  261. }