Skip to content

Commit

Permalink
tcpclient: added -e option to get remote host address from /etc/hosts
Browse files Browse the repository at this point in the history
1. tcpclient.c: added -e option to get remote host address from /etc/hosts
2. getip.c: new function getip() which uses getaddrinfo() to get remote host
   address
3. tcpserver.c: use INET6_ADDRLENSTRLEN for AF_INET6
  • Loading branch information
mbhangui committed Oct 5, 2024
1 parent 5984044 commit 6b7ba43
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 84 deletions.
9 changes: 7 additions & 2 deletions ucspi-tcp-x/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ warn-auto.sh tcpcat.sh conf-prefix

tcpclient: \
load tcpclient.o tcpremoteinfo.o timeoutconn.o \
upathexec_run.o dns.a unix.a socket.lib
upathexec_run.o getip.o dns.a unix.a socket.lib
./load tcpclient tcpremoteinfo.o timeoutconn.o \
upathexec_run.o dns.a unix.a \
upathexec_run.o getip.o dns.a unix.a \
$(SSLLIBS) `cat socket.lib` -lqmail

tcpclient.o: \
Expand All @@ -406,6 +406,11 @@ haveip6.h ip6.h conf-ip conf-tls
./compile \
`grep -h -v "^#" conf-ip conf-tls 2>/dev/null` tcpclient.c

getip.o: \
compile getip.c getip.h haveip6.h conf-ip
./compile \
`grep -h -v "^#" conf-ip 2>/dev/null` getip.c

dotls: \
load dotls.o upathexec_run.o upathexec_env.o
./load dotls upathexec_run.o \
Expand Down
10 changes: 5 additions & 5 deletions ucspi-tcp-x/dns_ipq.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "dns.h"

static int
doit(stralloc * work, char *rule)
doit(stralloc *work, char *rule)
{
char ch;
unsigned int colon;
Expand Down Expand Up @@ -56,9 +56,9 @@ doit(stralloc * work, char *rule)

int
#ifdef IPV6
dns_ip6_qualify_rules(stralloc * out, stralloc * fqdn, stralloc *in, stralloc *rules)
dns_ip6_qualify_rules(stralloc *out, stralloc *fqdn, stralloc *in, stralloc *rules)
#else
dns_ip4_qualify_rules(stralloc * out, stralloc * fqdn, stralloc * in, stralloc * rules)
dns_ip4_qualify_rules(stralloc *out, stralloc *fqdn, stralloc *in, stralloc *rules)
#endif
{
unsigned int i;
Expand Down Expand Up @@ -105,9 +105,9 @@ dns_ip4_qualify_rules(stralloc * out, stralloc * fqdn, stralloc * in, stralloc *

int
#ifdef IPV6
dns_ip6_qualify(stralloc * out, stralloc * fqdn, stralloc *in)
dns_ip6_qualify(stralloc *out, stralloc *fqdn, stralloc *in)
#else
dns_ip4_qualify(stralloc * out, stralloc * fqdn, stralloc * in)
dns_ip4_qualify(stralloc *out, stralloc *fqdn, stralloc *in)
#endif
{
static stralloc rules;
Expand Down
6 changes: 6 additions & 0 deletions ucspi-tcp-x/doc/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Release 1.1.4-1.1 Start 08/02/2024 End 06/08/2024
- 05/09/2024
08. tcpclient.c: added starttls capability for imap
09. dotls.c: added starttls handler for imap
- 04/10/2024
10. tcpclient.c: added -e option to get remote host address from /etc/hosts
11. getip.c: new function getip() which uses getaddrinfo() to get remote host
address
- 05/10/2024
12. tcpserver.c: use INET6_ADDRLENSTRLEN for AF_INET6

* Mon Jan 01 2024 19:01:07 +0000 Manvendra Bhangui <[email protected]> 1.1.3-1.1${?dist}
Release 1.1.3-1.1 Start 27/04/2023 End 01/01/2024
Expand Down
158 changes: 158 additions & 0 deletions ucspi-tcp-x/getip.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* $Id: getip.c,v 1.1 2024-10-05 22:30:15+05:30 Cprogrammer Exp mbhangui $
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <byte.h>
#include <stralloc.h>
#include <strerr.h>
#include "haveip6.h"
#include "ip4.h"
#ifdef IPV6
#include "ip6.h"
#endif

int
isip(const char *addr)
{
int i;
#if defined(LIBC_HAS_IP6) && defined(IPV6)
struct in_addr addr4;
struct in6_addr addr6;

if ((i = inet_pton(AF_INET, addr, &addr4)) == 1)
return 1;
else
if (i == -1)
return -1;
if ((i = inet_pton(AF_INET6, addr, &addr6)) == 1)
return 1;
else
if (i == -1)
return -1;
#else
struct in_addr addr4;

if ((i = inet_aton(addr, &addr4)) == 1)
return 1;
else
if (i == -1)
return -1;
#endif
return 0;
}

int
getip(const char *host, stralloc *insa)
{
int i;
struct in_addr addr4;
#if defined(LIBC_HAS_IP6) && defined(IPV6)
char addrBuf[INET6_ADDRSTRLEN];
struct addrinfo hints = {0}, *addr_res = 0, *addr_res0 = 0;
struct sockaddr sa;
struct in6_addr addr6;
struct sockaddr_in *in4 = (struct sockaddr_in *) &sa;
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sa;
#else
char **ptr;
struct hostent *hp;
#endif

insa->len = 0;
#if defined(LIBC_HAS_IP6) && defined(IPV6)
if ((i = inet_pton(AF_INET, host, &addr4)) == 1) {
if (!stralloc_ready(insa, 16))
strerr_die1x(111, "tcpclient: fatal: out of memory");
insa->len = 16;
byte_copy(insa->s, 12, (char *) V4mappedprefix);
byte_copy(insa->s + 12, 4, (char *) &addr4);
return 0;
} else
if (i == -1)
strerr_die1x(111, "tcpclient: fatal: invalid address family AF_INET");
if ((i = inet_pton(AF_INET6, host, &addr6)) == 1) {
if (!stralloc_ready(insa, 16))
strerr_die1x(111, "tcpclient: fatal: out of memory");
insa->len = 16;
byte_copy(insa->s, 16, (char *) &addr6);
return 0;
} else
if (i == -1)
strerr_die1x(111, "tcpclient: fatal: invalid address family AF_INET6");
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = 0;
hints.ai_addr = 0;
hints.ai_next = 0;
if ((i = getaddrinfo(host, 0, &hints, &addr_res0))) {
if (i == EAI_NONAME)
return 1;
strerr_die2x(111, "tcpclient: fatal: getaddrinfo: ", gai_strerror(i));
}
for (addr_res = addr_res0; addr_res; addr_res = addr_res->ai_next) {
byte_copy((char *) &sa, addr_res->ai_addrlen, (char *) addr_res->ai_addr);
if (sa.sa_family == AF_INET) {
in4 = (struct sockaddr_in *) &sa;
if (!inet_ntop(AF_INET, (void *) &in4->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
freeaddrinfo(addr_res0);
strerr_die1sys(111, "tcpclient: fatal: inet_ntop");
}
if (!stralloc_ready(insa, insa->len + 16))
strerr_die1x(111, "tcpclient: fatal: out of memory");
byte_copy(insa->s + insa->len, 12, (char *) V4mappedprefix);
byte_copy(insa->s + insa->len + 12, 4, (char *) &in4->sin_addr);
insa->len += 16;
} else
if (sa.sa_family == AF_INET6) {
in6 = (struct sockaddr_in6 *) &sa;
if (!inet_ntop(AF_INET6, (void *) &in6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
freeaddrinfo(addr_res0);
strerr_die1sys(111, "tcpclient: fatal: inet_ntop");
}
if (!stralloc_ready(insa, insa->len + 16))
strerr_die1x(111, "tcpclient: fatal: out of memory");
byte_copy(insa->s + insa->len, 16, (char *) &in6->sin6_addr);
insa->len += 16;
} else
continue;
}
freeaddrinfo(addr_res0);
return insa->len ? 0 : 1;
#else
if ((i = inet_aton(host, &addr4)) == 1) {
if (!stralloc_ready(insa, 4))
strerr_die1x(111, "tcpclient: fatal: out of memory");
insa->len = 4;
byte_copy(insa->s, 4, (char *) &addr4);
return 0;
} else
if (i == -1)
return -1;
if (!(hp = gethostbyname(host))) {
if (h_errno == HOST_NOT_FOUND)
return 1;
else
strerr_die2sys(111, "tcpclient: fatal: gethostbyname: ", hstrerror(h_errno));
}
for (ptr = hp->h_addr_list; addr_list[i]; ptr++) {
if (!stralloc_ready(insa, insa->len + 4))
strerr_die1x(111, "tcpclient: fatal: out of memory");
byte_copy(insa->s + insa->len, 4, *ptr);
insa->len += 4;
}
return 0;
#endif
}

/*
* $Log: getip.c,v $
* Revision 1.1 2024-10-05 22:30:15+05:30 Cprogrammer
* Initial revision
*
*/
15 changes: 15 additions & 0 deletions ucspi-tcp-x/getip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* $Log: getip.h,v $
* Revision 1.1 2024-10-05 22:55:32+05:30 Cprogrammer
* Initial revision
*
*/
#ifndef GETIP_H
#define GETIP_H

#include <stralloc.h>

int getip(const char *, stralloc *);
int isip(const char *);

#endif
4 changes: 2 additions & 2 deletions ucspi-tcp-x/ip6_bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static char strnum[FMT_ULONG];
*/

int
ip6tobitstring(char *ip6address, stralloc * ip6string, unsigned int prefix)
ip6tobitstring(char *ip6address, stralloc *ip6string, unsigned int prefix)
{
char ip6[16];
int bit, octettbitpos, number, shiftedvalue;
Expand Down Expand Up @@ -97,7 +97,7 @@ ip6tobitstring(char *ip6address, stralloc * ip6string, unsigned int prefix)
*/

int
bitstringtoip6(stralloc * ip6string, stralloc * ip6addr)
bitstringtoip6(stralloc *ip6string, stralloc *ip6addr)
{
int j = 0;
int i = 0;
Expand Down
15 changes: 11 additions & 4 deletions ucspi-tcp-x/tcpclient.9
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ option.

The server's address is given by a \fIsocket\fR path or \fIhost\fR and
\fIport\fR. \fIport\fR may be a name from /etc/services or a number.
\fIhost\fR may be 0, referring to the local machine, or a dotted-decimal IP
address or IPv6 address, or a host name; it is fed through qualification
using dns_ip4_qualify.
\fIhost\fR may be 0, referring to the local machine, or a dotted-decimal
IPv4/IPv6 address or a host name. If the address is not an IPv4/IPv6
address or if the '-e' option is specified, the address is fed through
qualification using dns_ip6_qualify/dns_ip4_qualify.

If the server has several IP addresses, \fBtcpclient\fR tries each address
in turn.
in turn until it gets connected.

.SH OPTIONS
\fBGeneral options\fR:
Expand Down Expand Up @@ -200,6 +201,12 @@ variable $TCPREMOTEHOST. This option is not used for UNIX domain sockets.
Do not look up the remote host name in DNS; remove the environment
variable $TCPREMOTEHOST. This option is not used for UNIX domain sockets.

.TP
.B \-e
Lookup remote host name in \fI/etc/hosts\fR. This option also bypasses
feeding remote address for qualification using
dns_ip6_qualify/dns_ip4_qualify.

.TP
.B \-l \fIlocalname
Do not look up the local host name in DNS; use
Expand Down
Loading

0 comments on commit 6b7ba43

Please sign in to comment.