3
0

xconnect.c 13 KB

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