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

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


  • To: minios-devel@xxxxxxxxxxxxx
  • From: Costin Lupu <costin.lupu@xxxxxxxxx>
  • Date: Fri, 16 Aug 2019 10:09:39 +0300
  • Cc: felipe.huici@xxxxxxxxx, simon.kuenzer@xxxxxxxxx
  • Delivery-date: Fri, 16 Aug 2019 07:10:04 +0000
  • Ironport-phdr: 9a23:huxS7RQXBhg6jcxL8O+GFNZ+Itpsv+yvbD5Q0YIujvd0So/mwa6zZR2N2/xhgRfzUJnB7Loc0qyK6vqmADdaqs/Y7DgrS99lb1c9k8IYnggtUoauKHbQC7rUVRE8B9lIT1R//nu2YgB/Ecf6YEDO8DXptWZBUhrwOhBoKevrB4Xck9q41/yo+53Ufg5EmCexbal9IRmrswndrNQajIV8Jqo+1xfFv3RFcPlKyG11Il6egwzy7dqq8p559CRQtfMh98peXqj/Yq81U79WAik4Pm4s/MHkugXNQgWJ5nsHT2UZiQFIDBTf7BH7RZj+rC33vfdg1SaAPM32Sbc0WSm+76puVRTlhjsLOyI//WrKjMF7kaBVrw+7pxFnw4DafpybOvR9cK3af9waS3FMUtpNWyFbHo+xdZcDA/YHMO1Fr4f9vVwOrR6mCASyA+Pv0SNIhn7w3aYn1eohDBnG3BE9FN8JrHvUq871NLsTUeCz1qXIySjDb+tL2Trm9IfIaQotoemRXb1ra8bR004vGhrDg16Np4LlODaV2f4Ms2id9+dgWuOvi3InqwFsuTej3Nsjio7Mho8T11vK9j15zZ4oKdC3VUJ3e92pHZtKuy2EKYd7QNkuTm9wtConxbAKpIS3cSsKxZg92RLSZfKKf5KV7h/sSuqcJypzimh/d7KlnRmy9FCtyuj7VsapzllHtjFFktzQtnAV0BzT99SHRuN9/ki/3TaP0Bje6v9BIU8ulKrbL4QtzaIrlpYJqUTDAzT5lF/sjK+Rbkkk++6o5Pr7Yrj+u5OROJJ4hhv9P6kugMCzH/o0PwoUU2WV4ei80afs/Uz9QLVElP02lazZvYjGKsQcva65Hw5V0oA55xa7EjemyMkYkGIbI1JeYh6Hl5XpNErULPDiCvezm06snytzx/DaIr3hBY3ALn3CkLfnfLZ97FVcxBMuwt9B+ZJbFLUBLen3Wk/wr9zYEgQ5Pxa7wun5DtVxzIQeWXiAAqWBKqPdrUeI5v4zI+mLfIIVtijyK/wk5/72k3A1g18dfbSo3ZQLaXC1BfJmLFuFbnromNgOCn0GsRQjTObykl2NTSZTZ2quX6I7/jw7E4OmDYLERoCrmrCB3zq7EYNQZmFHFF+MC2zld4OaVPgQbCKdONRuniYaWri8GMcd0kSrtQn7zKEiIufK9ykwsZP4yMMz9+DV0xYo+m9aFcOYhkqKVH11mCssWicrlPR0pldhy1HF1bVgntRTDppL+vkPSABsZs2U9PBzF92nAlGJRdyOUlvzGtg=
  • Ironport-sdr: uJ6I4XHot64hlQN2VCMNYw7zMN/+3w2p36we/TVJXLnm3h8uk97THxn7618xVNuu9soeNwYAFJ 43yPgILUic8w==
  • 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>
Reviewed-by: Felipe Huici <felipe.huici@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®.