1 /* 2 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana 3 * University Research and Technology 4 * Corporation. All rights reserved. 5 * Copyright (c) 2004-2005 The University of Tennessee and The University 6 * of Tennessee Research Foundation. All rights 7 * reserved. 8 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 9 * University of Stuttgart. All rights reserved. 10 * Copyright (c) 2004-2005 The Regents of the University of California. 11 * All rights reserved. 12 * Copyright (c) 2007 Los Alamos National Security, LLC. All rights 13 * reserved. 14 * Copyright (c) 2017 Intel, Inc. All rights reserved. 15 * $COPYRIGHT$ 16 * 17 * Additional copyrights may follow 18 * 19 * $HEADER$ 20 */ 21 22 /* @file */ 23 24 #ifndef PMIX_UTIL_NET_H 25 #define PMIX_UTIL_NET_H 26 27 #include "pmix_config.h" 28 29 #ifdef HAVE_SYS_TYPES_H 30 #include <sys/types.h> 31 #endif 32 #ifdef HAVE_SYS_SOCKET_H 33 #include <sys/socket.h> 34 #endif 35 #ifdef HAVE_NETINET_IN_H 36 #include <netinet/in.h> 37 #endif 38 39 BEGIN_C_DECLS 40 41 /** 42 * Intiailize the network helper subsystem 43 * 44 * Initialize the network helper subsystem. Should be called exactly 45 * once for any process that will use any function in the network 46 * helper subsystem. 47 * 48 * @retval PMIX_SUCCESS Success 49 * @retval PMIX_ERR_TEMP_OUT_OF_RESOURCE Not enough memory for static 50 * buffer creation 51 */ 52 PMIX_EXPORT int pmix_net_init(void); 53 54 55 /** 56 * Finalize the network helper subsystem 57 * 58 * Finalize the network helper subsystem. Should be called exactly 59 * once for any process that will use any function in the network 60 * helper subsystem. 61 * 62 * @retval PMIX_SUCCESS Success 63 */ 64 PMIX_EXPORT int pmix_net_finalize(void); 65 66 67 /** 68 * Calculate netmask in network byte order from CIDR notation 69 * 70 * @param prefixlen (IN) CIDR prefixlen 71 * @return netmask in network byte order 72 */ 73 PMIX_EXPORT uint32_t pmix_net_prefix2netmask(uint32_t prefixlen); 74 75 76 /** 77 * Determine if given IP address is in the localhost range 78 * 79 * Determine if the given IP address is in the localhost range 80 * (127.0.0.0/8), meaning that it can't be used to connect to machines 81 * outside the current host. 82 * 83 * @param addr struct sockaddr_in of IP address 84 * @return true if \c addr is a localhost address, 85 * false otherwise. 86 */ 87 PMIX_EXPORT bool pmix_net_islocalhost(const struct sockaddr *addr); 88 89 90 /** 91 * Are we on the same network? 92 * 93 * For IPv6, we only need to check for /64, there are no other 94 * local netmasks. 95 * 96 * @param addr1 struct sockaddr of address 97 * @param addr2 struct sockaddr of address 98 * @param prefixlen netmask (either CIDR or IPv6 prefixlen) 99 * @return true if \c addr1 and \c addr2 are on the 100 * same net, false otherwise. 101 */ 102 PMIX_EXPORT bool pmix_net_samenetwork(const struct sockaddr *addr1, 103 const struct sockaddr *addr2, 104 uint32_t prefixlen); 105 106 107 /** 108 * Is the given address a public IPv4 address? Returns false for IPv6 109 * address. 110 * 111 * @param addr address as struct sockaddr 112 * @return true, if \c addr is IPv4 public, false otherwise 113 */ 114 PMIX_EXPORT bool pmix_net_addr_isipv4public(const struct sockaddr *addr); 115 116 117 /** 118 * Get string version of address 119 * 120 * Return the un-resolved address in a string format. The string will 121 * be returned in a per-thread static buffer and should not be freed 122 * by the user. 123 * 124 * @param addr struct sockaddr of address 125 * @return literal representation of \c addr 126 */ 127 PMIX_EXPORT char* pmix_net_get_hostname(const struct sockaddr *addr); 128 129 130 /** 131 * Get port number from struct sockaddr 132 * 133 * Return the port number (as an integr) from either a struct 134 * sockaddr_in or a struct sockaddr_in6. 135 * 136 * @param addr struct sockaddr containing address 137 * @return port number from \addr 138 */ 139 PMIX_EXPORT int pmix_net_get_port(const struct sockaddr *addr); 140 141 /** 142 * Test if a string is actually an IP address 143 * 144 * Returns true if the string is of IPv4 or IPv6 address form 145 */ 146 PMIX_EXPORT bool pmix_net_isaddr(const char *name); 147 148 END_C_DECLS 149 150 #endif /* PMIX_UTIL_NET_H */