From d1a69d0fff03dcd4326adb63aee6464be0cad83f Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Tue, 6 Nov 2012 20:16:53 -0200 Subject: [PATCH 1/2] Fix compilation for mingw32 and cygwin inet_pton is available only in windows vista or later, fixed the issue by reimplementing it using WSAStringToAddress --- src/netops.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- src/netops.h | 1 + 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/netops.c b/src/netops.c index 3e2743486..6d7d2c1c1 100644 --- a/src/netops.c +++ b/src/netops.c @@ -274,11 +274,11 @@ static int verify_server_cert(gitno_ssl *ssl, const char *host) } /* Try to parse the host as an IP address to see if it is */ - if (inet_pton(AF_INET, host, &addr4)) { + if (gitno_inet_pton(AF_INET, host, &addr4)) { type = GEN_IPADD; addr = &addr4; } else { - if(inet_pton(AF_INET6, host, &addr6)) { + if(gitno_inet_pton(AF_INET6, host, &addr6)) { type = GEN_IPADD; addr = &addr6; } @@ -597,3 +597,51 @@ int gitno_extract_host_and_port(char **host, char **port, const char *url, const return 0; } + +int gitno_inet_pton(int af, const char* src, void* dst) +{ + /* inet_pton is only available in Windows Vista or later + * mingw32 and cygwin give compile errors */ +#ifndef GIT_WIN32 + return inet_pton(af, src, dst); +#else + union { + struct sockaddr_in6 sin6; + struct sockaddr_in sin; + } sa; + size_t srcsize; + + switch(af) + { + case AF_INET: + sa.sin.sin_family = AF_INET; + srcsize = sizeof (sa.sin); + break; + case AF_INET6: + sa.sin6.sin6_family = AF_INET6; + srcsize = sizeof (sa.sin6); + break; + default: + errno = WSAEPFNOSUPPORT; + return -1; + } + + if (WSAStringToAddress(src, af, NULL, (struct sockaddr *) &sa, &srcsize) != 0) + { + errno = WSAGetLastError(); + return -1; + } + + switch(af) + { + case AF_INET: + memcpy(dst, &sa.sin.sin_addr, sizeof(sa.sin.sin_addr)); + break; + case AF_INET6: + memcpy(dst, &sa.sin6.sin6_addr, sizeof(sa.sin6.sin6_addr)); + break; + } + + return 1; +#endif +} diff --git a/src/netops.h b/src/netops.h index efbbc65a4..b2a793081 100644 --- a/src/netops.h +++ b/src/netops.h @@ -67,5 +67,6 @@ int gitno_close(gitno_socket *s); int gitno_select_in(gitno_buffer *buf, long int sec, long int usec); int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port); +int gitno_inet_pton(int af, const char *src, void *dst); #endif From 345eef23741b98636ab7ac3b1a12fa5178d5912b Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Wed, 7 Nov 2012 16:10:57 -0200 Subject: [PATCH 2/2] Move inet_pton to posix platform-compatibility layer --- src/netops.c | 52 ++----------------------------------------- src/netops.h | 1 - src/posix.c | 2 ++ src/unix/posix.h | 1 + src/win32/posix.h | 1 + src/win32/posix_w32.c | 44 +++++++++++++++++++++++++++++++++++- 6 files changed, 49 insertions(+), 52 deletions(-) diff --git a/src/netops.c b/src/netops.c index 6d7d2c1c1..fa4a729bd 100644 --- a/src/netops.c +++ b/src/netops.c @@ -274,11 +274,11 @@ static int verify_server_cert(gitno_ssl *ssl, const char *host) } /* Try to parse the host as an IP address to see if it is */ - if (gitno_inet_pton(AF_INET, host, &addr4)) { + if (p_inet_pton(AF_INET, host, &addr4)) { type = GEN_IPADD; addr = &addr4; } else { - if(gitno_inet_pton(AF_INET6, host, &addr6)) { + if(p_inet_pton(AF_INET6, host, &addr6)) { type = GEN_IPADD; addr = &addr6; } @@ -597,51 +597,3 @@ int gitno_extract_host_and_port(char **host, char **port, const char *url, const return 0; } - -int gitno_inet_pton(int af, const char* src, void* dst) -{ - /* inet_pton is only available in Windows Vista or later - * mingw32 and cygwin give compile errors */ -#ifndef GIT_WIN32 - return inet_pton(af, src, dst); -#else - union { - struct sockaddr_in6 sin6; - struct sockaddr_in sin; - } sa; - size_t srcsize; - - switch(af) - { - case AF_INET: - sa.sin.sin_family = AF_INET; - srcsize = sizeof (sa.sin); - break; - case AF_INET6: - sa.sin6.sin6_family = AF_INET6; - srcsize = sizeof (sa.sin6); - break; - default: - errno = WSAEPFNOSUPPORT; - return -1; - } - - if (WSAStringToAddress(src, af, NULL, (struct sockaddr *) &sa, &srcsize) != 0) - { - errno = WSAGetLastError(); - return -1; - } - - switch(af) - { - case AF_INET: - memcpy(dst, &sa.sin.sin_addr, sizeof(sa.sin.sin_addr)); - break; - case AF_INET6: - memcpy(dst, &sa.sin6.sin6_addr, sizeof(sa.sin6.sin6_addr)); - break; - } - - return 1; -#endif -} diff --git a/src/netops.h b/src/netops.h index b2a793081..efbbc65a4 100644 --- a/src/netops.h +++ b/src/netops.h @@ -67,6 +67,5 @@ int gitno_close(gitno_socket *s); int gitno_select_in(gitno_buffer *buf, long int sec, long int usec); int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port); -int gitno_inet_pton(int af, const char *src, void *dst); #endif diff --git a/src/posix.c b/src/posix.c index 985221dd5..d207ce1a0 100644 --- a/src/posix.c +++ b/src/posix.c @@ -205,3 +205,5 @@ int p_write(git_file fd, const void *buf, size_t cnt) } return 0; } + + diff --git a/src/unix/posix.h b/src/unix/posix.h index bcd800301..f6f2e2353 100644 --- a/src/unix/posix.h +++ b/src/unix/posix.h @@ -21,5 +21,6 @@ #define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__) #define p_mkstemp(p) mkstemp(p) #define p_setenv(n,v,o) setenv(n,v,o) +#define p_inet_pton(a, b, c) inet_pton(a, b, c) #endif diff --git a/src/win32/posix.h b/src/win32/posix.h index 80dcca5c1..d99864d05 100644 --- a/src/win32/posix.h +++ b/src/win32/posix.h @@ -48,5 +48,6 @@ extern int p_getcwd(char *buffer_out, size_t size); extern int p_rename(const char *from, const char *to); extern int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags); extern int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags); +extern int p_inet_pton(int af, const char* src, void* dst); #endif diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c index 649fe9b95..557f4f3bf 100644 --- a/src/win32/posix_w32.c +++ b/src/win32/posix_w32.c @@ -11,7 +11,7 @@ #include #include #include - +#include int p_unlink(const char *path) { @@ -504,3 +504,45 @@ int p_gettimeofday(struct timeval *tv, struct timezone *tz) return 0; } + +int p_inet_pton(int af, const char* src, void* dst) +{ + union { + struct sockaddr_in6 sin6; + struct sockaddr_in sin; + } sa; + size_t srcsize; + + switch(af) + { + case AF_INET: + sa.sin.sin_family = AF_INET; + srcsize = sizeof (sa.sin); + break; + case AF_INET6: + sa.sin6.sin6_family = AF_INET6; + srcsize = sizeof (sa.sin6); + break; + default: + errno = WSAEPFNOSUPPORT; + return -1; + } + + if (WSAStringToAddress(src, af, NULL, (struct sockaddr *) &sa, &srcsize) != 0) + { + errno = WSAGetLastError(); + return -1; + } + + switch(af) + { + case AF_INET: + memcpy(dst, &sa.sin.sin_addr, sizeof(sa.sin.sin_addr)); + break; + case AF_INET6: + memcpy(dst, &sa.sin6.sin6_addr, sizeof(sa.sin6.sin6_addr)); + break; + } + + return 1; +}