Modify bb_lookup_port to allow the protocol to be specified, allowing

/etc/services support for inetd, netcat and tftp.
This commit is contained in:
Glenn L McGrath 2004-01-17 05:03:31 +00:00
parent b03be7f567
commit 036dbaa082
8 changed files with 22 additions and 31 deletions

View File

@ -297,7 +297,7 @@ extern struct hostent *xgethostbyname2(const char *name, int af);
extern int create_icmp_socket(void); extern int create_icmp_socket(void);
extern int create_icmp6_socket(void); extern int create_icmp6_socket(void);
extern int xconnect(struct sockaddr_in *s_addr); extern int xconnect(struct sockaddr_in *s_addr);
extern unsigned short bb_lookup_port(const char *port, unsigned short default_port); extern unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port);
extern void bb_lookup_host(struct sockaddr_in *s_in, const char *host); extern void bb_lookup_host(struct sockaddr_in *s_in, const char *host);
//#warning wrap this? //#warning wrap this?

View File

@ -23,7 +23,7 @@
* If "port" is a name it is looked up in /etc/services, if it isnt found return * If "port" is a name it is looked up in /etc/services, if it isnt found return
* default_port * default_port
*/ */
unsigned short bb_lookup_port(const char *port, unsigned short default_port) unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port)
{ {
unsigned short port_nr = htons(default_port); unsigned short port_nr = htons(default_port);
if (port) { if (port) {
@ -37,7 +37,7 @@ unsigned short bb_lookup_port(const char *port, unsigned short default_port)
errno = 0; errno = 0;
port_long = strtol(port, &endptr, 10); port_long = strtol(port, &endptr, 10);
if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) { if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
struct servent *tserv = getservbyname(port, "tcp"); struct servent *tserv = getservbyname(port, protocol);
if (tserv) { if (tserv) {
port_nr = tserv->s_port; port_nr = tserv->s_port;
} }

View File

@ -349,7 +349,7 @@ int ftpgetput_main(int argc, char **argv)
* and we want to connect to only one IP... */ * and we want to connect to only one IP... */
server->s_in = &s_in; server->s_in = &s_in;
bb_lookup_host(&s_in, argv[optind]); bb_lookup_host(&s_in, argv[optind]);
s_in.sin_port = bb_lookup_port(port, 21); s_in.sin_port = bb_lookup_port(port, "tcp", 21);
if (verbose_flag) { if (verbose_flag) {
printf("Connecting to %s[%s]:%d\n", printf("Connecting to %s[%s]:%d\n",
argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port)); argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));

View File

@ -610,20 +610,14 @@ static void config(int signum)
sep->se_ctrladdr_in.sin_family = AF_INET; sep->se_ctrladdr_in.sin_family = AF_INET;
sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in; sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
{ {
u_short port = htons(atoi(sep->se_service)); u_short port = bb_lookup_port(sep->se_service, sep->se_proto, 0);
if (!port) { if (port == 0) {
struct servent *sp;
sp = getservbyname(sep->se_service,
sep->se_proto);
if (sp == 0) {
syslog(LOG_ERR, syslog(LOG_ERR,
"%s/%s: unknown service", "%s/%s: unknown service",
sep->se_service, sep->se_proto); sep->se_service, sep->se_proto);
continue; continue;
} }
port = sp->s_port;
}
if (port != sep->se_ctrladdr_in.sin_port) { if (port != sep->se_ctrladdr_in.sin_port) {
sep->se_ctrladdr_in.sin_port = port; sep->se_ctrladdr_in.sin_port = port;
if (sep->se_fd != -1) { if (sep->se_fd != -1) {

View File

@ -61,7 +61,7 @@ int nc_main(int argc, char **argv)
do_listen++; do_listen++;
break; break;
case 'p': case 'p':
lport = atoi(optarg); lport = bb_lookup_port(optarg, "tcp", 0);
break; break;
case 'i': case 'i':
delay = atoi(optarg); delay = atoi(optarg);
@ -96,7 +96,7 @@ int nc_main(int argc, char **argv)
if (lport != 0) { if (lport != 0) {
memset(&address.sin_addr, 0, sizeof(address.sin_addr)); memset(&address.sin_addr, 0, sizeof(address.sin_addr));
address.sin_port = htons(lport); address.sin_port = lport;
if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
bb_perror_msg_and_die("bind"); bb_perror_msg_and_die("bind");
@ -117,7 +117,7 @@ int nc_main(int argc, char **argv)
hostinfo = xgethostbyname(argv[optind]); hostinfo = xgethostbyname(argv[optind]);
address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
address.sin_port = htons(atoi(argv[optind+1])); address.sin_port = bb_lookup_port(argv[optind+1], "tcp", 0);
if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
bb_perror_msg_and_die("connect"); bb_perror_msg_and_die("connect");

View File

@ -599,7 +599,7 @@ extern int telnet_main(int argc, char** argv)
bb_show_usage(); bb_show_usage();
bb_lookup_host(&s_in, argv[1]); bb_lookup_host(&s_in, argv[1]);
s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", 23); s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
G.netfd = xconnect(&s_in); G.netfd = xconnect(&s_in);

View File

@ -138,7 +138,7 @@ static char *tftp_option_get(char *buf, int len, char *option)
#endif #endif
static inline int tftp(const int cmd, const struct hostent *host, static inline int tftp(const int cmd, const struct hostent *host,
const char *remotefile, int localfd, const int port, int tftp_bufsize) const char *remotefile, int localfd, const unsigned short port, int tftp_bufsize)
{ {
const int cmd_get = cmd & tftp_cmd_get; const int cmd_get = cmd & tftp_cmd_get;
const int cmd_put = cmd & tftp_cmd_put; const int cmd_put = cmd & tftp_cmd_put;
@ -179,7 +179,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
bind(socketfd, (struct sockaddr *)&sa, len); bind(socketfd, (struct sockaddr *)&sa, len);
sa.sin_family = host->h_addrtype; sa.sin_family = host->h_addrtype;
sa.sin_port = htons(port); sa.sin_port = port;
memcpy(&sa.sin_addr, (struct in_addr *) host->h_addr, memcpy(&sa.sin_addr, (struct in_addr *) host->h_addr,
sizeof(sa.sin_addr)); sizeof(sa.sin_addr));
@ -332,7 +332,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
timeout = 0; timeout = 0;
if (sa.sin_port == htons(port)) { if (sa.sin_port == port) {
sa.sin_port = from.sin_port; sa.sin_port = from.sin_port;
} }
if (sa.sin_port == from.sin_port) { if (sa.sin_port == from.sin_port) {
@ -487,7 +487,7 @@ int tftp_main(int argc, char **argv)
struct hostent *host = NULL; struct hostent *host = NULL;
char *localfile = NULL; char *localfile = NULL;
char *remotefile = NULL; char *remotefile = NULL;
int port = 69; int port;
int cmd = 0; int cmd = 0;
int fd = -1; int fd = -1;
int flags = 0; int flags = 0;
@ -564,10 +564,7 @@ int tftp_main(int argc, char **argv)
} }
host = xgethostbyname(argv[optind]); host = xgethostbyname(argv[optind]);
port = bb_lookup_port(argv[optind + 1], "udp", 69);
if (optind + 2 == argc) {
port = atoi(argv[optind + 1]);
}
#ifdef CONFIG_FEATURE_TFTP_DEBUG #ifdef CONFIG_FEATURE_TFTP_DEBUG
printf("using server \"%s\", remotefile \"%s\", " printf("using server \"%s\", remotefile \"%s\", "

View File

@ -537,11 +537,11 @@ void parse_url(char *url, struct host_info *h)
char *cp, *sp, *up, *pp; char *cp, *sp, *up, *pp;
if (strncmp(url, "http://", 7) == 0) { if (strncmp(url, "http://", 7) == 0) {
h->port = bb_lookup_port("http", 80); h->port = bb_lookup_port("http", "tcp", 80);
h->host = url + 7; h->host = url + 7;
h->is_ftp = 0; h->is_ftp = 0;
} else if (strncmp(url, "ftp://", 6) == 0) { } else if (strncmp(url, "ftp://", 6) == 0) {
h->port = bb_lookup_port("ftp", 21); h->port = bb_lookup_port("ftp", "tfp", 21);
h->host = url + 6; h->host = url + 6;
h->is_ftp = 1; h->is_ftp = 1;
} else } else
@ -834,7 +834,7 @@ progressmeter(int flag)
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: wget.c,v 1.64 2003/12/27 00:21:47 bug1 Exp $ * $Id: wget.c,v 1.65 2004/01/17 05:03:31 bug1 Exp $
*/ */