/lib/truffle/socket/truffle/ancillary_data.rb

https://github.com/oracle/truffleruby · Ruby · 82 lines · 47 code · 7 blank · 28 comment · 9 complexity · f78ed07fb80c158a2093143e9d308ff2 MD5 · raw file

  1. # Copyright (c) 2013, Brian Shirai
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. #
  7. # 1. Redistributions of source code must retain the above copyright notice, this
  8. # list of conditions and the following disclaimer.
  9. # 2. Redistributions in binary form must reproduce the above copyright notice,
  10. # this list of conditions and the following disclaimer in the documentation
  11. # and/or other materials provided with the distribution.
  12. # 3. Neither the name of the library nor the names of its contributors may be
  13. # used to endorse or promote products derived from this software without
  14. # specific prior written permission.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. # DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
  20. # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. module Truffle
  27. module Socket
  28. module AncillaryData
  29. LEVEL_PREFIXES = {
  30. ::Socket::SOL_SOCKET => %w{SCM_ UNIX},
  31. ::Socket::IPPROTO_IP => %w{IP_ IP},
  32. ::Socket::IPPROTO_IPV6 => %w{IPV6_ IPV6},
  33. ::Socket::IPPROTO_TCP => %w{TCP_ TCP},
  34. ::Socket::IPPROTO_UDP => %w{UDP_ UDP}
  35. }
  36. def self.level(raw_level)
  37. if raw_level.is_a?(Integer)
  38. raw_level
  39. else
  40. level = Socket.coerce_to_string(raw_level)
  41. if level == 'SOL_SOCKET' or level == 'SOCKET'
  42. ::Socket::SOL_SOCKET
  43. # Translates "TCP" into "IPPROTO_TCP", "UDP" into "IPPROTO_UDP", etc.
  44. else
  45. Socket.prefixed_socket_constant(level, 'IPPROTO_') do
  46. "unknown protocol level: #{level}"
  47. end
  48. end
  49. end
  50. end
  51. def self.type(family, level, raw_type)
  52. if raw_type.is_a?(Integer)
  53. raw_type
  54. else
  55. type = Socket.coerce_to_string(raw_type)
  56. if family == ::Socket::AF_INET or family == ::Socket::AF_INET6
  57. prefix, label = LEVEL_PREFIXES[level]
  58. else
  59. prefix, label = LEVEL_PREFIXES[::Socket::SOL_SOCKET]
  60. end
  61. # Translates "RIGHTS" into "SCM_RIGHTS", "CORK" into "TCP_CORK" (when
  62. # the level is IPPROTO_TCP), etc.
  63. if prefix and label
  64. Socket.prefixed_socket_constant(type, prefix) do
  65. "Unknown #{label} control message: #{type}"
  66. end
  67. else
  68. raise TypeError,
  69. "no implicit conversion of #{type.class} into Integer"
  70. end
  71. end
  72. end
  73. end
  74. end
  75. end