Class: Addrinfo

Inherits:
Object
  • Object
show all
Defined in:
mrbgems/mruby-socket/src/socket.c,
mrbgems/mruby-socket/mrblib/socket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sockaddr, family = Socket::PF_UNSPEC, socktype = 0, protocol = 0) ⇒ Addrinfo

Returns a new instance of Addrinfo



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 2

def initialize(sockaddr, family=Socket::PF_UNSPEC, socktype=0, protocol=0)
  @hostname = nil
  if sockaddr.is_a? Array
    sary = sockaddr
    if sary[0] == 'AF_INET' || sary[0] == 'AF_INET6'
      @sockaddr = Socket.sockaddr_in(sary[1], sary[3])
      @hostname = sary[2]
    elsif sary[0] == 'AF_UNIX'
      @sockaddr = Socket.sockaddr_un(sary[1])
    end
  else
    @sockaddr = sockaddr.dup
  end
  if family == Socket::PF_UNSPEC or family == nil
    @family = Socket._sockaddr_family(@sockaddr)
  else
    @family = family
  end
  @socktype = socktype
  @protocol = protocol
  @canonname = nil
end

Instance Attribute Details

#canonnameObject (readonly)

def bind



53
54
55
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 53

def canonname
  @canonname
end

#protocolObject (readonly)

Returns the value of attribute protocol



139
140
141
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 139

def protocol
  @protocol
end

#socktypeObject (readonly)

Returns the value of attribute socktype



140
141
142
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 140

def socktype
  @socktype
end

Class Method Details

.foreach(nodename, service, family = nil, socktype = nil, protocol = nil, flags = 0, &block) ⇒ Object



25
26
27
28
29
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 25

def self.foreach(nodename, service, family=nil, socktype=nil, protocol=nil, flags=0, &block)
  a = self.getaddrinfo(nodename, service, family, socktype, protocol, flags)
  a.each { |ai| block.call(ai) }
  a
end

.ip(host) ⇒ Object



31
32
33
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 31

def self.ip(host)
  Addrinfo.new(Socket.sockaddr_in(0, host))
end

.tcp(host, port) ⇒ Object



35
36
37
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 35

def self.tcp(host, port)
  Addrinfo.getaddrinfo(host, port, nil, Socket::SOCK_STREAM, Socket::IPPROTO_TCP)[0]
end

.udp(host, port) ⇒ Object



39
40
41
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 39

def self.udp(host, port)
  Addrinfo.getaddrinfo(host, port, nil, Socket::SOCK_DGRAM, Socket::IPPROTO_UDP)[0]
end

.unix(path, socktype = Socket::SOCK_STREAM) ⇒ Object



43
44
45
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 43

def self.unix(path, socktype=Socket::SOCK_STREAM)
  Addrinfo.new(Socket.sockaddr_un(path), Socket::AF_UNIX, socktype)
end

Instance Method Details

#_to_arrayObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 142

def _to_array
  case @family
  when Socket::AF_INET
    s = "AF_INET"
  when Socket::AF_INET6
    s = "AF_INET6"
  when Socket::AF_UNIX
    s = "AF_UNIX"
  else
    s = "(unknown AF)"
  end
  addr, port = self.getnameinfo(Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV)
  [ s, port.to_i, addr, addr ]
end

#afamilyObject



47
48
49
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 47

def afamily
  @family
end

#getnameinfoObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'mrbgems/mruby-socket/src/socket.c', line 191

static mrb_value
mrb_addrinfo_getnameinfo(mrb_state *mrb, mrb_value self)
{
  mrb_int flags;
  mrb_value ary, host, sastr, serv;
  int error;

  flags = 0;
  mrb_get_args(mrb, "|i", &flags);
  host = mrb_str_buf_new(mrb, NI_MAXHOST);
  serv = mrb_str_buf_new(mrb, NI_MAXSERV);

  sastr = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@sockaddr"));
  if (!mrb_string_p(sastr)) {
    mrb_raise(mrb, E_SOCKET_ERROR, "invalid sockaddr");
  }
  error = getnameinfo((struct sockaddr *)RSTRING_PTR(sastr), (socklen_t)RSTRING_LEN(sastr), RSTRING_PTR(host), NI_MAXHOST, RSTRING_PTR(serv), NI_MAXSERV, (int)flags);
  if (error) {
    mrb_raisef(mrb, E_SOCKET_ERROR, "getnameinfo: %s", gai_strerror(error));
  }
  ary = mrb_ary_new_capa(mrb, 2);
  mrb_str_resize(mrb, host, strlen(RSTRING_PTR(host)));
  mrb_ary_push(mrb, ary, host);
  mrb_str_resize(mrb, serv, strlen(RSTRING_PTR(serv)));
  mrb_ary_push(mrb, ary, serv);
  return ary;
}

#inspectObject

def family_addrinfo(host, port=nil) def getnameinfo(flags=0) Socket.getnameinfo end



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 64

def inspect
  if ipv4? or ipv6?
    if @protocol == Socket::IPPROTO_TCP or (@socktype == Socket::SOCK_STREAM and @protocol == 0)
      proto = 'TCP'
    elsif @protocol == Socket::IPPROTO_UDP or (@socktype == Socket::SOCK_DGRAM and @protocol == 0)
      proto = 'UDP'
    else
      proto = '???'
    end
    "#<Addrinfo: #{inspect_sockaddr} #{proto}>"
  else
    "#<Addrinfo: #{self.unix_path} SOCK_STREAM>"
  end
end

#inspect_sockaddrObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 79

def inspect_sockaddr
  if ipv4?
    a, p = ip_unpack
    "#{a}:#{p}"
  elsif ipv6?
    a, p = ip_unpack
    "[#{a}]:#{p}"
  elsif unix?
    unix_path
  else
    '???'
  end
end

#ip?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 93

def ip?
  ipv4? or ipv6?
end

#ip_addressObject



97
98
99
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 97

def ip_address
  ip_unpack[0]
end

#ip_portObject



101
102
103
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 101

def ip_port
  ip_unpack[1]
end

#ip_unpackObject



105
106
107
108
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 105

def ip_unpack
  h, p = getnameinfo(Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV)
  [ h, p.to_i ]
end

#ipv4?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 110

def ipv4?
  @family == Socket::AF_INET
end

#ipv6?Boolean

def ipv4_loopback? def ipv4_multicast? def ipv4_private?

Returns:

  • (Boolean)


118
119
120
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 118

def ipv6?
  @family == Socket::AF_INET6
end

#pfamilyObject

def ipv6_loopback? def ipv6_mc_global? def ipv6_mc_linklocal? def ipv6_mc_nodelocal? def ipv6_mc_orilocal? def ipv6_mc_sitelocal? def ipv6_multicast? def ipv6_to_ipv4 def ipv6_unspecified def ipv6_v4compat? def ipv6_v4mapped? def listen(backlog=5)



135
136
137
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 135

def pfamily
  @family
end

#to_sockaddrObject Also known as: to_s



157
158
159
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 157

def to_sockaddr
  @sockaddr
end

#unix?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 163

def unix?
  @family == Socket::AF_UNIX
end

#unix_pathObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'mrbgems/mruby-socket/src/socket.c', line 220

static mrb_value
mrb_addrinfo_unix_path(mrb_state *mrb, mrb_value self)
{
  mrb_value sastr;

  sastr = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@sockaddr"));
  if (((struct sockaddr *)RSTRING_PTR(sastr))->sa_family != AF_UNIX)
    mrb_raise(mrb, E_SOCKET_ERROR, "need AF_UNIX address");
  if (RSTRING_LEN(sastr) < (mrb_int)offsetof(struct sockaddr_un, sun_path) + 1) {
    return mrb_str_new(mrb, "", 0);
  } else {
    return mrb_str_new_cstr(mrb, ((struct sockaddr_un *)RSTRING_PTR(sastr))->sun_path);
  }
}