Improve the mingw getaddrinfo stub to handle more use cases
Allow the node parameter to be null, which is used for getting the default bind address. Also allow the hints parameter to be null, to improve standard conformance of the stub implementation a little. Signed-off-by: Martin Storsjo <martin@martin.st> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Martin Storsjö committed
Nov 4, 2010 at 02:35 UTC
cbefd2d6322bff0033fd50bfe175c52b79933571
1 file changed
+21
-7
compat/mingw.c
+21
-7
@@ -1039,19 +1039,22 @@ static int WSAAPI getaddrinfo_stub(const char *node, const char *service,
1039
const struct addrinfo *hints,
1040
struct addrinfo **res)
1041
{
1042
- struct hostent *h = gethostbyname(node);
1042
+ struct hostent *h = NULL;
1043
struct addrinfo *ai;
1044
struct sockaddr_in *sin;
1045
1046
- if (!h)
1047
- return WSAGetLastError();
1046
+ if (node) {
1047
+ h = gethostbyname(node);
1048
+ if (!h)
1049
+ return WSAGetLastError();
1050
+ }
1051
1052
ai = xmalloc(sizeof(struct addrinfo));
1053
*res = ai;
1054
ai->ai_flags = 0;
1055
ai->ai_family = AF_INET;
1053
- ai->ai_socktype = hints->ai_socktype;
1054
- switch (hints->ai_socktype) {
1056
+ ai->ai_socktype = hints ? hints->ai_socktype : 0;
1057
+ switch (ai->ai_socktype) {
1058
case SOCK_STREAM:
1059
ai->ai_protocol = IPPROTO_TCP;
1060
break;
@@ -1063,14 +1066,25 @@ static int WSAAPI getaddrinfo_stub(const char *node, const char *service,
1066
break;
1067
}
1068
ai->ai_addrlen = sizeof(struct sockaddr_in);
1066
- ai->ai_canonname = strdup(h->h_name);
1069
+ if (hints && (hints->ai_flags & AI_CANONNAME))
1070
+ ai->ai_canonname = h ? strdup(h->h_name) : NULL;
1071
+ else
1072
+ ai->ai_canonname = NULL;
1073
1074
sin = xmalloc(ai->ai_addrlen);
1075
memset(sin, 0, ai->ai_addrlen);
1076
sin->sin_family = AF_INET;
1077
+ /* Note: getaddrinfo is supposed to allow service to be a string,
1078
+ * which should be looked up using getservbyname. This is
1079
+ * currently not implemented */
1080
if (service)
1081
sin->sin_port = htons(atoi(service));
1073
- sin->sin_addr = *(struct in_addr *)h->h_addr;
1082
+ if (h)
1083
+ sin->sin_addr = *(struct in_addr *)h->h_addr;
1084
+ else if (hints && (hints->ai_flags & AI_PASSIVE))
1085
+ sin->sin_addr.s_addr = INADDR_ANY;
1086
+ else
1087
+ sin->sin_addr.s_addr = INADDR_LOOPBACK;
1088
ai->ai_addr = (struct sockaddr *)sin;
1089
ai->ai_next = 0;
1090
return 0;