Permalink
Browse files

dns, feat: add module dns, support dns.resolve.

  • Loading branch information...
xicilion committed Dec 19, 2017
1 parent b85cc93 commit 52b984cb66cf57a58f5a7274414879e2f8d41f1a
Showing with 130 additions and 0 deletions.
  1. +84 −0 fibjs/include/ifs/dns.h
  2. +31 −0 fibjs/src/net/net.cpp
  3. +15 −0 idl/zh-cn/dns.idl
View
@@ -0,0 +1,84 @@
/***************************************************************************
* *
* This file was automatically generated using idlc.js *
* PLEASE DO NOT EDIT!!!! *
* *
***************************************************************************/
#ifndef _dns_base_H_
#define _dns_base_H_
/**
@author Leo Hoo <lion@9465.net>
*/
#include "../object.h"
namespace fibjs {
class dns_base : public object_base {
DECLARE_CLASS(dns_base);
public:
// dns_base
static result_t resolve(exlib::string name, obj_ptr<NArray>& retVal, AsyncEvent* ac);
public:
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args)
{
CONSTRUCT_INIT();
Isolate* isolate = Isolate::current();
isolate->m_isolate->ThrowException(
isolate->NewString("not a constructor"));
}
public:
static void s_resolve(const v8::FunctionCallbackInfo<v8::Value>& args);
public:
ASYNC_STATICVALUE2(dns_base, resolve, exlib::string, obj_ptr<NArray>);
};
}
namespace fibjs {
inline ClassInfo& dns_base::class_info()
{
static ClassData::ClassMethod s_method[] = {
{ "resolve", s_resolve, true },
{ "resolveSync", s_resolve, true }
};
static ClassData s_cd = {
"dns", true, s__new, NULL,
ARRAYSIZE(s_method), s_method, 0, NULL, 0, NULL, 0, NULL, NULL, NULL,
&object_base::class_info()
};
static ClassInfo s_ci(s_cd);
return s_ci;
}
inline void dns_base::s_resolve(const v8::FunctionCallbackInfo<v8::Value>& args)
{
obj_ptr<NArray> vr;
METHOD_NAME("dns.resolve");
METHOD_ENTER();
ASYNC_METHOD_OVER(1, 1);
ARG(exlib::string, 0);
if (!cb.IsEmpty()) {
acb_resolve(v0, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_resolve(v0, vr);
METHOD_RETURN();
}
}
#endif
View
@@ -6,6 +6,7 @@
*/
#include "object.h"
#include "ifs/dns.h"
#include "ifs/ssl.h"
#include "ifs/os.h"
#include "Socket.h"
@@ -19,6 +20,36 @@
namespace fibjs {
DECLARE_MODULE(dns);
result_t dns_base::resolve(exlib::string name, obj_ptr<NArray>& retVal, AsyncEvent* ac)
{
if (ac->isSync())
return CHECK_ERROR(CALL_E_LONGSYNC);
addrinfo hints = { 0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, 0, 0, 0 };
addrinfo* result = NULL;
addrinfo* ptr = NULL;
if (getaddrinfo(name.c_str(), NULL, &hints, &result))
return CHECK_ERROR(SocketError());
obj_ptr<NArray> arr = new NArray();
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
inetAddr addr_info;
addr_info.init(addr_info.addr4.sin_family);
memcpy(&addr_info, ptr->ai_addr, addr_info.size());
arr->append(addr_info.str());
}
freeaddrinfo(result);
retVal = arr;
return 0;
}
DECLARE_MODULE(net);
result_t net_base::info(v8::Local<v8::Object>& retVal)
View
@@ -0,0 +1,15 @@
/*! @brief dns 域名查询模块
基础模块,引用方式:
```JavaScript
var dns = require('dns');
```
*/
module dns
{
/*! @brief 查询给定的主机名的地址
@param name 指定主机名
@return 返回查询的 ip 字符串数组
*/
static NArray resolve(String name) async;
};

0 comments on commit 52b984c

Please sign in to comment.