-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tcpclient: added -e option to get remote host address from /etc/hosts
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
Showing
9 changed files
with
338 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.