aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-12-20 01:47:18 +0000
committerbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-12-20 01:47:18 +0000
commitdd797e19af7a7c3ee42c6fc37a4d09c883cf6b67 (patch)
tree859f5849c30de6cb69bf6336af6d2228402f2395 /libbb
parent2c55d72493d14416986fa7eee6b66ee034de0658 (diff)
downloadbusybox-w32-dd797e19af7a7c3ee42c6fc37a4d09c883cf6b67.tar.gz
busybox-w32-dd797e19af7a7c3ee42c6fc37a4d09c883cf6b67.tar.bz2
busybox-w32-dd797e19af7a7c3ee42c6fc37a4d09c883cf6b67.zip
Change interface to bb_lookup_host, dont try and set port inside this
function as there is no gracefull way of handling failures. Rename bb_getport to bb_lookup_port, allow a default port to be specified so it always returns a correct value. Modify ftpgetput/rdate/wget to use the new interface. wget/rdate now use etc/services with a falback default value. git-svn-id: svn://busybox.net/trunk/busybox@8140 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r--libbb/xconnect.c33
1 files changed, 15 insertions, 18 deletions
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index 2cbc8400b..b3619fd0e 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -18,30 +18,31 @@
18#include <arpa/inet.h> 18#include <arpa/inet.h>
19#include "libbb.h" 19#include "libbb.h"
20 20
21int bb_getport(const char *port) 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 */
26unsigned short bb_lookup_port(const char *port, unsigned short default_port)
22{ 27{
23 int port_nr; 28 unsigned short port_nr = htons(default_port);
29 if (port) {
24 char *endptr; 30 char *endptr;
25 struct servent *tserv; 31 long port_long = strtol(port, &endptr, 10);
26 32
27 if (!port) { 33 if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
28 return -1; 34 struct servent *tserv = getservbyname(port, "tcp");
29 } 35 if (tserv) {
30 port_nr=strtol(port, &endptr, 10);
31 if (errno != 0 || *endptr!='\0' || endptr==port || port_nr < 1 || port_nr > 65536)
32 {
33 if (port_nr==0 && (tserv = getservbyname(port, "tcp")) != NULL) {
34 port_nr = tserv->s_port; 36 port_nr = tserv->s_port;
35 } else {
36 return -1;
37 } 37 }
38 } else { 38 } else {
39 port_nr = htons(port_nr); 39 port_nr = htons(port_long);
40 }
40 } 41 }
41 return port_nr; 42 return port_nr;
42} 43}
43 44
44void bb_lookup_host(struct sockaddr_in *s_in, const char *host, const char *port) 45void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
45{ 46{
46 struct hostent *he; 47 struct hostent *he;
47 48
@@ -49,10 +50,6 @@ void bb_lookup_host(struct sockaddr_in *s_in, const char *host, const char *port
49 s_in->sin_family = AF_INET; 50 s_in->sin_family = AF_INET;
50 he = xgethostbyname(host); 51 he = xgethostbyname(host);
51 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length); 52 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} 53}
57 54
58int xconnect(struct sockaddr_in *s_addr) 55int xconnect(struct sockaddr_in *s_addr)