xconnect.c 12 KB

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