فهرست منبع

LibC: Let gethostbyname() handle IPv4 address as input.

It would be neat to do a reverse lookup too, but for now we just parse
the IPv4 address into a dword.
Andreas Kling 6 سال پیش
والد
کامیت
696f9c5bc0
1فایلهای تغییر یافته به همراه19 افزوده شده و 0 حذف شده
  1. 19 0
      LibC/netdb.cpp

+ 19 - 0
LibC/netdb.cpp

@@ -17,6 +17,25 @@ static in_addr_t* __gethostbyname_address_list_buffer[2];
 
 
 hostent* gethostbyname(const char* name)
 hostent* gethostbyname(const char* name)
 {
 {
+    unsigned a;
+    unsigned b;
+    unsigned c;
+    unsigned d;
+    int count = sscanf(name, "%u.%u.%u.%u", &a, &b, &c, &d);
+    if (count == 4 && a < 256 && b < 256 && c < 256 && d < 256) {
+        sprintf(__gethostbyname_name_buffer, "%u.%u.%u.%u", a, b, c, d);
+        __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
+        __gethostbyname_buffer.h_aliases = nullptr;
+        __gethostbyname_buffer.h_addrtype = AF_INET;
+        new (&__gethostbyname_address) IPv4Address(a, b, c, d);
+        __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
+        __gethostbyname_address_list_buffer[1] = nullptr;
+        __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
+        __gethostbyname_buffer.h_length = 4;
+
+        return &__gethostbyname_buffer;
+    }
+
     int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
     int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
     if (fd < 0) {
     if (fd < 0) {
         perror("socket");
         perror("socket");