blob: 39052b87b6d584ce29e6fc8aa22d0a10f3910dd5 [file] [log] [blame]
Eric Andersen0b315862002-07-03 11:51:44 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenaff114c2004-04-14 17:51:38 +00005 * Connect to host at port using address resolution from getaddrinfo
Eric Andersen0b315862002-07-03 11:51:44 +00006 *
7 */
8
9#include <unistd.h>
10#include <string.h>
Eric Andersencafc1032002-07-11 10:40:43 +000011#include <stdlib.h>
Eric Andersen0b315862002-07-03 11:51:44 +000012#include <sys/types.h>
13#include <sys/socket.h>
Eric Andersene6dc4392003-10-31 09:31:46 +000014#include <errno.h>
Eric Andersen0b315862002-07-03 11:51:44 +000015#include <netdb.h>
Eric Andersene6dc4392003-10-31 09:31:46 +000016#include <sys/socket.h>
Eric Andersencafc1032002-07-11 10:40:43 +000017#include <netinet/in.h>
Eric Andersene6dc4392003-10-31 09:31:46 +000018#include <arpa/inet.h>
Eric Andersen0b315862002-07-03 11:51:44 +000019#include "libbb.h"
20
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000021/* Return network byte ordered port number for a service.
22 * If "port" is a number use it as the port.
23 * If "port" is a name it is looked up in /etc/services, if it isnt found return
24 * default_port
25 */
Glenn L McGrath036dbaa2004-01-17 05:03:31 +000026unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port)
Eric Andersen0b315862002-07-03 11:51:44 +000027{
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000028 unsigned short port_nr = htons(default_port);
29 if (port) {
Eric Andersen9cccc182003-12-23 20:37:23 +000030 char *endptr;
31 int old_errno;
32 long port_long;
Eric Andersen0b315862002-07-03 11:51:44 +000033
Eric Andersen9cccc182003-12-23 20:37:23 +000034 /* Since this is a lib function, we're not allowed to reset errno to 0.
35 * Doing so could break an app that is deferring checking of errno. */
36 old_errno = errno;
37 errno = 0;
38 port_long = strtol(port, &endptr, 10);
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000039 if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
Glenn L McGrath036dbaa2004-01-17 05:03:31 +000040 struct servent *tserv = getservbyname(port, protocol);
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000041 if (tserv) {
Eric Andersen3a5ed562003-12-23 20:37:54 +000042 port_nr = tserv->s_port;
43 }
44 } else {
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000045 port_nr = htons(port_long);
46 }
Eric Andersen9cccc182003-12-23 20:37:23 +000047 errno = old_errno;
Eric Andersene6dc4392003-10-31 09:31:46 +000048 }
49 return port_nr;
50}
51
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000052void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
Eric Andersene6dc4392003-10-31 09:31:46 +000053{
54 struct hostent *he;
55
56 memset(s_in, 0, sizeof(struct sockaddr_in));
57 s_in->sin_family = AF_INET;
58 he = xgethostbyname(host);
59 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
Eric Andersene6dc4392003-10-31 09:31:46 +000060}
61
62int xconnect(struct sockaddr_in *s_addr)
63{
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +000064 int s = bb_xsocket(AF_INET, SOCK_STREAM, 0);
"Vladimir N. Oleynik"06776b32006-02-17 09:33:22 +000065 if (connect(s, (struct sockaddr *)s_addr, sizeof(struct sockaddr_in)) < 0)
Eric Andersene6dc4392003-10-31 09:31:46 +000066 {
Rob Landley64175642005-08-22 15:57:50 +000067 if (ENABLE_FEATURE_CLEAN_UP) close(s);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000068 bb_perror_msg_and_die("Unable to connect to remote host (%s)",
Eric Andersene6dc4392003-10-31 09:31:46 +000069 inet_ntoa(s_addr->sin_addr));
Eric Andersen0b315862002-07-03 11:51:44 +000070 }
71 return s;
Eric Andersen0b315862002-07-03 11:51:44 +000072}