3
0

xconnect.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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(len_and_sockaddr *lsa, unsigned port)
  122. {
  123. #if ENABLE_FEATURE_IPV6
  124. if (lsa->u.sa.sa_family == AF_INET6) {
  125. lsa->u.sin6.sin6_port = port;
  126. return;
  127. }
  128. #endif
  129. if (lsa->u.sa.sa_family == AF_INET) {
  130. lsa->u.sin.sin_port = port;
  131. return;
  132. }
  133. /* What? UNIX socket? IPX?? :) */
  134. }
  135. /* We hijack this constant to mean something else */
  136. /* It doesn't hurt because we will remove this bit anyway */
  137. #define DIE_ON_ERROR AI_CANONNAME
  138. /* host: "1.2.3.4[:port]", "www.google.com[:port]"
  139. * port: if neither of above specifies port # */
  140. static len_and_sockaddr* str2sockaddr(
  141. const char *host, int port,
  142. IF_FEATURE_IPV6(sa_family_t af,)
  143. int ai_flags)
  144. {
  145. IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
  146. int rc;
  147. len_and_sockaddr *r;
  148. struct addrinfo *result = NULL;
  149. struct addrinfo *used_res;
  150. const char *org_host = host; /* only for error msg */
  151. const char *cp;
  152. struct addrinfo hint;
  153. if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
  154. struct sockaddr_un *sun;
  155. r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
  156. r->len = sizeof(struct sockaddr_un);
  157. r->u.sa.sa_family = AF_UNIX;
  158. sun = (struct sockaddr_un *)&r->u.sa;
  159. safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
  160. return r;
  161. }
  162. r = NULL;
  163. /* Ugly parsing of host:addr */
  164. if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
  165. /* Even uglier parsing of [xx]:nn */
  166. host++;
  167. cp = strchr(host, ']');
  168. if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
  169. /* Malformed: must be [xx]:nn or [xx] */
  170. bb_error_msg("bad address '%s'", org_host);
  171. if (ai_flags & DIE_ON_ERROR)
  172. xfunc_die();
  173. return NULL;
  174. }
  175. } else {
  176. cp = strrchr(host, ':');
  177. if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
  178. /* There is more than one ':' (e.g. "::1") */
  179. cp = NULL; /* it's not a port spec */
  180. }
  181. }
  182. if (cp) { /* points to ":" or "]:" */
  183. int sz = cp - host + 1;
  184. host = safe_strncpy(alloca(sz), host, sz);
  185. if (ENABLE_FEATURE_IPV6 && *cp != ':') {
  186. cp++; /* skip ']' */
  187. if (*cp == '\0') /* [xx] without port */
  188. goto skip;
  189. }
  190. cp++; /* skip ':' */
  191. port = bb_strtou(cp, NULL, 10);
  192. if (errno || (unsigned)port > 0xffff) {
  193. bb_error_msg("bad port spec '%s'", org_host);
  194. if (ai_flags & DIE_ON_ERROR)
  195. xfunc_die();
  196. return NULL;
  197. }
  198. skip: ;
  199. }
  200. /* Next two if blocks allow to skip getaddrinfo()
  201. * in case host name is a numeric IP(v6) address.
  202. * getaddrinfo() initializes DNS resolution machinery,
  203. * scans network config and such - tens of syscalls.
  204. */
  205. /* If we were not asked specifically for IPv6,
  206. * check whether this is a numeric IPv4 */
  207. IF_FEATURE_IPV6(if(af != AF_INET6)) {
  208. struct in_addr in4;
  209. if (inet_aton(host, &in4) != 0) {
  210. r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in));
  211. r->len = sizeof(struct sockaddr_in);
  212. r->u.sa.sa_family = AF_INET;
  213. r->u.sin.sin_addr = in4;
  214. goto set_port;
  215. }
  216. }
  217. #if ENABLE_FEATURE_IPV6
  218. /* If we were not asked specifically for IPv4,
  219. * check whether this is a numeric IPv6 */
  220. if (af != AF_INET) {
  221. struct in6_addr in6;
  222. if (inet_pton(AF_INET6, host, &in6) > 0) {
  223. r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6));
  224. r->len = sizeof(struct sockaddr_in6);
  225. r->u.sa.sa_family = AF_INET6;
  226. r->u.sin6.sin6_addr = in6;
  227. goto set_port;
  228. }
  229. }
  230. #endif
  231. memset(&hint, 0 , sizeof(hint));
  232. hint.ai_family = af;
  233. /* Needed. Or else we will get each address thrice (or more)
  234. * for each possible socket type (tcp,udp,raw...): */
  235. hint.ai_socktype = SOCK_STREAM;
  236. hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
  237. rc = getaddrinfo(host, NULL, &hint, &result);
  238. if (rc || !result) {
  239. bb_error_msg("bad address '%s'", org_host);
  240. if (ai_flags & DIE_ON_ERROR)
  241. xfunc_die();
  242. goto ret;
  243. }
  244. used_res = result;
  245. #if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
  246. while (1) {
  247. if (used_res->ai_family == AF_INET)
  248. break;
  249. used_res = used_res->ai_next;
  250. if (!used_res) {
  251. used_res = result;
  252. break;
  253. }
  254. }
  255. #endif
  256. r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen);
  257. r->len = used_res->ai_addrlen;
  258. memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
  259. set_port:
  260. set_nport(r, htons(port));
  261. ret:
  262. freeaddrinfo(result);
  263. return r;
  264. }
  265. #if !ENABLE_FEATURE_IPV6
  266. #define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
  267. #endif
  268. #if ENABLE_FEATURE_IPV6
  269. len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
  270. {
  271. return str2sockaddr(host, port, af, 0);
  272. }
  273. len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
  274. {
  275. return str2sockaddr(host, port, af, DIE_ON_ERROR);
  276. }
  277. #endif
  278. len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
  279. {
  280. return str2sockaddr(host, port, AF_UNSPEC, 0);
  281. }
  282. len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
  283. {
  284. return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
  285. }
  286. len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
  287. {
  288. return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
  289. }
  290. #undef xsocket_type
  291. int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type)
  292. {
  293. IF_NOT_FEATURE_IPV6(enum { family = AF_INET };)
  294. len_and_sockaddr *lsa;
  295. int fd;
  296. int len;
  297. #if ENABLE_FEATURE_IPV6
  298. if (family == AF_UNSPEC) {
  299. fd = socket(AF_INET6, sock_type, 0);
  300. if (fd >= 0) {
  301. family = AF_INET6;
  302. goto done;
  303. }
  304. family = AF_INET;
  305. }
  306. #endif
  307. fd = xsocket(family, sock_type, 0);
  308. len = sizeof(struct sockaddr_in);
  309. #if ENABLE_FEATURE_IPV6
  310. if (family == AF_INET6) {
  311. done:
  312. len = sizeof(struct sockaddr_in6);
  313. }
  314. #endif
  315. lsa = xzalloc(LSA_LEN_SIZE + len);
  316. lsa->len = len;
  317. lsa->u.sa.sa_family = family;
  318. *lsap = lsa;
  319. return fd;
  320. }
  321. int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
  322. {
  323. return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
  324. }
  325. static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
  326. {
  327. int fd;
  328. len_and_sockaddr *lsa;
  329. if (bindaddr && bindaddr[0]) {
  330. lsa = xdotted2sockaddr(bindaddr, port);
  331. /* user specified bind addr dictates family */
  332. fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
  333. } else {
  334. fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type);
  335. set_nport(lsa, htons(port));
  336. }
  337. setsockopt_reuseaddr(fd);
  338. xbind(fd, &lsa->u.sa, lsa->len);
  339. free(lsa);
  340. return fd;
  341. }
  342. int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
  343. {
  344. return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
  345. }
  346. int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
  347. {
  348. return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
  349. }
  350. int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
  351. {
  352. int fd;
  353. len_and_sockaddr *lsa;
  354. lsa = xhost2sockaddr(peer, port);
  355. fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
  356. setsockopt_reuseaddr(fd);
  357. xconnect(fd, &lsa->u.sa, lsa->len);
  358. free(lsa);
  359. return fd;
  360. }
  361. int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
  362. {
  363. int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
  364. xconnect(fd, &lsa->u.sa, lsa->len);
  365. return fd;
  366. }
  367. /* We hijack this constant to mean something else */
  368. /* It doesn't hurt because we will add this bit anyway */
  369. #define IGNORE_PORT NI_NUMERICSERV
  370. static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
  371. {
  372. char host[128];
  373. char serv[16];
  374. int rc;
  375. socklen_t salen;
  376. if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
  377. struct sockaddr_un *sun = (struct sockaddr_un *)sa;
  378. return xasprintf("local:%.*s",
  379. (int) sizeof(sun->sun_path),
  380. sun->sun_path);
  381. }
  382. salen = LSA_SIZEOF_SA;
  383. #if ENABLE_FEATURE_IPV6
  384. if (sa->sa_family == AF_INET)
  385. salen = sizeof(struct sockaddr_in);
  386. if (sa->sa_family == AF_INET6)
  387. salen = sizeof(struct sockaddr_in6);
  388. #endif
  389. rc = getnameinfo(sa, salen,
  390. host, sizeof(host),
  391. /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
  392. serv, sizeof(serv),
  393. /* do not resolve port# into service _name_ */
  394. flags | NI_NUMERICSERV
  395. );
  396. if (rc)
  397. return NULL;
  398. if (flags & IGNORE_PORT)
  399. return xstrdup(host);
  400. #if ENABLE_FEATURE_IPV6
  401. if (sa->sa_family == AF_INET6) {
  402. if (strchr(host, ':')) /* heh, it's not a resolved hostname */
  403. return xasprintf("[%s]:%s", host, serv);
  404. /*return xasprintf("%s:%s", host, serv);*/
  405. /* - fall through instead */
  406. }
  407. #endif
  408. /* For now we don't support anything else, so it has to be INET */
  409. /*if (sa->sa_family == AF_INET)*/
  410. return xasprintf("%s:%s", host, serv);
  411. /*return xstrdup(host);*/
  412. }
  413. char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
  414. {
  415. return sockaddr2str(sa, 0);
  416. }
  417. char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
  418. {
  419. return sockaddr2str(sa, IGNORE_PORT);
  420. }
  421. char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
  422. {
  423. return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
  424. }
  425. char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
  426. {
  427. return sockaddr2str(sa, NI_NUMERICHOST);
  428. }
  429. char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
  430. {
  431. return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
  432. }