xconnect.c 14 KB

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