Optional Two-phase heap tracing
[hiphop-php.git] / hphp / util / network.h
blobea18b8cf33743491b48ec07ecdb62f7cfa9fbcca
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_NETWORK_H_
18 #define incl_HPHP_NETWORK_H_
20 #include <cstdlib>
21 #include <string>
23 #include <folly/IPAddress.h>
24 #include <folly/portability/Sockets.h>
26 /**
27 * Network utility functions.
29 namespace HPHP {
30 ///////////////////////////////////////////////////////////////////////////////
31 // thread-safe network functions
33 struct HostEnt {
34 HostEnt() : tmphstbuf(nullptr) {}
35 ~HostEnt() { if (tmphstbuf) free(tmphstbuf);}
37 struct hostent hostbuf;
38 char *tmphstbuf;
39 int herr;
42 // Extract out the scheme, host and port from a URL
43 // http://192.168.1.0:80
44 // http, 192.168.1.0, 80
45 // http://[2a03:2880::1]:80
46 // http, 2a03:2880::1, 80
47 // ssl://192.168.1.0:443
48 // ssl, 192.168.1.0, 80
49 // ssl://[2a03:2880::1]:443
50 // ssl, 2a03:2880::1, 443
51 struct HostURL {
52 explicit HostURL(const std::string &hosturl, int port = 0);
54 bool isIPv6() const {return m_ipv6;}
55 bool isValid() const {return m_valid;}
56 uint16_t getPort() const {return m_port;}
57 std::string getScheme() const {return m_scheme;}
58 std::string getHost() const {return m_host;}
59 std::string getHostURL() const {return m_hosturl;}
61 private:
62 bool m_ipv6;
63 bool m_valid;
64 uint16_t m_port;
65 std::string m_scheme;
66 std::string m_host;
67 std::string m_hosturl;
70 bool safe_gethostbyname(const char *address, HostEnt &result);
72 ///////////////////////////////////////////////////////////////////////////////
73 /**
74 * Get local machine's primary IP address.
76 std::string GetPrimaryIP();
77 std::string GetPrimaryIPv4();
78 std::string GetPrimaryIPv6();
80 ///////////////////////////////////////////////////////////////////////////////
83 #endif // incl_HPHP_NETWORK_H_