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