Created
May 16, 2011 14:50
-
-
Save windhamdavid/974569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
opyright (c) 2009-2011 VMware, Inc. | |
2 require 'fileutils' | |
3 require 'logging' | |
4 require 'socket' | |
5 | |
6 # VMware's Cloud Application Platform | |
7 | |
8 module VCAP | |
9 | |
10 A_ROOT_SERVER = '198.41.0.4' | |
11 | |
12 def self.local_ip(route = A_ROOT_SERVER) | |
13 route ||= A_ROOT_SERVER | |
14 orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true | |
15 UDPSocket.open {|s| s.connect(route, 1); s.addr.last } | |
16 ensure | |
17 Socket.do_not_reverse_lookup = orig | |
18 end | |
19 | |
20 def self.fast_uuid | |
21 values = [ | |
22 rand(0x0010000),rand(0x0010000),rand(0x0010000), | |
23 rand(0x0010000),rand(0x0010000),rand(0x1000000), | |
24 rand(0x1000000), | |
25 ] | |
26 "%04x%04x%04x%04x%04x%06x%06x" % values | |
27 end | |
28 | |
29 def self.grab_ephemeral_port | |
30 socket = TCPServer.new('0.0.0.0', 0) | |
31 socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true) | |
32 Socket.do_not_reverse_lookup = true | |
33 port = socket.addr[1] | |
34 socket.close | |
35 return port | |
36 end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment