3
0

xconnect.c 12 KB

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