2002-07-03 11:51:44 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-04-14 17:51:38 +00:00
|
|
|
* Connect to host at port using address resolution from getaddrinfo
|
2002-07-03 11:51:44 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2002-07-11 10:40:43 +00:00
|
|
|
#include <stdlib.h>
|
2002-07-03 11:51:44 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2003-10-31 09:31:46 +00:00
|
|
|
#include <errno.h>
|
2002-07-03 11:51:44 +00:00
|
|
|
#include <netdb.h>
|
2003-10-31 09:31:46 +00:00
|
|
|
#include <sys/socket.h>
|
2002-07-11 10:40:43 +00:00
|
|
|
#include <netinet/in.h>
|
2003-10-31 09:31:46 +00:00
|
|
|
#include <arpa/inet.h>
|
2002-07-03 11:51:44 +00:00
|
|
|
#include "libbb.h"
|
|
|
|
|
2003-12-20 01:47:18 +00:00
|
|
|
/* Return network byte ordered port number for a service.
|
|
|
|
* If "port" is a number use it as the port.
|
|
|
|
* If "port" is a name it is looked up in /etc/services, if it isnt found return
|
|
|
|
* default_port
|
|
|
|
*/
|
2004-01-17 05:03:31 +00:00
|
|
|
unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port)
|
2002-07-03 11:51:44 +00:00
|
|
|
{
|
2003-12-20 01:47:18 +00:00
|
|
|
unsigned short port_nr = htons(default_port);
|
|
|
|
if (port) {
|
2003-12-23 20:37:23 +00:00
|
|
|
char *endptr;
|
|
|
|
int old_errno;
|
|
|
|
long port_long;
|
2002-07-03 11:51:44 +00:00
|
|
|
|
2003-12-23 20:37:23 +00:00
|
|
|
/* Since this is a lib function, we're not allowed to reset errno to 0.
|
|
|
|
* Doing so could break an app that is deferring checking of errno. */
|
|
|
|
old_errno = errno;
|
|
|
|
errno = 0;
|
|
|
|
port_long = strtol(port, &endptr, 10);
|
2003-12-20 01:47:18 +00:00
|
|
|
if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
|
2004-01-17 05:03:31 +00:00
|
|
|
struct servent *tserv = getservbyname(port, protocol);
|
2003-12-20 01:47:18 +00:00
|
|
|
if (tserv) {
|
2003-12-23 20:37:54 +00:00
|
|
|
port_nr = tserv->s_port;
|
|
|
|
}
|
|
|
|
} else {
|
2003-12-20 01:47:18 +00:00
|
|
|
port_nr = htons(port_long);
|
|
|
|
}
|
2003-12-23 20:37:23 +00:00
|
|
|
errno = old_errno;
|
2002-07-03 11:51:44 +00:00
|
|
|
}
|
2003-10-31 09:31:46 +00:00
|
|
|
return port_nr;
|
|
|
|
}
|
2002-07-03 11:51:44 +00:00
|
|
|
|
2003-12-20 01:47:18 +00:00
|
|
|
void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
|
2003-10-31 09:31:46 +00:00
|
|
|
{
|
|
|
|
struct hostent *he;
|
2002-07-03 11:51:44 +00:00
|
|
|
|
2003-10-31 09:31:46 +00:00
|
|
|
memset(s_in, 0, sizeof(struct sockaddr_in));
|
|
|
|
s_in->sin_family = AF_INET;
|
2002-07-03 11:51:44 +00:00
|
|
|
he = xgethostbyname(host);
|
2003-10-31 09:31:46 +00:00
|
|
|
memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
int xconnect(struct sockaddr_in *s_addr)
|
|
|
|
{
|
|
|
|
int s = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (connect(s, (struct sockaddr_in *)s_addr, sizeof(struct sockaddr_in)) < 0)
|
2002-07-03 11:51:44 +00:00
|
|
|
{
|
2005-08-22 15:57:50 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) close(s);
|
2004-03-15 08:29:22 +00:00
|
|
|
bb_perror_msg_and_die("Unable to connect to remote host (%s)",
|
2003-10-31 09:31:46 +00:00
|
|
|
inet_ntoa(s_addr->sin_addr));
|
2002-07-03 11:51:44 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|