{"thread":{"id":"1285","subject":"[PATCH 1/2] GIT: Try all addresses for given remote name","startedAt":"2005-07-21T13:10:36Z","lastAt":"2005-07-23T00:15:50Z","messageCount":5,"participants":["YOSHIFUJI Hideaki / 吉藤英明","Petr Baudis","Sam Ravnborg"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"6292","messageId":"20050721.091036.01119516.yoshfuji@linux-ipv6.org","threadId":"1285","inReplyTo":null,"subject":"[PATCH 1/2] GIT: Try all addresses for given remote name","fromName":"YOSHIFUJI Hideaki / 吉藤英明","fromEmail":"yoshfuji@linux-ipv6.org","sentAt":"2005-07-21T13:10:36Z","receivedAt":"2005-07-21T13:10:36Z","isPatch":true,"sender":{"key":"yoshfuji@linux-ipv6.org","avatar":"https://gravatar.com/avatar/92c288bb8d3e34afd7bb1574cc1079a3d0aa6d36d80955d1ec6a1cea2868d392?d=mp&s=160"},"body":"Hello.\n\nTry all addresses for given remote name until it succeeds.\nAlso supports IPv6.\n\nSigned-of-by: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>\n\ndiff --git a/connect.c b/connect.c\n--- a/connect.c\n+++ b/connect.c\n@@ -96,42 +96,57 @@ static enum protocol get_protocol(const \n \tdie(\"I don't handle protocol '%s'\", name);\n }\n \n-static void lookup_host(const char *host, struct sockaddr *in)\n-{\n-\tstruct addrinfo *res;\n-\tint ret;\n-\n-\tret = getaddrinfo(host, NULL, NULL, &res);\n-\tif (ret)\n-\t\tdie(\"Unable to look up %s (%s)\", host, gai_strerror(ret));\n-\t*in = *res->ai_addr;\n-\tfreeaddrinfo(res);\n-}\n+#define STR_(s)\t# s\n+#define STR(s)\tSTR_(s)\n \n static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)\n {\n-\tstruct sockaddr addr;\n-\tint port = DEFAULT_GIT_PORT, sockfd;\n-\tchar *colon;\n-\n-\tcolon = strchr(host, ':');\n-\tif (colon) {\n-\t\tchar *end;\n-\t\tunsigned long n = strtoul(colon+1, &end, 0);\n-\t\tif (colon[1] && !*end) {\n-\t\t\t*colon = 0;\n-\t\t\tport = n;\n+\tint sockfd = -1;\n+\tchar *colon, *end;\n+\tchar *port = STR(DEFAULT_GIT_PORT);\n+\tstruct addrinfo hints, *ai0, *ai;\n+\tint gai;\n+\n+\tif (host[0] == '[') {\n+\t\tend = strchr(host + 1, ']');\n+\t\tif (end) {\n+\t\t\t*end = 0;\n+\t\t\tend++;\n+\t\t\thost++;\n+\t\t} else\n+\t\t\tend = host;\n+\t} else\n+\t\tend = host;\n+\tcolon = strchr(end, ':');\n+\n+\tif (colon)\n+\t\tport = colon + 1;\n+\n+\tmemset(&hints, 0, sizeof(hints));\n+\thints.ai_socktype = SOCK_STREAM;\n+\thints.ai_protocol = IPPROTO_TCP;\n+\n+\tgai = getaddrinfo(host, port, &hints, &ai);\n+\tif (gai)\n+\t\tdie(\"Unable to look up %s (%s)\", host, gai_strerror(gai));\n+\n+\tfor (ai0 = ai; ai; ai = ai->ai_next) {\n+\t\tsockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);\n+\t\tif (sockfd < 0)\n+\t\t\tcontinue;\n+\t\tif (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {\n+\t\t\tclose(sockfd);\n+\t\t\tsockfd = -1;\n+\t\t\tcontinue;\n \t\t}\n+\t\tbreak;\n \t}\n \n-\tlookup_host(host, &addr);\n-\t((struct sockaddr_in *)&addr)->sin_port = htons(port);\n+\tfreeaddrinfo(ai0);\n \n-\tsockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);\n \tif (sockfd < 0)\n \t\tdie(\"unable to create socket (%s)\", strerror(errno));\n-\tif (connect(sockfd, (void *)&addr, sizeof(addr)) < 0)\n-\t\tdie(\"unable to connect (%s)\", strerror(errno));\n+\n \tfd[0] = sockfd;\n \tfd[1] = sockfd;\n \tpacket_write(sockfd, \"%s %s\\n\", prog, path);\n\n-- \nYOSHIFUJI Hideaki @ USAGI Project  <yoshfuji@linux-ipv6.org>\nGPG-FP  : 9022 65EB 1ECF 3AD1 0BDF  80D8 4807 F894 E062 0EEA\n"},{"id":"6321","messageId":"20050722210913.GH11916@pasky.ji.cz","threadId":"1285","inReplyTo":"20050721.091036.01119516.yoshfuji@linux-ipv6.org","subject":"Re: [PATCH 1/2] GIT: Try all addresses for given remote name","fromName":"Petr Baudis","fromEmail":"pasky@suse.cz","sentAt":"2005-07-22T21:09:13Z","receivedAt":"2005-07-22T21:09:13Z","isPatch":true,"sender":{"key":"pasky@ucw.cz","avatar":"https://avatars.githubusercontent.com/u/18439?v=4"},"body":"Dear diary, on Thu, Jul 21, 2005 at 03:10:36PM CEST, I got a letter\nwhere \"YOSHIFUJI Hideaki / ?$B5HF#1QL@\" <yoshfuji@linux-ipv6.org> told me that...\n> Hello.\n\nHello,\n\n> Try all addresses for given remote name until it succeeds.\n> Also supports IPv6.\n> \n> Signed-of-by: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>\n> \n> diff --git a/connect.c b/connect.c\n> --- a/connect.c\n> +++ b/connect.c\n> @@ -96,42 +96,57 @@ static enum protocol get_protocol(const \n>  \tdie(\"I don't handle protocol '%s'\", name);\n>  }\n>  \n> -static void lookup_host(const char *host, struct sockaddr *in)\n> -{\n> -\tstruct addrinfo *res;\n> -\tint ret;\n> -\n> -\tret = getaddrinfo(host, NULL, NULL, &res);\n> -\tif (ret)\n> -\t\tdie(\"Unable to look up %s (%s)\", host, gai_strerror(ret));\n> -\t*in = *res->ai_addr;\n> -\tfreeaddrinfo(res);\n> -}\n> +#define STR_(s)\t# s\n> +#define STR(s)\tSTR_(s)\n\nUh-huh? Why two macros? Well, why any macros at all?\n\n>  static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)\n>  {\n> -\tstruct sockaddr addr;\n> -\tint port = DEFAULT_GIT_PORT, sockfd;\n> -\tchar *colon;\n> -\n> -\tcolon = strchr(host, ':');\n> -\tif (colon) {\n> -\t\tchar *end;\n> -\t\tunsigned long n = strtoul(colon+1, &end, 0);\n> -\t\tif (colon[1] && !*end) {\n> -\t\t\t*colon = 0;\n> -\t\t\tport = n;\n> +\tint sockfd = -1;\n> +\tchar *colon, *end;\n> +\tchar *port = STR(DEFAULT_GIT_PORT);\n> +\tstruct addrinfo hints, *ai0, *ai;\n> +\tint gai;\n> +\n> +\tif (host[0] == '[') {\n> +\t\tend = strchr(host + 1, ']');\n> +\t\tif (end) {\n> +\t\t\t*end = 0;\n> +\t\t\tend++;\n> +\t\t\thost++;\n> +\t\t} else\n> +\t\t\tend = host;\n> +\t} else\n> +\t\tend = host;\n> +\tcolon = strchr(end, ':');\n> +\n> +\tif (colon)\n> +\t\tport = colon + 1;\n> +\n> +\tmemset(&hints, 0, sizeof(hints));\n> +\thints.ai_socktype = SOCK_STREAM;\n> +\thints.ai_protocol = IPPROTO_TCP;\n> +\n> +\tgai = getaddrinfo(host, port, &hints, &ai);\n> +\tif (gai)\n> +\t\tdie(\"Unable to look up %s (%s)\", host, gai_strerror(gai));\n> +\n> +\tfor (ai0 = ai; ai; ai = ai->ai_next) {\n> +\t\tsockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);\n> +\t\tif (sockfd < 0)\n> +\t\t\tcontinue;\n> +\t\tif (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {\n> +\t\t\tclose(sockfd);\n> +\t\t\tsockfd = -1;\n> +\t\t\tcontinue;\n>  \t\t}\n> +\t\tbreak;\n>  \t}\n>  \n> -\tlookup_host(host, &addr);\n> -\t((struct sockaddr_in *)&addr)->sin_port = htons(port);\n> +\tfreeaddrinfo(ai0);\n>  \n> -\tsockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);\n>  \tif (sockfd < 0)\n>  \t\tdie(\"unable to create socket (%s)\", strerror(errno));\n> -\tif (connect(sockfd, (void *)&addr, sizeof(addr)) < 0)\n> -\t\tdie(\"unable to connect (%s)\", strerror(errno));\n> +\n>  \tfd[0] = sockfd;\n>  \tfd[1] = sockfd;\n>  \tpacket_write(sockfd, \"%s %s\\n\", prog, path);\n\nYou are saying that you were unable to create socket while you just were\nunable to connect.  Not any biggie, but it saves the user the trouble of\none strace after being confused by an error message. :-)\n\n-- \n\t\t\t\tPetr \"Pasky\" Baudis\nStuff: http://pasky.or.cz/\nIf you want the holes in your knowledge showing up try teaching\nsomeone.  -- Alan Cox\n"},{"id":"6324","messageId":"20050722.172629.73295196.yoshfuji@linux-ipv6.org","threadId":"1285","inReplyTo":"20050722210913.GH11916@pasky.ji.cz","subject":"Re: [PATCH 1/2] GIT: Try all addresses for given remote name","fromName":"YOSHIFUJI Hideaki / 吉藤英明","fromEmail":"yoshfuji@linux-ipv6.org","sentAt":"2005-07-22T21:26:29Z","receivedAt":"2005-07-22T21:26:29Z","isPatch":true,"sender":{"key":"yoshfuji@linux-ipv6.org","avatar":"https://gravatar.com/avatar/92c288bb8d3e34afd7bb1574cc1079a3d0aa6d36d80955d1ec6a1cea2868d392?d=mp&s=160"},"body":"In article <20050722210913.GH11916@pasky.ji.cz> (at Fri, 22 Jul 2005 23:09:13 +0200), Petr Baudis <pasky@suse.cz> says:\n\n> > -}\n> > +#define STR_(s)\t# s\n> > +#define STR(s)\tSTR_(s)\n> \n> Uh-huh? Why two macros? Well, why any macros at all?\n> \n:\n> > +\tchar *colon, *end;\n> > +\tchar *port = STR(DEFAULT_GIT_PORT);\n> > +\tstruct addrinfo hints, *ai0, *ai;\n\nThe macro is used here.\nThis is trick.\n\nAfter preprocess,\n\n/* --- cut here --- */\n#define TEST 12345\n#define STR_(s) # s\n#define STR(s) STR_(s)\n\ntest(STR(TEST));\ntest(STR_(TEST));\ntest(# TEST);\n/* --- cut here --- */\n\nbecomes\n\ntest(\"12345\");\ntest(\"TEST\");\ntest(# 12345);\n\n\n> >  \tif (sockfd < 0)\n> >  \t\tdie(\"unable to create socket (%s)\", strerror(errno));\n> > -\tif (connect(sockfd, (void *)&addr, sizeof(addr)) < 0)\n> > -\t\tdie(\"unable to connect (%s)\", strerror(errno));\n:\n> You are saying that you were unable to create socket while you just were\n> unable to connect.  Not any biggie, but it saves the user the trouble of\n> one strace after being confused by an error message. :-)\n\nIn fact, I don't think it is really worng, because it says that\nit could not create (connected) socket or endpoint of connection.\n\nAnyway, I agree that it would be confusing.\nBetter ideas / wordings?\n\n--yoshfuji\n"},{"id":"6328","messageId":"20050722213753.GK11916@pasky.ji.cz","threadId":"1285","inReplyTo":"20050722.172629.73295196.yoshfuji@linux-ipv6.org","subject":"Re: [PATCH 1/2] GIT: Try all addresses for given remote name","fromName":"Petr Baudis","fromEmail":"pasky@suse.cz","sentAt":"2005-07-22T21:37:53Z","receivedAt":"2005-07-22T21:37:53Z","isPatch":true,"sender":{"key":"pasky@ucw.cz","avatar":"https://avatars.githubusercontent.com/u/18439?v=4"},"body":"Dear diary, on Fri, Jul 22, 2005 at 11:26:29PM CEST, I got a letter\nwhere \"YOSHIFUJI Hideaki / ?$B5HF#1QL@\" <yoshfuji@linux-ipv6.org> told me that...\n> In article <20050722210913.GH11916@pasky.ji.cz> (at Fri, 22 Jul 2005 23:09:13 +0200), Petr Baudis <pasky@suse.cz> says:\n> \n> > > -}\n> > > +#define STR_(s)\t# s\n> > > +#define STR(s)\tSTR_(s)\n> > \n> > Uh-huh? Why two macros? Well, why any macros at all?\n> > \n> :\n> > > +\tchar *colon, *end;\n> > > +\tchar *port = STR(DEFAULT_GIT_PORT);\n> > > +\tstruct addrinfo hints, *ai0, *ai;\n> \n> The macro is used here.\n> This is trick.\n> \n> After preprocess,\n> \n> /* --- cut here --- */\n> #define TEST 12345\n> #define STR_(s) # s\n> #define STR(s) STR_(s)\n> \n> test(STR(TEST));\n> test(STR_(TEST));\n> test(# TEST);\n> /* --- cut here --- */\n> \n> becomes\n> \n> test(\"12345\");\n> test(\"TEST\");\n> test(# 12345);\n\nI see, I didn't know about this. Well, I didn't actually use the #\nstringificator many times. Perhaps a short comment would be due.\n\n> > >  \tif (sockfd < 0)\n> > >  \t\tdie(\"unable to create socket (%s)\", strerror(errno));\n> > > -\tif (connect(sockfd, (void *)&addr, sizeof(addr)) < 0)\n> > > -\t\tdie(\"unable to connect (%s)\", strerror(errno));\n> :\n> > You are saying that you were unable to create socket while you just were\n> > unable to connect.  Not any biggie, but it saves the user the trouble of\n> > one strace after being confused by an error message. :-)\n> \n> In fact, I don't think it is really worng, because it says that\n> it could not create (connected) socket or endpoint of connection.\n\nIt uses only two words (\"create socket\") from that, though. ;-)\n\n> Anyway, I agree that it would be confusing.\n> Better ideas / wordings?\n\n\tif (connect() < 0) sockfd = -2;\n\t...\n\tif (sockfd == -1) die(\"unable to create socket\");\n\telse die(\"unable to connect\")\n\nor I'd prefer the second error message \"unable to connect\", which is\nperhaps more descriptive regarding what effectively happened.\n\nBTW, could the close() on the socket ever fail (read as \"modify errno\")\nhere?\n\n-- \n\t\t\t\tPetr \"Pasky\" Baudis\nStuff: http://pasky.or.cz/\nIf you want the holes in your knowledge showing up try teaching\nsomeone.  -- Alan Cox\n"},{"id":"6336","messageId":"20050723001550.GB8047@mars.ravnborg.org","threadId":"1285","inReplyTo":"20050722.172629.73295196.yoshfuji@linux-ipv6.org","subject":"Re: [PATCH 1/2] GIT: Try all addresses for given remote name","fromName":"Sam Ravnborg","fromEmail":"sam@ravnborg.org","sentAt":"2005-07-23T00:15:50Z","receivedAt":"2005-07-23T00:15:50Z","isPatch":true,"sender":{"key":"sam@ravnborg.org","avatar":"https://gravatar.com/avatar/168a912606ed0742d840bb365e3cc21db390c36531a58341dc7a069cc1f15f62?d=mp&s=160"},"body":"On Fri, Jul 22, 2005 at 05:26:29PM -0400, YOSHIFUJI Hideaki / ?$B5HF#1QL@ wrote:\n> In article <20050722210913.GH11916@pasky.ji.cz> (at Fri, 22 Jul 2005 23:09:13 +0200), Petr Baudis <pasky@suse.cz> says:\n> \n> > > -}\n> > > +#define STR_(s)\t# s\n> > > +#define STR(s)\tSTR_(s)\n> > \n> > Uh-huh? Why two macros? Well, why any macros at all?\n> > \n> :\n> > > +\tchar *colon, *end;\n> > > +\tchar *port = STR(DEFAULT_GIT_PORT);\n> > > +\tstruct addrinfo hints, *ai0, *ai;\n> \n> The macro is used here.\n\nYou could copy the include/linux/stingify.h file from the kernel also.\nThat makes it available for other users also.\n\n\tSam\n"}]}