[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT/LWIP PATCH 3/5] Import getnameinfo() function from OsV


  • To: minios-devel@xxxxxxxxxxxxx
  • From: Costin Lupu <costin.lupu@xxxxxxxxx>
  • Date: Fri, 2 Aug 2019 15:57:41 +0300
  • Cc: felipe.huici@xxxxxxxxx, simon.kuenzer@xxxxxxxxx, sharan.santhanam@xxxxxxxxx
  • Delivery-date: Fri, 02 Aug 2019 12:57:57 +0000
  • Ironport-phdr: 9a23:5+LOSxRNA5PijSMGw/oTEXlEj9psv+yvbD5Q0YIujvd0So/mwa6zbBWN2/xhgRfzUJnB7Loc0qyK6vqmCD1Lus7JmUtBWaQEbwUCh8QSkl5oK+++Imq/EsTXaTcnFt9JTl5v8iLzG0FUHMHjew+a+SXqvnYdFRrlKAV6OPn+FJLMgMSrzeCy/IDYbxlViDanbr5+MQu6oR/Vu8QSjodvJLo9wQbVr3VVfOhb2XlmLk+JkRbm4cew8p9j8yBOtP8k6sVNT6b0cbkmQLJBFDgpPHw768PttRnYUAuA/WAcXXkMkhpJGAfK8hf3VYrsvyTgt+p93C6aPdDqTb0xRD+v4btnRAPuhSwaMTMy7WPZhdFqjK9DoByvuQFxzYDXbo+SNvV+cLjQcc8GSWdbQspcTTBNDp+6YoASD+QBJ+FYr4zlqlYQqhu+HhWsBOLpyjRVgHH2wLU60/k8GgzBwAMgGMkOsGjVrNXzLqsSVf21zLHVzTjYc/xWwi3x6JDVch86u/2MR7VwfNPXxEIyGQ3FiVCQppbkPzOTzukNvGmb7/ZgVeKykGErsR1+oj+qxsoql4LHhZoVx0jZ+Sh23Yo5P961RU5hbdK6DpddtTuWOoVuTs8/TGxkpTw2x74GtJKhYiQHxpoqywTCZ/GIbYSF5A/oWvyLLjdinn1lfaqyhxO18Ue91OLxTtK00FNWripdldnMq2wN2wTT6seZTvt9+V+s2SiK1w/J6+FEJVo4mrbAK5I72r4wi4EcvV7ZHi/xgEn2jamWeVs4+uWw9ujqbajqqoWfOoJ0kA3yLLoil86lDek2KgQOWnKU+eW41L3t5035R7BKg+Uqnaneq5DaOcUbpqijDw9Rz4Yj8wu/Aiy639QDh3UHNE9Fdwmdg4jzPFHCOOr4Auung1SwjDdrwOjLPrjgApXLL3jDl6zhca9g5EFC1go809Rf6olQCrEAO/LzRlTxuMbXDxAnNQy42eHnCM9y1okGQ2KAHreZML/OsV+P/u8vLfeDZIsUuDblNfcp/f7ujWI4mVABfKmkxp0XZ26kEfRiOUqWemDgjckcEW0SpAoxUPTqiEGeUT5Uf3uyRKI86SshCI28C4fPXJutj6aH3CenHp1ZfGZGBUqJEXrzcYWLResDaCaILcB6iDALS6WhQZc71R6yrA/616ZnLu3M9y0DtJLj0d915+vJmREx6Dx7EcKd0mCTQGFuhW4IQSE507x7oUNn0ViPy7Z3g+ELXeBUsvZIVAY9LtvQwvJ3D/j2WxndZZGZRVDgRc+pUh8rSddk6NgVf0d7U/G/lg2LiyGtGKMUkfqPGYQp2qnHmWDsLYBnzCCVh+Eak1A6T54XZiWdjall+l2LCg==
  • Ironport-sdr: v8V2gBG40wk1q7tYwySAvt6WUnOYKeJQlGT1QK5Z34MHOsGB8Joowas23QdN0C7wG7bajzZvFo GpYIu90Ajrcg==
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>

Copied as it is from official OsV git mirror
https://github.com/cloudius-systems/osv.git, commit ee7a2cd4.

Original file location: libc/network/getnameinfo.c

Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
---
 getnameinfo.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)
 create mode 100644 getnameinfo.c

diff --git a/getnameinfo.c b/getnameinfo.c
new file mode 100644
index 0000000..fc5acf6
--- /dev/null
+++ b/getnameinfo.c
@@ -0,0 +1,80 @@
+#include <osv/debug.h>
+#include <netdb.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include "__dns.hh"
+
+int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
+       char *restrict node, socklen_t nodelen,
+       char *restrict serv, socklen_t servlen,
+       int flags)
+{
+       char buf[256];
+       unsigned char reply[512];
+       int af = sa->sa_family;
+       char line[512];
+       FILE *f;
+       unsigned char *a;
+
+       switch (af) {
+       case AF_INET:
+               a = (void *)&((struct sockaddr_in *)sa)->sin_addr;
+               if (sl != sizeof(struct sockaddr_in)) return EAI_FAMILY;
+               break;
+       case AF_INET6:
+               a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr;
+               if (sl != sizeof(struct sockaddr_in6)) return EAI_FAMILY;
+               break;
+       default:
+               return EAI_FAMILY;
+       }
+
+       /* Try to find ip within /etc/hosts */
+       if ((node && nodelen) && (af == AF_INET)) {
+               const char *ipstr = inet_ntoa(((struct sockaddr_in 
*)sa)->sin_addr);
+               size_t l = strlen(ipstr);
+               f = fopen("/etc/hosts", "r");
+               if (f) while (fgets(line, sizeof line, f)) {
+                       if (strncmp(line, ipstr, l) != 0)
+                               continue;
+
+                       char *domain = strtok(line, " ");
+                       if (!domain) continue;
+                       domain = strtok(NULL, " ");
+                       if (!domain) continue;
+
+                       if (strlen(domain) >= nodelen) return EAI_OVERFLOW;
+                       strcpy(node, domain);
+                       fclose(f);
+                       return 0;
+               }
+               if (f) fclose(f);
+       }
+
+       if (node && nodelen) {
+               if ((flags & NI_NUMERICHOST)
+                       || __dns_query(reply, a, af, 1) <= 0
+                       || __dns_get_rr(buf, 0, 256, 1, reply, RR_PTR, 1) <= 0)
+               {
+                       if (flags & NI_NAMEREQD) return EAI_NONAME;
+                       inet_ntop(af, a, buf, sizeof buf);
+               }
+               if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
+               strcpy(node, buf);
+       }
+
+       if (serv && servlen) {
+               if (snprintf(buf, sizeof buf, "%d",
+                       ntohs(((struct sockaddr_in *)sa)->sin_port))>=servlen)
+                       return EAI_OVERFLOW;
+               strcpy(serv, buf);
+       }
+
+       return 0;
+}
-- 
2.20.1


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.