blob: 27459791ec1ea9b9f65cda99647a8272e706e1fc [file] [log] [blame]
Eric Andersen0b315862002-07-03 11:51:44 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Connect to host at port using address resolusion from getaddrinfo
6 *
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
Eric Andersene6dc4392003-10-31 09:31:46 +000021int bb_getport(char *port)
Eric Andersen0b315862002-07-03 11:51:44 +000022{
Eric Andersene6dc4392003-10-31 09:31:46 +000023 int port_nr;
24 char *endptr;
Eric Andersen0b315862002-07-03 11:51:44 +000025 struct servent *tserv;
Eric Andersen0b315862002-07-03 11:51:44 +000026
Eric Andersene6dc4392003-10-31 09:31:46 +000027 if (!port) {
28 return -1;
29 }
30 port_nr=strtol(port, &endptr, 10);
31 if (errno != 0 || *endptr!='\0' || endptr==port || port_nr < 1 || port_nr > 65536)
Eric Andersen0b315862002-07-03 11:51:44 +000032 {
Eric Andersene6dc4392003-10-31 09:31:46 +000033 if (port_nr==0 && (tserv = getservbyname(port, "tcp")) != NULL) {
34 port_nr = tserv->s_port;
35 } else {
36 return -1;
37 }
38 } else {
39 port_nr = htons(port_nr);
40 }
41 return port_nr;
42}
43
44void bb_lookup_host(struct sockaddr_in *s_in, char *host, char *port)
45{
46 struct hostent *he;
47
48 memset(s_in, 0, sizeof(struct sockaddr_in));
49 s_in->sin_family = AF_INET;
50 he = xgethostbyname(host);
51 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
52
53 if (port) {
54 s_in->sin_port=bb_getport(port);
55 }
56}
57
58int xconnect(struct sockaddr_in *s_addr)
59{
60 int s = socket(AF_INET, SOCK_STREAM, 0);
61 if (connect(s, (struct sockaddr_in *)s_addr, sizeof(struct sockaddr_in)) < 0)
62 {
63 bb_perror_msg_and_die("Unable to connect to remote host (%s)",
64 inet_ntoa(s_addr->sin_addr));
Eric Andersen0b315862002-07-03 11:51:44 +000065 }
66 return s;
Eric Andersen0b315862002-07-03 11:51:44 +000067}