xconnect.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Connect to host at port using address resolution from getaddrinfo
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  8. */
  9. #include <netinet/in.h>
  10. #include <net/if.h>
  11. #include <sys/un.h>
  12. #include "libbb.h"
  13. void FAST_FUNC setsockopt_reuseaddr(int fd)
  14. {
  15. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
  16. }
  17. int FAST_FUNC setsockopt_broadcast(int fd)
  18. {
  19. return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
  20. }
  21. int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
  22. {
  23. int r;
  24. struct ifreq ifr;
  25. strncpy_IFNAMSIZ(ifr.ifr_name, iface);
  26. /* NB: passing (iface, strlen(iface) + 1) does not work!
  27. * (maybe it works on _some_ kernels, but not on 2.6.26)
  28. * Actually, ifr_name is at offset 0, and in practice
  29. * just giving char[IFNAMSIZ] instead of struct ifreq works too.
  30. * But just in case it's not true on some obscure arch... */
  31. r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
  32. if (r)
  33. bb_perror_msg("can't bind to interface %s", iface);
  34. return r;
  35. }
  36. len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
  37. {
  38. len_and_sockaddr lsa;
  39. len_and_sockaddr *lsa_ptr;
  40. lsa.len = LSA_SIZEOF_SA;
  41. if (getsockname(fd, &lsa.u.sa, &lsa.len) != 0)
  42. return NULL;
  43. lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
  44. if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
  45. lsa_ptr->len = lsa.len;
  46. getsockname(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
  47. } else {
  48. memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
  49. }
  50. return lsa_ptr;
  51. }
  52. void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
  53. {
  54. if (connect(s, s_addr, addrlen) < 0) {
  55. if (ENABLE_FEATURE_CLEAN_UP)
  56. close(s);
  57. if (s_addr->sa_family == AF_INET)
  58. bb_perror_msg_and_die("%s (%s)",
  59. "cannot connect to remote host",
  60. inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
  61. bb_perror_msg_and_die("cannot connect to remote host");
  62. }
  63. }
  64. /* Return port number for a service.
  65. * If "port" is a number use it as the port.
  66. * If "port" is a name it is looked up in /etc/services,
  67. * if it isnt found return default_port
  68. */
  69. unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
  70. {
  71. unsigned port_nr = default_port;
  72. if (port) {
  73. int old_errno;
  74. /* Since this is a lib function, we're not allowed to reset errno to 0.
  75. * Doing so could break an app that is deferring checking of errno. */
  76. old_errno = errno;
  77. port_nr = bb_strtou(port, NULL, 10);
  78. if (errno || port_nr > 65535) {
  79. struct servent *tserv = getservbyname(port, protocol);
  80. port_nr = default_port;
  81. if (tserv)
  82. port_nr = ntohs(tserv->s_port);
  83. }
  84. errno = old_errno;
  85. }
  86. return (uint16_t)port_nr;
  87. }
  88. /* "Old" networking API - only IPv4 */
  89. /*
  90. void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host)
  91. {
  92. struct hostent *he;
  93. memset(s_in, 0, sizeof(struct sockaddr_in));
  94. s_in->sin_family = AF_INET;
  95. he = xgethostbyname(host);
  96. memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
  97. }
  98. int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr)
  99. {
  100. int s = xsocket(AF_INET, SOCK_STREAM, 0);
  101. xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
  102. return s;
  103. }
  104. */
  105. /* "New" networking API */
  106. int FAST_FUNC get_nport(const struct sockaddr *sa)
  107. {
  108. #if ENABLE_FEATURE_IPV6
  109. if (sa->sa_family == AF_INET6) {
  110. return ((struct sockaddr_in6*)sa)->sin6_port;
  111. }
  112. #endif
  113. if (sa->sa_family == AF_INET) {
  114. return ((struct sockaddr_in*)sa)->sin_port;
  115. }
  116. /* What? UNIX socket? IPX?? :) */
  117. return -1;
  118. }
  119. void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
  120. {
  121. #if ENABLE_FEATURE_IPV6
  122. if (lsa->u.sa.sa_family == AF_INET6) {
  123. lsa->u.sin6.sin6_port = port;
  124. return;
  125. }
  126. #endif
  127. if (lsa->u.sa.sa_family == AF_INET) {
  128. lsa->u.sin.sin_port = port;
  129. return;
  130. }
  131. /* What? UNIX socket? IPX?? :) */
  132. }
  133. /* We hijack this constant to mean something else */
  134. /* It doesn't hurt because we will remove this bit anyway */
  135. #define DIE_ON_ERROR AI_CANONNAME
  136. /* host: "1.2.3.4[:port]", "www.google.com[:port]"
  137. * port: if neither of above specifies port # */
  138. static len_and_sockaddr* str2sockaddr(
  139. const char *host, int port,
  140. IF_FEATURE_IPV6(sa_family_t af,)
  141. int ai_flags)
  142. {
  143. int rc;
  144. len_and_sockaddr *r;
  145. struct addrinfo *result = NULL;
  146. struct addrinfo *used_res;
  147. const char *org_host = host; /* only for error msg */
  148. const char *cp;
  149. struct addrinfo hint;
  150. if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
  151. struct sockaddr_un *sun;
  152. r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
  153. r->len = sizeof(struct sockaddr_un);
  154. r->u.sa.sa_family = AF_UNIX;
  155. sun = (struct sockaddr_un *)&r->u.sa;
  156. safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
  157. return r;
  158. }
  159. r = NULL;
  160. /* Ugly parsing of host:addr */
  161. if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
  162. /* Even uglier parsing of [xx]:nn */
  163. host++;
  164. cp = strchr(host, ']');
  165. if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
  166. /* Malformed: must be [xx]:nn or [xx] */
  167. bb_error_msg("bad address '%s'", org_host);
  168. if (ai_flags & DIE_ON_ERROR)
  169. xfunc_die();
  170. return NULL;
  171. }
  172. } else {
  173. cp = strrchr(host, ':');
  174. if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
  175. /* There is more than one ':' (e.g. "::1") */
  176. cp = NULL; /* it's not a port spec */
  177. }
  178. }
  179. if (cp) { /* points to ":" or "]:" */
  180. int sz = cp - host + 1;
  181. host = safe_strncpy(alloca(sz), host, sz);
  182. if (ENABLE_FEATURE_IPV6 && *cp != ':') {
  183. cp++; /* skip ']' */
  184. if (*cp == '\0') /* [xx] without port */
  185. goto skip;
  186. }
  187. cp++; /* skip ':' */
  188. port = bb_strtou(cp, NULL, 10);
  189. if (errno || (unsigned)port > 0xffff) {
  190. bb_error_msg("bad port spec '%s'", org_host);
  191. if (ai_flags & DIE_ON_ERROR)
  192. xfunc_die();
  193. return NULL;
  194. }
  195. skip: ;
  196. }
  197. memset(&hint, 0 , sizeof(hint));
  198. #if !ENABLE_FEATURE_IPV6
  199. hint.ai_family = AF_INET; /* do not try to find IPv6 */
  200. #else
  201. hint.ai_family = af;
  202. #endif
  203. /* Needed. Or else we will get each address thrice (or more)
  204. * for each possible socket type (tcp,udp,raw...): */
  205. hint.ai_socktype = SOCK_STREAM;
  206. hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
  207. rc = getaddrinfo(host, NULL, &hint, &result);
  208. if (rc || !result) {
  209. bb_error_msg("bad address '%s'", org_host);
  210. if (ai_flags & DIE_ON_ERROR)
  211. xfunc_die();
  212. goto ret;
  213. }
  214. used_res = result;
  215. #if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
  216. while (1) {
  217. if (used_res->ai_family == AF_INET)
  218. break;
  219. used_res = used_res->ai_next;
  220. if (!used_res) {
  221. used_res = result;
  222. break;
  223. }
  224. }
  225. #endif
  226. r = xmalloc(offsetof(len_and_sockaddr, u.sa) + used_res->ai_addrlen);
  227. r->len = used_res->ai_addrlen;
  228. memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
  229. set_nport(r, htons(port));
  230. ret:
  231. freeaddrinfo(result);
  232. return r;
  233. }
  234. #if !ENABLE_FEATURE_IPV6
  235. #define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
  236. #endif
  237. #if ENABLE_FEATURE_IPV6
  238. len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
  239. {
  240. return str2sockaddr(host, port, af, 0);
  241. }
  242. len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
  243. {
  244. return str2sockaddr(host, port, af, DIE_ON_ERROR);
  245. }
  246. #endif
  247. len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
  248. {
  249. return str2sockaddr(host, port, AF_UNSPEC, 0);
  250. }
  251. len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
  252. {
  253. return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
  254. }
  255. len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
  256. {
  257. return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
  258. }
  259. #undef xsocket_type
  260. int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type)
  261. {
  262. IF_NOT_FEATURE_IPV6(enum { family = AF_INET };)
  263. len_and_sockaddr *lsa;
  264. int fd;
  265. int len;
  266. #if ENABLE_FEATURE_IPV6
  267. if (family == AF_UNSPEC) {
  268. fd = socket(AF_INET6, sock_type, 0);
  269. if (fd >= 0) {
  270. family = AF_INET6;
  271. goto done;
  272. }
  273. family = AF_INET;
  274. }
  275. #endif
  276. fd = xsocket(family, sock_type, 0);
  277. len = sizeof(struct sockaddr_in);
  278. #if ENABLE_FEATURE_IPV6
  279. if (family == AF_INET6) {
  280. done:
  281. len = sizeof(struct sockaddr_in6);
  282. }
  283. #endif
  284. lsa = xzalloc(offsetof(len_and_sockaddr, u.sa) + len);
  285. lsa->len = len;
  286. lsa->u.sa.sa_family = family;
  287. *lsap = lsa;
  288. return fd;
  289. }
  290. int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
  291. {
  292. return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
  293. }
  294. static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
  295. {
  296. int fd;
  297. len_and_sockaddr *lsa;
  298. if (bindaddr && bindaddr[0]) {
  299. lsa = xdotted2sockaddr(bindaddr, port);
  300. /* user specified bind addr dictates family */
  301. fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
  302. } else {
  303. fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type);
  304. set_nport(lsa, htons(port));
  305. }
  306. setsockopt_reuseaddr(fd);
  307. xbind(fd, &lsa->u.sa, lsa->len);
  308. free(lsa);
  309. return fd;
  310. }
  311. int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
  312. {
  313. return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
  314. }
  315. int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
  316. {
  317. return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
  318. }
  319. int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
  320. {
  321. int fd;
  322. len_and_sockaddr *lsa;
  323. lsa = xhost2sockaddr(peer, port);
  324. fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
  325. setsockopt_reuseaddr(fd);
  326. xconnect(fd, &lsa->u.sa, lsa->len);
  327. free(lsa);
  328. return fd;
  329. }
  330. int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
  331. {
  332. int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
  333. xconnect(fd, &lsa->u.sa, lsa->len);
  334. return fd;
  335. }
  336. /* We hijack this constant to mean something else */
  337. /* It doesn't hurt because we will add this bit anyway */
  338. #define IGNORE_PORT NI_NUMERICSERV
  339. static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
  340. {
  341. char host[128];
  342. char serv[16];
  343. int rc;
  344. socklen_t salen;
  345. if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
  346. struct sockaddr_un *sun = (struct sockaddr_un *)sa;
  347. return xasprintf("local:%.*s",
  348. (int) sizeof(sun->sun_path),
  349. sun->sun_path);
  350. }
  351. salen = LSA_SIZEOF_SA;
  352. #if ENABLE_FEATURE_IPV6
  353. if (sa->sa_family == AF_INET)
  354. salen = sizeof(struct sockaddr_in);
  355. if (sa->sa_family == AF_INET6)
  356. salen = sizeof(struct sockaddr_in6);
  357. #endif
  358. rc = getnameinfo(sa, salen,
  359. host, sizeof(host),
  360. /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
  361. serv, sizeof(serv),
  362. /* do not resolve port# into service _name_ */
  363. flags | NI_NUMERICSERV
  364. );
  365. if (rc)
  366. return NULL;
  367. if (flags & IGNORE_PORT)
  368. return xstrdup(host);
  369. #if ENABLE_FEATURE_IPV6
  370. if (sa->sa_family == AF_INET6) {
  371. if (strchr(host, ':')) /* heh, it's not a resolved hostname */
  372. return xasprintf("[%s]:%s", host, serv);
  373. /*return xasprintf("%s:%s", host, serv);*/
  374. /* - fall through instead */
  375. }
  376. #endif
  377. /* For now we don't support anything else, so it has to be INET */
  378. /*if (sa->sa_family == AF_INET)*/
  379. return xasprintf("%s:%s", host, serv);
  380. /*return xstrdup(host);*/
  381. }
  382. char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
  383. {
  384. return sockaddr2str(sa, 0);
  385. }
  386. char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
  387. {
  388. return sockaddr2str(sa, IGNORE_PORT);
  389. }
  390. char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
  391. {
  392. return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
  393. }
  394. char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
  395. {
  396. return sockaddr2str(sa, NI_NUMERICHOST);
  397. }
  398. char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
  399. {
  400. return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
  401. }