@matheusbsbs / matheus-bagliano / commits / 72a534dab0
Fork 0

connect.c: move duplicated code to a new function 'get_host_and_port'

The following functions: git_tcp_connect_sock (IPV6 version) git_tcp_connect_sock (no IPV6 version), git_proxy_connect have common block of code. Move it to a new function 'get_host_and_port' Signed-off-by: Michael Lukashov <michael.lukashov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Lukashov committed Feb 17, 2010 at 20:56 UTC 72a534dab016813eec71935323b197650a2c0c9d
1 file changed +29 -54
connect.c
+29 -54
@@ -152,6 +152,28 @@ static enum protocol get_protocol(const char *name)
152 #define STR_(s) # s
153 #define STR(s) STR_(s)
154
155 +static void get_host_and_port(char **host, const char **port)
156 +{
157 + char *colon, *end;
158 +
159 + if (*host[0] == '[') {
160 + end = strchr(*host + 1, ']');
161 + if (end) {
162 + *end = 0;
163 + end++;
164 + (*host)++;
165 + } else
166 + end = *host;
167 + } else
168 + end = *host;
169 + colon = strchr(end, ':');
170 +
171 + if (colon) {
172 + *colon = 0;
173 + *port = colon + 1;
174 + }
175 +}
176 +
177 #ifndef NO_IPV6
178
179 static const char *ai_name(const struct addrinfo *ai)
@@ -170,30 +192,14 @@ static const char *ai_name(const struct addrinfo *ai)
192 static int git_tcp_connect_sock(char *host, int flags)
193 {
194 int sockfd = -1, saved_errno = 0;
173 - char *colon, *end;
195 const char *port = STR(DEFAULT_GIT_PORT);
196 struct addrinfo hints, *ai0, *ai;
197 int gai;
198 int cnt = 0;
199
179 - if (host[0] == '[') {
180 - end = strchr(host + 1, ']');
181 - if (end) {
182 - *end = 0;
183 - end++;
184 - host++;
185 - } else
186 - end = host;
187 - } else
188 - end = host;
189 - colon = strchr(end, ':');
190 -
191 - if (colon) {
192 - *colon = 0;
193 - port = colon + 1;
194 - if (!*port)
195 - port = "<none>";
196 - }
200 + get_host_and_port(&host, &port);
201 + if (!*port)
202 + port = "<none>";
203
204 memset(&hints, 0, sizeof(hints));
205 hints.ai_socktype = SOCK_STREAM;
@@ -251,30 +257,15 @@ static int git_tcp_connect_sock(char *host, int flags)
257 static int git_tcp_connect_sock(char *host, int flags)
258 {
259 int sockfd = -1, saved_errno = 0;
254 - char *colon, *end;
255 - char *port = STR(DEFAULT_GIT_PORT), *ep;
260 + const char *port = STR(DEFAULT_GIT_PORT);
261 + char *ep;
262 struct hostent *he;
263 struct sockaddr_in sa;
264 char **ap;
265 unsigned int nport;
266 int cnt;
267
262 - if (host[0] == '[') {
263 - end = strchr(host + 1, ']');
264 - if (end) {
265 - *end = 0;
266 - end++;
267 - host++;
268 - } else
269 - end = host;
270 - } else
271 - end = host;
272 - colon = strchr(end, ':');
273 -
274 - if (colon) {
275 - *colon = 0;
276 - port = colon + 1;
277 - }
268 + get_host_and_port(&host, &port);
269
270 if (flags & CONNECT_VERBOSE)
271 fprintf(stderr, "Looking up %s ... ", host);
@@ -406,26 +397,10 @@ static int git_use_proxy(const char *host)
397 static void git_proxy_connect(int fd[2], char *host)
398 {
399 const char *port = STR(DEFAULT_GIT_PORT);
409 - char *colon, *end;
400 const char *argv[4];
401 struct child_process proxy;
402
413 - if (host[0] == '[') {
414 - end = strchr(host + 1, ']');
415 - if (end) {
416 - *end = 0;
417 - end++;
418 - host++;
419 - } else
420 - end = host;
421 - } else
422 - end = host;
423 - colon = strchr(end, ':');
424 -
425 - if (colon) {
426 - *colon = 0;
427 - port = colon + 1;
428 - }
403 + get_host_and_port(&host, &port);
404
405 argv[0] = git_proxy_command;
406 argv[1] = host;