blob: aaf489f6ca9bdbba4ef7d2dc5e36a97c8e9b1966 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Cosmos Nicolaou43b95352014-10-14 11:09:52 -07005// +build linux
6
7package internal
8
9import (
10 "net"
11
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070012 "v.io/v23/logging"
Cosmos Nicolaou43b95352014-10-14 11:09:52 -070013
Suharsh Sivakumardcc11d72015-05-11 12:19:20 -070014 "v.io/x/ref/runtime/internal/gce"
Cosmos Nicolaou43b95352014-10-14 11:09:52 -070015)
16
17// GCEPublicAddress returns the public IP address of the GCE instance
18// it is run from, or nil if run from anywhere else. The returned address
19// is the public address of a 1:1 NAT tunnel to this host.
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070020func GCEPublicAddress(log logging.Logger) *net.IPAddr {
Cosmos Nicolaou43b95352014-10-14 11:09:52 -070021 if !gce.RunningOnGCE() {
22 return nil
23 }
24 var pub *net.IPAddr
25 // Determine the IP address from GCE's metadata
26 if ip, err := gce.ExternalIPAddress(); err != nil {
27 log.Infof("failed to query GCE metadata: %s", err)
28 } else {
29 // 1:1 NAT case, our network config will not change.
30 pub = &net.IPAddr{IP: ip}
31 }
32 if pub == nil {
33 log.Infof("failed to determine public IP address to publish with")
34 }
35 return pub
36}