1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
From 24a4b1328b9cd1dbeff6f17f0034fd432784dcc4 Mon Sep 17 00:00:00 2001
From: Jonathan Nieder <jrnieder@gmail.com>
Date: Mon, 6 Jun 2011 04:39:29 -0500
Subject: [PATCH 07/12] daemon: move locate_host() to tcp.c
Keep the different name resolution functions close together so they
can learn from each other and perhaps share code in the future.
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Makefile | 2 +-
daemon.c | 66 ++------------------------------------------------------------
tcp.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++
tcp.h | 3 +++
4 files changed, 63 insertions(+), 65 deletions(-)
diff --git a/Makefile b/Makefile
index b06c60d..d321ad4 100644
--- a/Makefile
+++ b/Makefile
@@ -2168,7 +2168,7 @@ builtin/prune.o builtin/reflog.o reachable.o: reachable.h
builtin/commit.o builtin/revert.o wt-status.o: wt-status.h
builtin/tar-tree.o archive-tar.o: tar.h
connect.o transport.o url.o http-backend.o: url.h
-connect.o tcp.o: tcp.h
+connect.o daemon.o tcp.o: tcp.h
http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h
http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h
diff --git a/daemon.c b/daemon.c
index 93356d6..fe8883f 100644
--- a/daemon.c
+++ b/daemon.c
@@ -1,14 +1,11 @@
#include "cache.h"
#include "pkt-line.h"
+#include "tcp.h"
#include "exec_cmd.h"
#include "run-command.h"
#include "strbuf.h"
#include "string-list.h"
-#ifndef HOST_NAME_MAX
-#define HOST_NAME_MAX 256
-#endif
-
#ifndef NI_MAXSERV
#define NI_MAXSERV 32
#endif
@@ -441,65 +438,6 @@ static void parse_host_and_port(char *hostport, char **host,
}
}
-#ifndef NO_IPV6
-
-static void locate_host(const char *hostname, char **ip_address,
- char **canon_hostname)
-{
- struct addrinfo hints;
- struct addrinfo *ai;
- int gai;
- static char addrbuf[HOST_NAME_MAX + 1];
- struct sockaddr_in *sin_addr;
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_flags = AI_CANONNAME;
-
- gai = getaddrinfo(hostname, NULL, &hints, &ai);
- if (gai)
- return;
-
- sin_addr = (void *)ai->ai_addr;
- inet_ntop(AF_INET, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
- free(*ip_address);
- *ip_address = xstrdup(addrbuf);
-
- free(*canon_hostname);
- *canon_hostname = xstrdup(ai->ai_canonname ?
- ai->ai_canonname : *ip_address);
-
- freeaddrinfo(ai);
-}
-
-#else
-
-static void locate_host(const char *hostname, char **ip_address,
- char **canon_hostname)
-{
- struct hostent *hent;
- struct sockaddr_in sa;
- char **ap;
- static char addrbuf[HOST_NAME_MAX + 1];
-
- hent = gethostbyname(hostname);
-
- ap = hent->h_addr_list;
- memset(&sa, 0, sizeof sa);
- sa.sin_family = hent->h_addrtype;
- sa.sin_port = htons(0);
- memcpy(&sa.sin_addr, *ap, hent->h_length);
-
- inet_ntop(hent->h_addrtype, &sa.sin_addr,
- addrbuf, sizeof(addrbuf));
-
- free(*canon_hostname);
- *canon_hostname = xstrdup(hent->h_name);
- free(*ip_address);
- *ip_address = xstrdup(addrbuf);
-}
-
-#endif
-
/*
* Read the host as supplied by the client connection.
*/
@@ -539,7 +477,7 @@ static void parse_host_arg(char *extra_args, int buflen)
* if possible.
*/
if (hostname)
- locate_host(hostname, &ip_address, &canon_hostname);
+ git_locate_host(hostname, &ip_address, &canon_hostname);
}
diff --git a/tcp.c b/tcp.c
index f5e1ab3..9263e0d 100644
--- a/tcp.c
+++ b/tcp.c
@@ -1,6 +1,10 @@
#include "cache.h"
#include "run-command.h"
+#ifndef HOST_NAME_MAX
+#define HOST_NAME_MAX 256
+#endif
+
#define STR_(s) # s
#define STR(s) STR_(s)
@@ -47,6 +51,34 @@ static const char *ai_name(const struct addrinfo *ai)
return addr;
}
+void git_locate_host(const char *hostname, char **ip_address,
+ char **canon_hostname)
+{
+ struct addrinfo hints;
+ struct addrinfo *ai;
+ int gai;
+ static char addrbuf[HOST_NAME_MAX + 1];
+ struct sockaddr_in *sin_addr;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_flags = AI_CANONNAME;
+
+ gai = getaddrinfo(hostname, NULL, &hints, &ai);
+ if (gai)
+ return;
+
+ sin_addr = (void *)ai->ai_addr;
+ inet_ntop(AF_INET, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
+ free(*ip_address);
+ *ip_address = xstrdup(addrbuf);
+
+ free(*canon_hostname);
+ *canon_hostname = xstrdup(ai->ai_canonname ?
+ ai->ai_canonname : *ip_address);
+
+ freeaddrinfo(ai);
+}
+
/*
* Returns a connected socket() fd, or else die()s.
*/
@@ -111,6 +143,31 @@ static int git_tcp_connect_sock(char *host, int flags)
#else /* NO_IPV6 */
+void git_locate_host(const char *hostname, char **ip_address,
+ char **canon_hostname)
+{
+ struct hostent *hent;
+ struct sockaddr_in sa;
+ char **ap;
+ static char addrbuf[HOST_NAME_MAX + 1];
+
+ hent = gethostbyname(hostname);
+
+ ap = hent->h_addr_list;
+ memset(&sa, 0, sizeof sa);
+ sa.sin_family = hent->h_addrtype;
+ sa.sin_port = htons(0);
+ memcpy(&sa.sin_addr, *ap, hent->h_length);
+
+ inet_ntop(hent->h_addrtype, &sa.sin_addr,
+ addrbuf, sizeof(addrbuf));
+
+ free(*canon_hostname);
+ *canon_hostname = xstrdup(hent->h_name);
+ free(*ip_address);
+ *ip_address = xstrdup(addrbuf);
+}
+
/*
* Returns a connected socket() fd, or else die()s.
*/
diff --git a/tcp.h b/tcp.h
index 4de5f71..bed3cdc 100644
--- a/tcp.h
+++ b/tcp.h
@@ -1,6 +1,9 @@
#ifndef TCP_H
#define TCP_H
+extern void git_locate_host(const char *hostname,
+ char **ip_address, char **canon_hostname);
+
extern int git_use_proxy(const char *host);
extern void git_tcp_connect(int fd[2], char *host, int flags);
extern struct child_process *git_proxy_connect(int fd[2], char *host);
--
1.7.10
|