xconnect.c 10 KB

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