Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
resolv.c
Go to the documentation of this file.
1 #include <ruby.h>
2 #include <ruby/encoding.h>
3 #include <windows.h>
4 #ifndef NTDDI_VERSION
5 #define NTDDI_VERSION 0x06000000
6 #endif
7 #include <iphlpapi.h>
8 
9 static VALUE
10 w32error_make_error(DWORD e)
11 {
12  VALUE code = ULONG2NUM(e);
13  return rb_class_new_instance(1, &code, rb_path2class("Win32::Resolv::Error"));
14 }
15 
16 static void
17 w32error_raise(DWORD e)
18 {
19  rb_exc_raise(w32error_make_error(e));
20 }
21 
22 static VALUE
23 get_dns_server_list(VALUE self)
24 {
25  FIXED_INFO *fixedinfo = NULL;
26  ULONG buflen = 0;
27  DWORD ret;
28  VALUE buf, nameservers = Qnil;
29 
30  ret = GetNetworkParams(NULL, &buflen);
31  if (ret != NO_ERROR && ret != ERROR_BUFFER_OVERFLOW) {
32  w32error_raise(ret);
33  }
34  fixedinfo = ALLOCV(buf, buflen);
35  ret = GetNetworkParams(fixedinfo, &buflen);
36  if (ret == NO_ERROR) {
37  const IP_ADDR_STRING *ipaddr = &fixedinfo->DnsServerList;
38  nameservers = rb_ary_new();
39  do {
40  const char *s = ipaddr->IpAddress.String;
41  if (!*s) continue;
42  if (strcmp(s, "0.0.0.0") == 0) continue;
43  rb_ary_push(nameservers, rb_str_new_cstr(s));
44  } while ((ipaddr = ipaddr->Next) != NULL);
45  }
46  ALLOCV_END(buf);
47  if (ret != NO_ERROR) w32error_raise(ret);
48 
49  return nameservers;
50 }
51 
52 void
54 {
55  VALUE mWin32 = rb_define_module("Win32");
56  VALUE resolv = rb_define_module_under(mWin32, "Resolv");
57  VALUE singl = rb_singleton_class(resolv);
58  rb_define_private_method(singl, "get_dns_server_list", get_dns_server_list, 0);
59 }
60 
61 void
63 {
64  InitVM(resolv);
65 }
strcmp
int strcmp(const char *, const char *)
InitVM_resolv
void InitVM_resolv(void)
Definition: resolv.c:53
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:797
rb_str_new_cstr
#define rb_str_new_cstr(str)
Definition: rb_mjit_min_header-2.7.1.h:6078
VALUE
unsigned long VALUE
Definition: ruby.h:102
encoding.h
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:772
DWORD
IUnknown DWORD
Definition: win32ole.c:33
NULL
#define NULL
Definition: _sdbm.c:101
InitVM
#define InitVM(ext)
Definition: ruby.h:2329
ULONG2NUM
#define ULONG2NUM(x)
Definition: ruby.h:1645
ALLOCV_END
#define ALLOCV_END(v)
Definition: ruby.h:1750
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:1195
ruby.h
Init_resolv
void Init_resolv(void)
Definition: resolv.c:62
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4322
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:668
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1725
rb_path2class
VALUE rb_path2class(const char *)
Definition: variable.c:268
rb_class_new_instance
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
Allocates and initializes an instance of klass.
Definition: object.c:1955
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:723
Qnil
#define Qnil
Definition: ruby.h:469
rb_define_private_method
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1569
ALLOCV
#define ALLOCV(v, n)
Definition: ruby.h:1748