ds.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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->fd = socket(AF_INET6, SOCK_STREAM, 0);
  49. }else{
  50. h->fd = socket(AF_INET, SOCK_STREAM, 0);
  51. }
  52. int optval = 1;
  53. setsockopt(h->fd, 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->fd, (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->fd, (struct sockaddr*) &add, sizeof(add));
  67. }
  68. if(e)
  69. return clear(h);
  70. e = listen(h->fd, 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. ds createFromFile(int f){
  82. ds d = (ds)malloc(sizeof(s_ds));
  83. d->tp = file;
  84. d->fd = f;
  85. return d;
  86. }
  87. ds createFromFileName(const char *f){
  88. int fd = open(f, O_CREAT | O_RDWR);
  89. if(fd == -1){
  90. return NULL;
  91. }
  92. return createFromFile(fd);
  93. }
  94. ds createFromHandler(nethandler h){
  95. ds d = (ds)malloc(sizeof(s_ds));
  96. d->tp = sock;
  97. unsigned int s = sizeof(d->peer);
  98. d->fd = accept(h->fd, (struct sockaddr*)&(d->peer), &s);
  99. if(d->fd <= 0)
  100. return clear(d);
  101. d->ipv6 = d->peer.ss_family == AF_INET6;
  102. d->server = 1;
  103. return d;
  104. }
  105. ds createToHost(struct sockaddr *add, const int add_size, const int ipv6){
  106. ds d = (ds)malloc(sizeof(s_ds));
  107. d->tp = sock;
  108. if(ipv6){
  109. d->fd = socket(AF_INET6, SOCK_STREAM, 0);
  110. }else{
  111. d->fd = socket(AF_INET, SOCK_STREAM, 0);
  112. }
  113. if(connect(d->fd, add, add_size) < 0){
  114. int e = errno;
  115. free(d);
  116. errno = e;
  117. return NULL;
  118. }
  119. d->server = 0;
  120. return d;
  121. }
  122. ds createToIPv4Host(const unsigned long host, const int port){
  123. struct sockaddr_in add;
  124. add.sin_family = AF_INET;
  125. add.sin_port = htons(port);
  126. add.sin_addr.s_addr = host;
  127. return createToHost((struct sockaddr*) &add, sizeof(add), 0);
  128. }
  129. ds createToIPv6Host(const unsigned char host[16], const int port){
  130. struct sockaddr_in6 add;
  131. add.sin6_family = AF_INET6;
  132. add.sin6_port = htons(port);
  133. add.sin6_flowinfo = 0;
  134. copy6addr(add.sin6_addr.s6_addr, host);
  135. add.sin6_scope_id = 0;
  136. return createToHost((struct sockaddr*) &add, sizeof(add), 1);
  137. }
  138. int getPeer(ds d, unsigned long *ipv4peer, unsigned char ipv6peer[16], int *ipv6){
  139. int port = 0;
  140. struct sockaddr_storage peer;
  141. int peer_size = sizeof(peer);
  142. if(getpeername(d->fd, (struct sockaddr*)&peer, &peer_size)){
  143. return 0;
  144. }
  145. if(peer.ss_family == AF_INET){
  146. struct sockaddr_in *a = (struct sockaddr_in*)&(peer);
  147. zero6addr(ipv6peer);
  148. *ipv6 = -1;
  149. *ipv4peer = a->sin_addr.s_addr;
  150. port = a->sin_port;
  151. }else{
  152. struct sockaddr_in6 *a = (struct sockaddr_in6*)&(peer);
  153. *ipv4peer = 0;
  154. *ipv6 = 1;
  155. copy6addr(ipv6peer, a->sin6_addr.s6_addr);
  156. port = a->sin6_port;
  157. }
  158. return port;
  159. }
  160. int sendDs(ds d, const char *b, const int s){
  161. return write(d->fd, 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 recvDs(ds d, char *b, const int s){
  170. return read(d->fd, b, s);
  171. }
  172. int tlsDsRecv(tlsDs d, char *b, const int s){
  173. return SSL_read(d->s, b, s);
  174. }
  175. int stdDsRecv(char *b, const int s){
  176. return read(0, b, s);
  177. }
  178. int prepareToClose(ds d){
  179. int fd = d->fd;
  180. free(d);
  181. return fd;
  182. }
  183. ds closeTlsDs(tlsDs d){
  184. ds original = d->original;
  185. SSL_shutdown(d->s);
  186. SSL_shutdown(d->s);
  187. SSL_free(d->s);
  188. free(d);
  189. return original;
  190. }
  191. void closeHandler(nethandler h){
  192. close(h->fd);
  193. free(h);
  194. }
  195. tlsDs startSockTls(ds d, const char *cert, const char *key){
  196. loadOpenSSL();
  197. SSL_CTX * ctx = NULL;
  198. if(d->server)
  199. ctx = SSL_CTX_new(TLSv1_server_method());
  200. else
  201. ctx = SSL_CTX_new(TLSv1_client_method());
  202. if(!ctx)
  203. return NULL;
  204. SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
  205. if(cert)
  206. if(SSL_CTX_use_certificate_chain_file(ctx, cert) != 1){
  207. int f = prepareToClose(d);
  208. closeFd(f);
  209. return clear(ctx);
  210. }
  211. if(key)
  212. if(SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) != 1){
  213. int f = prepareToClose(d);
  214. closeFd(f);
  215. return clear(ctx);
  216. }
  217. tlsDs t = (tlsDs)malloc(sizeof(s_tlsDs));
  218. t->original = d;
  219. if(!(t->s = SSL_new(ctx))){
  220. int f = prepareToClose(d);
  221. closeFd(f);
  222. clear(ctx);
  223. return clear(t);
  224. }
  225. if(!SSL_set_fd(t->s, d->fd)){
  226. closeTlsDs(t);
  227. return NULL;
  228. }
  229. int retry = 1;
  230. int e;
  231. while(retry){
  232. retry = 0;
  233. if(d->server)
  234. e = SSL_accept(t->s);
  235. else
  236. e = SSL_connect(t->s);
  237. if(e <= 0){
  238. retry = 1;
  239. int erval = SSL_get_error(t->s, e);
  240. if((erval == SSL_ERROR_WANT_READ) || (erval == SSL_ERROR_WANT_WRITE)){
  241. }else{
  242. //ERR_print_errors(t->s->bbio);
  243. closeTlsDs(t);
  244. return NULL;
  245. }
  246. }
  247. }
  248. return t;
  249. }
  250. int getFd(ds d){
  251. return d->fd;
  252. }
  253. void closeFd(int fd){
  254. close(fd);
  255. }