Ruby: ext/win32/resolv/resolv.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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
9static VALUE
10w32error_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
16static void
17w32error_raise(DWORD e)
18{
19 rb_exc_raise(w32error_make_error(e));
20}
21
22static VALUE
23get_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
52void
53InitVM_resolv(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
61void
62Init_resolv(void)
63{
64 InitVM(resolv);
65}
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:1301
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:749
rb_define_private_method
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
Definition: cxxanyargs.hpp:660
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1924
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:871
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:895
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:712
rb_class_new_instance
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
Allocates and initializes an instance of klass.
Definition: object.c:1953
encoding.h
rb_str_new_cstr
#define rb_str_new_cstr(str)
Definition: string.h:219
rb_path2class
VALUE rb_path2class(const char *)
Definition: variable.c:287
buf
voidpf void * buf
Definition: ioapi.h:138
ULONG2NUM
#define ULONG2NUM
Definition: long.h:60
ALLOCV
#define ALLOCV
Definition: memory.h:138
ALLOCV_END
#define ALLOCV_END
Definition: memory.h:140
NULL
#define NULL
Definition: regenc.h:69
Init_resolv
void Init_resolv(void)
Definition: resolv.c:62
InitVM_resolv
void InitVM_resolv(void)
Definition: resolv.c:53
ruby.h
InitVM
#define InitVM(ext)
Definition: ruby.h:112
Qnil
#define Qnil
Definition: special_consts.h:51
code
Definition: inftree9.h:24
VALUE
unsigned long VALUE
Definition: value.h:38
DWORD
IUnknown DWORD
Definition: win32ole.c:33