aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2011-02-12 17:37:05 +0100
committerAndreas Schneider <asn@cryptomilk.org>2011-02-12 19:08:59 +0100
commitb313fa944a5ab6e0eb935601cfdf60cf9008d06c (patch)
treeb80a43784122c5ae876e65a8c39b6b48ac28c69f
parent7acc2fa607d8242372f84266f90a25bd7000e587 (diff)
downloadlibssh-b313fa944a5ab6e0eb935601cfdf60cf9008d06c.tar.gz
libssh-b313fa944a5ab6e0eb935601cfdf60cf9008d06c.tar.xz
libssh-b313fa944a5ab6e0eb935601cfdf60cf9008d06c.zip
misc: Added ssh_is_ipaddr() function.
-rw-r--r--include/libssh/misc.h2
-rw-r--r--src/misc.c32
2 files changed, 34 insertions, 0 deletions
diff --git a/include/libssh/misc.h b/include/libssh/misc.h
index a2f0239a..3ee26a3c 100644
--- a/include/libssh/misc.h
+++ b/include/libssh/misc.h
@@ -31,6 +31,8 @@ int ssh_file_readaccess_ok(const char *file);
char *ssh_path_expand_tilde(const char *d);
char *ssh_path_expand_escape(ssh_session session, const char *s);
int ssh_analyze_banner(ssh_session session, int server, int *ssh1, int *ssh2);
+int ssh_is_ipaddr_v4(const char *str);
+int ssh_is_ipaddr(const char *str);
/* macro for byte ordering */
uint64_t ntohll(uint64_t);
diff --git a/src/misc.c b/src/misc.c
index 2daa5f27..c0e62fa2 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -779,6 +779,38 @@ int ssh_analyze_banner(ssh_session session, int server, int *ssh1, int *ssh2) {
return 0;
}
+/* TODO for Windows, inet_pton is only available on Vista and newer */
+int ssh_is_ipaddr_v4(const char *str) {
+#ifndef _WIN32
+ int rc = -1;
+ struct in_addr dest;
+
+ rc = inet_pton(AF_INET, str, &dest);
+ if (rc > 0) {
+ return 1;
+ }
+#endif
+ return 0;
+}
+
+/* TODO for Windows, inet_pton is only available on Vista and newer */
+int ssh_is_ipaddr(const char *str) {
+#ifndef _WIN32
+ int rc = -1;
+
+ if (strchr(str, ':')) {
+ struct in6_addr dest6;
+
+ /* TODO link-local (IP:v6:addr%ifname). */
+ rc = inet_pton(AF_INET6, str, &dest6);
+ if (rc > 0) {
+ return 1;
+ }
+ }
+#endif
+ return ssh_is_ipaddr_v4(str);
+}
+
/** @} */
/* vim: set ts=4 sw=4 et cindent: */