This fixes a bug in PHP/HH's crypt_blowfish implementation that can cause a short...
[hiphop-php.git] / hphp / util / network.h
blob83dce744139e83fe26b54834dee18c05118f6270
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 #pragma once
19 #include <cstdlib>
20 #include <string>
22 #include <folly/IPAddress.h>
23 #include <folly/portability/Sockets.h>
25 /**
26 * Network utility functions.
28 namespace HPHP {
29 ///////////////////////////////////////////////////////////////////////////////
30 // thread-safe network functions
32 struct HostEnt {
33 HostEnt() : tmphstbuf(nullptr) {}
34 ~HostEnt() { if (tmphstbuf) free(tmphstbuf);}
36 struct hostent hostbuf;
37 char *tmphstbuf;
38 int herr;
41 // Extract out the scheme, host and port from a URL
42 // http://192.168.1.0:80
43 // http, 192.168.1.0, 80
44 // http://[2a03:2880::1]:80
45 // http, 2a03:2880::1, 80
46 // ssl://192.168.1.0:443
47 // ssl, 192.168.1.0, 80
48 // ssl://[2a03:2880::1]:443
49 // ssl, 2a03:2880::1, 443
50 struct HostURL {
51 explicit HostURL(const std::string &hosturl, int port = 0);
53 bool isIPv6() const {return m_ipv6;}
54 bool isValid() const {return m_valid;}
55 uint16_t getPort() const {return m_port;}
56 std::string getScheme() const {return m_scheme;}
57 std::string getHost() const {return m_host;}
58 std::string getHostURL() const {return m_hosturl;}
60 private:
61 bool m_ipv6;
62 bool m_valid;
63 uint16_t m_port;
64 std::string m_scheme;
65 std::string m_host;
66 std::string m_hosturl;
69 bool safe_gethostbyname(const char *address, HostEnt &result);
71 ///////////////////////////////////////////////////////////////////////////////
72 /**
73 * Get local machine's primary IP address.
75 std::string GetPrimaryIP();
76 std::string GetPrimaryIPv4();
77 std::string GetPrimaryIPv6();
79 ///////////////////////////////////////////////////////////////////////////////