Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 5 | * Connect to host at port using address resolution from getaddrinfo |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <unistd.h> |
| 10 | #include <string.h> |
Eric Andersen | cafc103 | 2002-07-11 10:40:43 +0000 | [diff] [blame] | 11 | #include <stdlib.h> |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <sys/socket.h> |
Eric Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 14 | #include <errno.h> |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 15 | #include <netdb.h> |
Eric Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 16 | #include <sys/socket.h> |
Eric Andersen | cafc103 | 2002-07-11 10:40:43 +0000 | [diff] [blame] | 17 | #include <netinet/in.h> |
Eric Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 18 | #include <arpa/inet.h> |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 19 | #include "libbb.h" |
| 20 | |
Glenn L McGrath | ffccf6e | 2003-12-20 01:47:18 +0000 | [diff] [blame] | 21 | /* 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 McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 26 | unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port) |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 27 | { |
Glenn L McGrath | ffccf6e | 2003-12-20 01:47:18 +0000 | [diff] [blame] | 28 | unsigned short port_nr = htons(default_port); |
| 29 | if (port) { |
Eric Andersen | 9cccc18 | 2003-12-23 20:37:23 +0000 | [diff] [blame] | 30 | char *endptr; |
| 31 | int old_errno; |
| 32 | long port_long; |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 33 | |
Eric Andersen | 9cccc18 | 2003-12-23 20:37:23 +0000 | [diff] [blame] | 34 | /* 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 McGrath | ffccf6e | 2003-12-20 01:47:18 +0000 | [diff] [blame] | 39 | if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) { |
Glenn L McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 40 | struct servent *tserv = getservbyname(port, protocol); |
Glenn L McGrath | ffccf6e | 2003-12-20 01:47:18 +0000 | [diff] [blame] | 41 | if (tserv) { |
Eric Andersen | 3a5ed56 | 2003-12-23 20:37:54 +0000 | [diff] [blame] | 42 | port_nr = tserv->s_port; |
| 43 | } |
| 44 | } else { |
Glenn L McGrath | ffccf6e | 2003-12-20 01:47:18 +0000 | [diff] [blame] | 45 | port_nr = htons(port_long); |
| 46 | } |
Eric Andersen | 9cccc18 | 2003-12-23 20:37:23 +0000 | [diff] [blame] | 47 | errno = old_errno; |
Eric Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 48 | } |
| 49 | return port_nr; |
| 50 | } |
| 51 | |
Glenn L McGrath | ffccf6e | 2003-12-20 01:47:18 +0000 | [diff] [blame] | 52 | void bb_lookup_host(struct sockaddr_in *s_in, const char *host) |
Eric Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 53 | { |
| 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 Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int xconnect(struct sockaddr_in *s_addr) |
| 63 | { |
| 64 | int s = socket(AF_INET, SOCK_STREAM, 0); |
| 65 | if (connect(s, (struct sockaddr_in *)s_addr, sizeof(struct sockaddr_in)) < 0) |
| 66 | { |
Rob Landley | 6417564 | 2005-08-22 15:57:50 +0000 | [diff] [blame] | 67 | if (ENABLE_FEATURE_CLEAN_UP) close(s); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 68 | bb_perror_msg_and_die("Unable to connect to remote host (%s)", |
Eric Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 69 | inet_ntoa(s_addr->sin_addr)); |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 70 | } |
| 71 | return s; |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 72 | } |