{"thread":{"id":"22145","subject":"[PATCH] daemon: consider only address in kill_some_child()","startedAt":"2010-01-09T14:13:28Z","lastAt":"2010-01-09T17:20:05Z","messageCount":2,"participants":["Erik Faye-Lund","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"131184","messageId":"1263046408-2368-1-git-send-email-kusmabite@gmail.com","threadId":"22145","inReplyTo":null,"subject":"[PATCH] daemon: consider only address in kill_some_child()","fromName":"Erik Faye-Lund","fromEmail":"kusmabite@googlemail.com","sentAt":"2010-01-09T14:13:28Z","receivedAt":"2010-01-09T14:13:28Z","isPatch":true,"sender":{"key":"kusmabite@gmail.com","avatar":"https://avatars.githubusercontent.com/u/47073?v=4"},"body":"kill_some_child() compares the entire sockaddr_storage\nstructure (with the pad-bits zeroed out) when trying to\nfind out if connections originate from the same host.\nHowever, sockaddr_storage contains the port-number for\nthe connection (which varies between connections), so\nthe comparison always fails.\n\nChange the code so we only consider the host-address,\nby introducing the addrcmp()-function that inspects\nthe address family and compare as appropriate.\n\nSigned-off-by: Erik Faye-Lund <kusmabite@gmail.com>\n---\n daemon.c |   28 ++++++++++++++++++++--------\n 1 files changed, 20 insertions(+), 8 deletions(-)\n\ndiff --git a/daemon.c b/daemon.c\nindex 5783e24..918e560 100644\n--- a/daemon.c\n+++ b/daemon.c\n@@ -562,6 +562,24 @@ static int execute(struct sockaddr *addr)\n \treturn -1;\n }\n \n+static int addrcmp(const struct sockaddr_storage *s1,\n+    const struct sockaddr_storage *s2)\n+{\n+\tif (s1->ss_family != s2->ss_family)\n+\t\treturn s1->ss_family - s2->ss_family;\n+\tif (s1->ss_family == AF_INET)\n+\t\treturn memcmp(&((struct sockaddr_in *)s1)->sin_addr,\n+\t\t    &((struct sockaddr_in *)s2)->sin_addr,\n+\t\t    sizeof(struct in_addr));\n+#ifndef NO_IPV6\n+\tif (s1->ss_family == AF_INET6)\n+\t\treturn memcmp(&((struct sockaddr_in6 *)s1)->sin6_addr,\n+\t\t    &((struct sockaddr_in6 *)s2)->sin6_addr,\n+\t\t    sizeof(struct in6_addr));\n+#endif\n+\treturn 0;\n+}\n+\n static int max_connections = 32;\n \n static unsigned int live_children;\n@@ -576,17 +594,12 @@ static void add_child(pid_t pid, struct sockaddr *addr, int addrlen)\n {\n \tstruct child *newborn, **cradle;\n \n-\t/*\n-\t * This must be xcalloc() -- we'll compare the whole sockaddr_storage\n-\t * but individual address may be shorter.\n-\t */\n \tnewborn = xcalloc(1, sizeof(*newborn));\n \tlive_children++;\n \tnewborn->pid = pid;\n \tmemcpy(&newborn->address, addr, addrlen);\n \tfor (cradle = &firstborn; *cradle; cradle = &(*cradle)->next)\n-\t\tif (!memcmp(&(*cradle)->address, &newborn->address,\n-\t\t\t    sizeof(newborn->address)))\n+\t\tif (!addrcmp(&(*cradle)->address, &newborn->address))\n \t\t\tbreak;\n \tnewborn->next = *cradle;\n \t*cradle = newborn;\n@@ -619,8 +632,7 @@ static void kill_some_child(void)\n \t\treturn;\n \n \tfor (; (next = blanket->next); blanket = next)\n-\t\tif (!memcmp(&blanket->address, &next->address,\n-\t\t\t    sizeof(next->address))) {\n+\t\tif (!addrcmp(&blanket->address, &next->address)) {\n \t\t\tkill(blanket->pid, SIGTERM);\n \t\t\tbreak;\n \t\t}\n-- \n1.6.6.95.gae285\n"},{"id":"131185","messageId":"7vocl3ry9m.fsf@alter.siamese.dyndns.org","threadId":"22145","inReplyTo":"1263046408-2368-1-git-send-email-kusmabite@gmail.com","subject":"Re: [PATCH] daemon: consider only address in kill_some_child()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-01-09T17:20:05Z","receivedAt":"2010-01-09T17:20:05Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Erik Faye-Lund <kusmabite@googlemail.com> writes:\n\n> kill_some_child() compares the entire sockaddr_storage structure (with\n> the pad-bits zeroed out) when trying to find out if connections\n> originate from the same host.  However, sockaddr_storage contains the\n> port-number for the connection (which varies between connections), so\n> the comparison always fails.\n>\n> Change the code so we only consider the host-address, by introducing the\n> addrcmp()-function that inspects the address family and compare as\n> appropriate.\n>\n> Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>\n> ---\n\nMakes sense.\n"}]}