socket.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include "common.h"
  21. #include "socket.h"
  22. #include "adns.h"
  23. #include "net.h"
  24. #include "egg_timer.h"
  25. #include <fcntl.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <arpa/inet.h>
  29. #ifdef no
  30. static interval_t resolve_timeout = 10; /* hostname/address lookup timeout */
  31. #endif
  32. int is_dotted_ip(const char *ip)
  33. {
  34. char buf[512];
  35. #ifdef USE_IPV6
  36. if (inet_pton(AF_INET6, ip, buf) > 0)
  37. return AF_INET6;
  38. #endif
  39. if (inet_pton(AF_INET, ip, buf) > 0)
  40. return AF_INET;
  41. return(0);
  42. }
  43. typedef struct connect_info {
  44. int dns_id;
  45. int timer_id;
  46. int idx;
  47. int port;
  48. } connect_info_t;
  49. #ifdef NO
  50. /* If a connection times out, due to dns timeout or connect timeout. */
  51. static int egg_connect_timeout(void *client_data)
  52. {
  53. connect_info_t *connect_info = (connect_info_t *) client_data;
  54. int idx, dns_id;
  55. idx = connect_info->idx;
  56. dns_id = connect_info->dns_id;
  57. connect_info->timer_id = -1;
  58. if (dns_id != -1) {
  59. /* dns_cancel will call connect_host_resolved for us, which
  60. * will filter up a "dns failed" error. */
  61. egg_dns_cancel(dns_id, 1);
  62. }
  63. // else {
  64. // detach(client_data, idx);
  65. // sockbuf_on_eof(idx, EGGNET_LEVEL, -1, "Connect timed out");
  66. // }
  67. return(0);
  68. }
  69. static connect_info_t *attach(int idx, const char *host, int port, interval_t timeout)
  70. {
  71. connect_info_t *connect_info = (connect_info_t *) my_calloc(1, sizeof(*connect_info));
  72. connect_info->port = port;
  73. connect_info->idx = idx;
  74. connect_info->dns_id = -1;
  75. connect_info->timer_id = -1;
  76. // sockbuf_attach_filter(connect_info->idx, &net_connect_filter, connect_info);
  77. if (timeout == 0) timeout = resolve_timeout;
  78. if (timeout > 0) {
  79. char buf[128];
  80. egg_timeval_t howlong;
  81. simple_snprintf(buf, sizeof(buf), "idx %d to %s/%d", idx, host, port);
  82. howlong.sec = timeout;
  83. howlong.usec = 0;
  84. connect_info->timer_id = timer_create_complex(&howlong, buf,
  85. (Function) egg_connect_timeout, connect_info, 0);
  86. }
  87. return(connect_info);
  88. }
  89. static int detach(void *client_data, int idx)
  90. {
  91. connect_info_t *connect_info = (connect_info_t *) client_data;
  92. if (connect_info->timer_id != -1) timer_destroy(connect_info->timer_id);
  93. // sockbuf_detach_filter(idx, &net_connect_filter, NULL);
  94. free(connect_info);
  95. return(0);
  96. }
  97. #endif
  98. /*
  99. int egg_client(int idx, const char *host, int port, const char *vip, int vport, int timeout)
  100. {
  101. connect_info_t *connect_info;
  102. // If they don't have their own idx (-1), create one.
  103. if (idx < 0) idx = sockbuf_new();
  104. // Resolve the hostname.
  105. connect_info = attach(idx, host, port, timeout);
  106. connect_info->dns_id = egg_dns_lookup(host, -1, connect_host_resolved, connect_info);
  107. return(idx);
  108. }
  109. */
  110. int socket_name(sockname_t *name, const char *ipaddr, int port)
  111. {
  112. bzero(name, sizeof(*name));
  113. if (inet_pton(AF_INET, ipaddr, &name->u.ipv4.sin_addr) > 0) {
  114. name->len = sizeof(name->u.ipv4);
  115. name->family = PF_INET;
  116. name->u.ipv4.sin_port = htons(port);
  117. name->u.ipv4.sin_family = AF_INET;
  118. return(0);
  119. }
  120. #ifdef USE_IPV6
  121. if (inet_pton(AF_INET6, ipaddr, &name->u.ipv6.sin6_addr) > 0) {
  122. name->len = sizeof(name->u.ipv6);
  123. name->family = PF_INET6;
  124. name->u.ipv6.sin6_port = htons(port);
  125. name->u.ipv6.sin6_family = AF_INET6;
  126. return(0);
  127. }
  128. #endif
  129. /* Invalid name? Then use passive. */
  130. name->len = sizeof(name->u.ipv4);
  131. name->family = PF_INET;
  132. name->u.ipv4.sin_port = htons(port);
  133. name->u.ipv4.sin_family = AF_INET;
  134. return(0);
  135. }
  136. int socket_set_nonblock(int sock, int value)
  137. {
  138. int oldflags = fcntl(sock, F_GETFL, 0);
  139. if (oldflags == -1) return -1;
  140. if (value != 0) oldflags |= O_NONBLOCK;
  141. else oldflags &= ~O_NONBLOCK;
  142. return fcntl(sock, F_SETFL, oldflags);
  143. }
  144. int socket_create(const char *dest_ip, int dest_port, const char *src_ip, int src_port, int flags)
  145. {
  146. char *passive[] = {"::", "0.0.0.0"};
  147. int sock = -1, pfamily, try_ok;
  148. sockname_t dest_name, src_name;
  149. /* If no source ip address is given, try :: and 0.0.0.0 (passive). */
  150. for (try_ok = 0; try_ok < 2; try_ok++) {
  151. /* Resolve the ip addresses. */
  152. socket_name(&dest_name, dest_ip ? dest_ip : passive[try_ok], dest_port);
  153. socket_name(&src_name, src_ip ? src_ip : passive[try_ok], src_port);
  154. if (src_ip || src_port) flags |= SOCKET_BIND;
  155. if (flags & SOCKET_CLIENT) pfamily = dest_name.family;
  156. else if (flags & SOCKET_SERVER) pfamily = src_name.family;
  157. else {
  158. errno = EADDRNOTAVAIL;
  159. return(-1);
  160. }
  161. /* Create the socket. */
  162. if (flags & SOCKET_UDP) sock = socket(pfamily, SOCK_DGRAM, 0);
  163. else sock = socket(pfamily, SOCK_STREAM, 0);
  164. if (sock >= 0) break;
  165. }
  166. if (sock < 0) return(-2);
  167. allocsock(sock, 0);
  168. if (flags & SOCKET_NONBLOCK) socket_set_nonblock(sock, 1);
  169. /* Do the bind if necessary. */
  170. if (flags & (SOCKET_SERVER|SOCKET_BIND)) {
  171. int yes = 1;
  172. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
  173. if (bind(sock, &src_name.u.addr, src_name.len) != 0) {
  174. killsock(sock);
  175. return(-3);
  176. }
  177. if (flags & SOCKET_SERVER) listen(sock, 50);
  178. }
  179. if (flags & SOCKET_CLIENT) {
  180. for (int i = 0; i < MAXSOCKS; i++) {
  181. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock)) {
  182. socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT | SOCK_PASS;
  183. socklist[i].host = strdup(dest_ip);
  184. socklist[i].port = dest_port;
  185. }
  186. }
  187. if (connect(sock, &dest_name.u.addr, dest_name.len) != 0) {
  188. if (errno != EINPROGRESS) {
  189. killsock(sock);
  190. return(-4);
  191. }
  192. }
  193. }
  194. errno = 0;
  195. /* Yay, we're done. */
  196. return(sock);
  197. }
  198. int socket_ip_to_uint(const char *ip, unsigned int *longip)
  199. {
  200. struct in_addr addr;
  201. inet_pton(AF_INET, ip, &addr);
  202. *longip = htonl(addr.s_addr);
  203. return(0);
  204. }
  205. #ifdef USE_IPV6
  206. static char hex_digits[] = {
  207. '0', '1', '2', '3', '4', '5', '6', '7',
  208. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  209. };
  210. #endif /* USE_IPV6 */
  211. /* Converts shorthand ipv6 notation (123:456::789) into long dotted-decimal
  212. * notation. 'dots' must be 16*4+1 = 128 bytes long. */
  213. int socket_ipv6_to_dots(const char *ip, char *dots)
  214. {
  215. #ifndef USE_IPV6
  216. dots[0] = 0;
  217. return(-1);
  218. #else
  219. struct in6_addr buf;
  220. char *cp = NULL;
  221. unsigned char *bytes = NULL;
  222. int i = 0;
  223. dots[0] = 0;
  224. if (inet_pton(AF_INET6, ip, &buf) <= 0) return(-1);
  225. /* bind9
  226. from lib/dns/byaddr.c
  227. */
  228. bytes = (unsigned char *) (&buf.s6_addr);
  229. cp = dots;
  230. for (i = 15; i >= 0; i--) {
  231. *cp++ = hex_digits[bytes[i] & 0x0f];
  232. *cp++ = '.';
  233. *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
  234. *cp++ = '.';
  235. }
  236. return(0);
  237. #endif
  238. }
  239. int get_addr(const char *ip, my_addr_t *addr)
  240. {
  241. if (inet_pton(AF_INET, ip, &addr->u.addr) > 0) {
  242. addr->family = AF_INET;
  243. return AF_INET;
  244. }
  245. #ifdef USE_IPV6
  246. if (inet_pton(AF_INET6, ip, &addr->u.addr6) > 0) {
  247. addr->family = AF_INET6;
  248. return AF_INET6;
  249. }
  250. #endif /* USE_IPV6 */
  251. return 0;
  252. }
  253. /* vim: set sts=4 sw=4 ts=4 noet: */