aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2004-01-17 05:03:31 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2004-01-17 05:03:31 +0000
commit036dbaa082f1974246d1d7d21a8e163559642485 (patch)
tree2488441544ab16f8e08d0955d323019e96ed3d4f
parentb03be7f5677b86acfe2f64b5a57e1f361e257f6c (diff)
downloadbusybox-w32-036dbaa082f1974246d1d7d21a8e163559642485.tar.gz
busybox-w32-036dbaa082f1974246d1d7d21a8e163559642485.tar.bz2
busybox-w32-036dbaa082f1974246d1d7d21a8e163559642485.zip
Modify bb_lookup_port to allow the protocol to be specified, allowing
/etc/services support for inetd, netcat and tftp.
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/xconnect.c4
-rw-r--r--networking/ftpgetput.c2
-rw-r--r--networking/inetd.c20
-rw-r--r--networking/nc.c6
-rw-r--r--networking/telnet.c2
-rw-r--r--networking/tftp.c13
-rw-r--r--networking/wget.c6
8 files changed, 23 insertions, 32 deletions
diff --git a/include/libbb.h b/include/libbb.h
index b2592c824..86e4fbea7 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -297,7 +297,7 @@ extern struct hostent *xgethostbyname2(const char *name, int af);
297extern int create_icmp_socket(void); 297extern int create_icmp_socket(void);
298extern int create_icmp6_socket(void); 298extern int create_icmp6_socket(void);
299extern int xconnect(struct sockaddr_in *s_addr); 299extern int xconnect(struct sockaddr_in *s_addr);
300extern unsigned short bb_lookup_port(const char *port, unsigned short default_port); 300extern unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port);
301extern void bb_lookup_host(struct sockaddr_in *s_in, const char *host); 301extern void bb_lookup_host(struct sockaddr_in *s_in, const char *host);
302 302
303//#warning wrap this? 303//#warning wrap this?
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index 29b984720..2443bb299 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -23,7 +23,7 @@
23 * If "port" is a name it is looked up in /etc/services, if it isnt found return 23 * If "port" is a name it is looked up in /etc/services, if it isnt found return
24 * default_port 24 * default_port
25 */ 25 */
26unsigned short bb_lookup_port(const char *port, unsigned short default_port) 26unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port)
27{ 27{
28 unsigned short port_nr = htons(default_port); 28 unsigned short port_nr = htons(default_port);
29 if (port) { 29 if (port) {
@@ -37,7 +37,7 @@ unsigned short bb_lookup_port(const char *port, unsigned short default_port)
37 errno = 0; 37 errno = 0;
38 port_long = strtol(port, &endptr, 10); 38 port_long = strtol(port, &endptr, 10);
39 if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) { 39 if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
40 struct servent *tserv = getservbyname(port, "tcp"); 40 struct servent *tserv = getservbyname(port, protocol);
41 if (tserv) { 41 if (tserv) {
42 port_nr = tserv->s_port; 42 port_nr = tserv->s_port;
43 } 43 }
diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c
index 27b272a48..4f6be1196 100644
--- a/networking/ftpgetput.c
+++ b/networking/ftpgetput.c
@@ -349,7 +349,7 @@ int ftpgetput_main(int argc, char **argv)
349 * and we want to connect to only one IP... */ 349 * and we want to connect to only one IP... */
350 server->s_in = &s_in; 350 server->s_in = &s_in;
351 bb_lookup_host(&s_in, argv[optind]); 351 bb_lookup_host(&s_in, argv[optind]);
352 s_in.sin_port = bb_lookup_port(port, 21); 352 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
353 if (verbose_flag) { 353 if (verbose_flag) {
354 printf("Connecting to %s[%s]:%d\n", 354 printf("Connecting to %s[%s]:%d\n",
355 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port)); 355 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
diff --git a/networking/inetd.c b/networking/inetd.c
index d225527a9..24415fe7d 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -610,19 +610,13 @@ static void config(int signum)
610 sep->se_ctrladdr_in.sin_family = AF_INET; 610 sep->se_ctrladdr_in.sin_family = AF_INET;
611 sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in; 611 sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
612 { 612 {
613 u_short port = htons(atoi(sep->se_service)); 613 u_short port = bb_lookup_port(sep->se_service, sep->se_proto, 0);
614 614
615 if (!port) { 615 if (port == 0) {
616 struct servent *sp; 616 syslog(LOG_ERR,
617 sp = getservbyname(sep->se_service, 617 "%s/%s: unknown service",
618 sep->se_proto); 618 sep->se_service, sep->se_proto);
619 if (sp == 0) { 619 continue;
620 syslog(LOG_ERR,
621 "%s/%s: unknown service",
622 sep->se_service, sep->se_proto);
623 continue;
624 }
625 port = sp->s_port;
626 } 620 }
627 if (port != sep->se_ctrladdr_in.sin_port) { 621 if (port != sep->se_ctrladdr_in.sin_port) {
628 sep->se_ctrladdr_in.sin_port = port; 622 sep->se_ctrladdr_in.sin_port = port;
diff --git a/networking/nc.c b/networking/nc.c
index 4888ccceb..ecb4a007b 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -61,7 +61,7 @@ int nc_main(int argc, char **argv)
61 do_listen++; 61 do_listen++;
62 break; 62 break;
63 case 'p': 63 case 'p':
64 lport = atoi(optarg); 64 lport = bb_lookup_port(optarg, "tcp", 0);
65 break; 65 break;
66 case 'i': 66 case 'i':
67 delay = atoi(optarg); 67 delay = atoi(optarg);
@@ -96,7 +96,7 @@ int nc_main(int argc, char **argv)
96 96
97 if (lport != 0) { 97 if (lport != 0) {
98 memset(&address.sin_addr, 0, sizeof(address.sin_addr)); 98 memset(&address.sin_addr, 0, sizeof(address.sin_addr));
99 address.sin_port = htons(lport); 99 address.sin_port = lport;
100 100
101 if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) 101 if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
102 bb_perror_msg_and_die("bind"); 102 bb_perror_msg_and_die("bind");
@@ -117,7 +117,7 @@ int nc_main(int argc, char **argv)
117 hostinfo = xgethostbyname(argv[optind]); 117 hostinfo = xgethostbyname(argv[optind]);
118 118
119 address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; 119 address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
120 address.sin_port = htons(atoi(argv[optind+1])); 120 address.sin_port = bb_lookup_port(argv[optind+1], "tcp", 0);
121 121
122 if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) 122 if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
123 bb_perror_msg_and_die("connect"); 123 bb_perror_msg_and_die("connect");
diff --git a/networking/telnet.c b/networking/telnet.c
index 110c9d151..1b71bf26a 100644
--- a/networking/telnet.c
+++ b/networking/telnet.c
@@ -599,7 +599,7 @@ extern int telnet_main(int argc, char** argv)
599 bb_show_usage(); 599 bb_show_usage();
600 600
601 bb_lookup_host(&s_in, argv[1]); 601 bb_lookup_host(&s_in, argv[1]);
602 s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", 23); 602 s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
603 603
604 G.netfd = xconnect(&s_in); 604 G.netfd = xconnect(&s_in);
605 605
diff --git a/networking/tftp.c b/networking/tftp.c
index a1a79a09c..146857686 100644
--- a/networking/tftp.c
+++ b/networking/tftp.c
@@ -138,7 +138,7 @@ static char *tftp_option_get(char *buf, int len, char *option)
138#endif 138#endif
139 139
140static inline int tftp(const int cmd, const struct hostent *host, 140static inline int tftp(const int cmd, const struct hostent *host,
141 const char *remotefile, int localfd, const int port, int tftp_bufsize) 141 const char *remotefile, int localfd, const unsigned short port, int tftp_bufsize)
142{ 142{
143 const int cmd_get = cmd & tftp_cmd_get; 143 const int cmd_get = cmd & tftp_cmd_get;
144 const int cmd_put = cmd & tftp_cmd_put; 144 const int cmd_put = cmd & tftp_cmd_put;
@@ -179,7 +179,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
179 bind(socketfd, (struct sockaddr *)&sa, len); 179 bind(socketfd, (struct sockaddr *)&sa, len);
180 180
181 sa.sin_family = host->h_addrtype; 181 sa.sin_family = host->h_addrtype;
182 sa.sin_port = htons(port); 182 sa.sin_port = port;
183 memcpy(&sa.sin_addr, (struct in_addr *) host->h_addr, 183 memcpy(&sa.sin_addr, (struct in_addr *) host->h_addr,
184 sizeof(sa.sin_addr)); 184 sizeof(sa.sin_addr));
185 185
@@ -332,7 +332,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
332 332
333 timeout = 0; 333 timeout = 0;
334 334
335 if (sa.sin_port == htons(port)) { 335 if (sa.sin_port == port) {
336 sa.sin_port = from.sin_port; 336 sa.sin_port = from.sin_port;
337 } 337 }
338 if (sa.sin_port == from.sin_port) { 338 if (sa.sin_port == from.sin_port) {
@@ -487,7 +487,7 @@ int tftp_main(int argc, char **argv)
487 struct hostent *host = NULL; 487 struct hostent *host = NULL;
488 char *localfile = NULL; 488 char *localfile = NULL;
489 char *remotefile = NULL; 489 char *remotefile = NULL;
490 int port = 69; 490 int port;
491 int cmd = 0; 491 int cmd = 0;
492 int fd = -1; 492 int fd = -1;
493 int flags = 0; 493 int flags = 0;
@@ -564,10 +564,7 @@ int tftp_main(int argc, char **argv)
564 } 564 }
565 565
566 host = xgethostbyname(argv[optind]); 566 host = xgethostbyname(argv[optind]);
567 567 port = bb_lookup_port(argv[optind + 1], "udp", 69);
568 if (optind + 2 == argc) {
569 port = atoi(argv[optind + 1]);
570 }
571 568
572#ifdef CONFIG_FEATURE_TFTP_DEBUG 569#ifdef CONFIG_FEATURE_TFTP_DEBUG
573 printf("using server \"%s\", remotefile \"%s\", " 570 printf("using server \"%s\", remotefile \"%s\", "
diff --git a/networking/wget.c b/networking/wget.c
index 8bed7369f..c92771e18 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -537,11 +537,11 @@ void parse_url(char *url, struct host_info *h)
537 char *cp, *sp, *up, *pp; 537 char *cp, *sp, *up, *pp;
538 538
539 if (strncmp(url, "http://", 7) == 0) { 539 if (strncmp(url, "http://", 7) == 0) {
540 h->port = bb_lookup_port("http", 80); 540 h->port = bb_lookup_port("http", "tcp", 80);
541 h->host = url + 7; 541 h->host = url + 7;
542 h->is_ftp = 0; 542 h->is_ftp = 0;
543 } else if (strncmp(url, "ftp://", 6) == 0) { 543 } else if (strncmp(url, "ftp://", 6) == 0) {
544 h->port = bb_lookup_port("ftp", 21); 544 h->port = bb_lookup_port("ftp", "tfp", 21);
545 h->host = url + 6; 545 h->host = url + 6;
546 h->is_ftp = 1; 546 h->is_ftp = 1;
547 } else 547 } else
@@ -834,7 +834,7 @@ progressmeter(int flag)
834 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 834 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
835 * SUCH DAMAGE. 835 * SUCH DAMAGE.
836 * 836 *
837 * $Id: wget.c,v 1.64 2003/12/27 00:21:47 bug1 Exp $ 837 * $Id: wget.c,v 1.65 2004/01/17 05:03:31 bug1 Exp $
838 */ 838 */
839 839
840 840