inet.ex (1346B)
1 defmodule Postgrex.Extensions.INET do 2 @moduledoc false 3 4 import Postgrex.BinaryUtils, warn: false 5 use Postgrex.BinaryExtension, send: "cidr_send", send: "inet_send" 6 7 def encode(_) do 8 quote location: :keep do 9 %Postgrex.INET{address: {a, b, c, d}, netmask: nil} -> 10 <<8::int32(), 2, 32, 0, 4, a, b, c, d>> 11 12 %Postgrex.INET{address: {a, b, c, d}, netmask: n} -> 13 <<8::int32(), 2, n, 1, 4, a, b, c, d>> 14 15 %Postgrex.INET{address: {a, b, c, d, e, f, g, h}, netmask: nil} -> 16 <<20::int32(), 3, 128, 0, 16, a::16, b::16, c::16, d::16, e::16, f::16, g::16, h::16>> 17 18 %Postgrex.INET{address: {a, b, c, d, e, f, g, h}, netmask: n} -> 19 <<20::int32(), 3, n, 1, 16, a::16, b::16, c::16, d::16, e::16, f::16, g::16, h::16>> 20 21 other -> 22 raise DBConnection.EncodeError, Postgrex.Utils.encode_msg(other, Postgrex.INET) 23 end 24 end 25 26 def decode(_) do 27 quote location: :keep do 28 <<8::int32(), 2, n, cidr?, 4, a, b, c, d>> -> 29 n = if(cidr? == 1 or n != 32, do: n, else: nil) 30 %Postgrex.INET{address: {a, b, c, d}, netmask: n} 31 32 <<20::int32(), 3, n, cidr?, 16, a::16, b::16, c::16, d::16, e::16, f::16, g::16, h::16>> -> 33 n = if(cidr? == 1 or n != 128, do: n, else: nil) 34 %Postgrex.INET{address: {a, b, c, d, e, f, g, h}, netmask: n} 35 end 36 end 37 end