You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

andns_net.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**************************************
  2. * AUTHOR: Federico Tomassini *
  3. * Copyright (C) Federico Tomassini *
  4. * Contact effetom@gmail.com *
  5. ***********************************************
  6. ******* BEGIN 4/2006 ********
  7. *************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, *
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  17. * GNU General Public License for more details. *
  18. * *
  19. ************************************************************************/
  20. #include "log.h"
  21. #include "andns_net.h"
  22. int idp_inet_ntop(int family,struct sockaddr *addr,char *buf,int buflen)
  23. {
  24. const char *res;
  25. struct sockaddr_in *saddr;
  26. struct sockaddr_in6 *saddr6;
  27. switch(family) {
  28. case AF_INET:
  29. saddr=(struct sockaddr_in*)addr;
  30. res=inet_ntop(family,(void*)(&(saddr->sin_addr)),buf,buflen);
  31. break;
  32. case AF_INET6:
  33. saddr6=(struct sockaddr_in6*)addr;
  34. res=inet_ntop(family,(void*)(&(saddr6->sin6_addr)),buf,buflen);
  35. break;
  36. default:
  37. return -1;
  38. }
  39. if (!res)
  40. return -1;
  41. return 0;
  42. }
  43. /* Connection Layer */
  44. int w_socket(int family,int type, int proto,int die)
  45. {
  46. int sk;
  47. sk=socket(family,type,proto);
  48. if (sk==-1) {
  49. if (die)
  50. fatal("w_socket: %s.",strerror(errno));
  51. debug(DBG_NORMAL,"w_socket: %s.",strerror(errno));
  52. return -1;
  53. }
  54. return sk;
  55. }
  56. int w_connect(struct addrinfo *ai,int die)
  57. {
  58. int sk,res;
  59. sk=w_socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol,die);
  60. res=connect(sk,ai->ai_addr,ai->ai_addrlen);
  61. if (!res)
  62. return sk;
  63. if (die)
  64. fatal("Unable to connect: %s.", strerror(errno));
  65. debug(DBG_NORMAL,"w_connect: %s.",strerror(errno));
  66. close(sk);
  67. return -1;
  68. }
  69. int serial_connect(struct addrinfo *ai,int die)
  70. {
  71. int res;
  72. struct addrinfo *temp;
  73. temp=ai;
  74. if (!temp) {
  75. if (die)
  76. fatal("Unable to connect: no host specified.");
  77. debug(DBG_NORMAL,"serial_connect: no host specified.");
  78. return -1;
  79. }
  80. do {
  81. res=w_connect(temp,0);
  82. temp=temp->ai_next;
  83. } while (res==-1 && temp);
  84. if (res==-1) {
  85. if (die)
  86. fatal("Unable to connect.");
  87. debug(DBG_NORMAL,"serial_connect: unable to connect.");
  88. return -1;
  89. }
  90. return res;
  91. }
  92. /*
  93. * host_connect returns a connected socket to (host,port)
  94. * endpoint. It is protocol independent.
  95. * -1 on error.
  96. */
  97. int host_connect(const char *host,uint16_t port,int type,int die)
  98. {
  99. int res;
  100. char portstr[6];
  101. struct addrinfo *ai,filter;
  102. memset(&filter,0,sizeof(struct addrinfo));
  103. filter.ai_socktype=type;
  104. if (!host)
  105. fatal("w_connect: malicious call.");
  106. memset(portstr,0,6);
  107. res=snprintf(portstr,6,"%d",port);
  108. if (res<0 || res>=6) {
  109. printf("Depousceve\n");
  110. return -1;
  111. }
  112. res=getaddrinfo(host,portstr,&filter,&ai);
  113. if (res!=0) {
  114. printf("w_connect: error %s.\n",gai_strerror(errno));
  115. return -1;
  116. }
  117. res=serial_connect(ai,die);
  118. freeaddrinfo(ai);
  119. return res;
  120. }
  121. int ai_connect(struct addrinfo *ai,int die,int free_ai)
  122. {
  123. int res;
  124. res=serial_connect(ai,die);
  125. if (free_ai)
  126. freeaddrinfo(ai);
  127. return res;
  128. }
  129. /* Communication Layer */
  130. ssize_t w_send(int sk,const void *buf,size_t len,int die)
  131. {
  132. ssize_t ret;
  133. ret=send(sk,buf,len,0);
  134. if (ret!=len) {
  135. if (die)
  136. fatal("Unable to send(): %s.",strerror(errno));
  137. debug(DBG_NORMAL,"w_send(): %s.",strerror(errno));
  138. }
  139. return ret;
  140. }
  141. ssize_t w_recv(int sk,void *buf,size_t len,int die)
  142. {
  143. ssize_t ret;
  144. ret=recv(sk,buf,len,0);
  145. if (ret<=0) {
  146. if (die)
  147. fatal("Unable to recv(): %s.",strerror(errno));
  148. debug(DBG_INSANE,"w_recv(): %s.",strerror(errno));
  149. }
  150. return ret;
  151. }
  152. /*
  153. * These two functions and the MACRO are
  154. * almost VERBATIM copied from inet.c and inet.h.
  155. * Functions by AlpT, Andrea Lo Pumo.
  156. */
  157. #define MILLISEC_TO_TV(x,t) \
  158. do{ \
  159. (t).tv_sec=(x)/1000; \
  160. (t).tv_usec=((x) - ((x)/1000)*1000)*1000; \
  161. }while(0)
  162. ssize_t w_send_timeout(int s,const void *buf,size_t len,int die,int timeout)
  163. {
  164. struct timeval timeout_t;
  165. fd_set fdset;
  166. int ret;
  167. MILLISEC_TO_TV(timeout*1000, timeout_t);
  168. FD_ZERO(&fdset);
  169. FD_SET(s, &fdset);
  170. ret = select(s+1, &fdset, NULL, NULL, &timeout_t);
  171. if (ret == -1) {
  172. if (die)
  173. fatal("send(): select error.");
  174. debug(DBG_NORMAL,"send(): select error.");
  175. return ret;
  176. }
  177. if(FD_ISSET(s, &fdset))
  178. return w_send(s, buf, len, die);
  179. return -1;
  180. }
  181. ssize_t w_recv_timeout(int s,void *buf,size_t len,int die,int timeout)
  182. {
  183. struct timeval timeout_t;
  184. fd_set fdset;
  185. int ret;
  186. MILLISEC_TO_TV(timeout*1000, timeout_t);
  187. FD_ZERO(&fdset);
  188. FD_SET(s, &fdset);
  189. ret = select(s+1, NULL, &fdset, NULL, &timeout_t);
  190. if (ret == -1) {
  191. if (die)
  192. fatal("recv(): select error.");
  193. debug(DBG_NORMAL,"recv(): select error.");
  194. return ret;
  195. }
  196. if(FD_ISSET(s, &fdset))
  197. return w_recv(s, buf, len, die);
  198. return -1;
  199. }
  200. /* Dialog Layer */
  201. /* "Botta e risposta" */
  202. ssize_t hn_send_recv_close(const char *host,uint16_t port,int type,void *buf,
  203. size_t buflen,void *anbuf,size_t anlen,int die,int timeout)
  204. {
  205. ssize_t ret;
  206. int res;
  207. res=host_connect(host,port,type,die);
  208. if (res==-1)
  209. return -1;
  210. if (timeout)
  211. ret=w_send_timeout(res,buf,buflen,die,timeout);
  212. else
  213. ret=w_send(res,buf,buflen,die);
  214. if (ret==-1)
  215. return -2;
  216. if (timeout)
  217. ret=w_recv_timeout(res,anbuf,anlen,die,timeout);
  218. else
  219. ret=w_recv(res,anbuf,anlen,die);
  220. if (ret==-1)
  221. return -3;
  222. close(res);
  223. return ret;
  224. }
  225. /* "Botta e risposta" */
  226. ssize_t ai_send_recv_close(struct addrinfo *ai,void *buf,size_t buflen,
  227. void *anbuf,size_t anlen,int die,int free_ai,int timeout)
  228. {
  229. ssize_t ret;
  230. int res;
  231. res=ai_connect(ai,die,free_ai);
  232. if (res==-1)
  233. return -1;
  234. if (timeout)
  235. ret=w_send_timeout(res,buf,buflen,die,timeout);
  236. else
  237. ret=w_send(res,buf,buflen,die);
  238. if (ret==-1)
  239. return -2;
  240. if (timeout)
  241. ret=w_recv_timeout(res,anbuf,anlen,die,timeout);
  242. else
  243. ret=w_recv(res,anbuf,anlen,die);
  244. if (ret==-1)
  245. return -3;
  246. close(res);
  247. return ret;
  248. }
  249. void char_print(char *buf, int len)
  250. {
  251. int i,count=0;
  252. printf("Printing %d bytes\n",len);
  253. for (i=0;i<len;i++) {
  254. printf("%02X ", (unsigned char)(buf[i]));
  255. count++;
  256. if ((count%16)==0) printf("\n");
  257. }
  258. printf("\n");
  259. return;
  260. }