1 /* 2 * Copyright (C) 2012 Raphael Coeffic 3 * 4 * This file is part of SEMS, a free SIP media server. 5 * 6 * SEMS is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. This program is released under 10 * the GPL with the additional exemption that compiling, linking, 11 * and/or using OpenSSL is allowed. 12 * 13 * For a license to use the SEMS software under conditions 14 * other than those described here, or to purchase support for this 15 * software, please contact iptel.org by e-mail at the following addresses: 16 * info@iptel.org 17 * 18 * SEMS is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 26 */ 27 #ifndef _ip_util_h_ 28 #define _ip_util_h_ 29 30 #include <ctype.h> 31 #include <sys/socket.h> 32 #include <netinet/in.h> 33 34 #include <string> 35 using std::string; 36 37 #define SAv4(addr) \ 38 ((struct sockaddr_in*)addr) 39 40 #define SAv6(addr) \ 41 ((struct sockaddr_in6*)addr) 42 43 #define SA_len(addr) \ 44 ((addr)->ss_family == AF_INET ? \ 45 sizeof(sockaddr_in) : sizeof(sockaddr_in6)) 46 47 /** 48 * Fill the sockaddr_storage structure based 49 * on the address given in 'src'. 50 * @param src string representation of the IP address. 51 * @param dst address stucture 52 * @return 1 on success, 0 if address is not valid 53 */ 54 int am_inet_pton(const char* src, struct sockaddr_storage* dst); 55 56 /** 57 * Print a string representation of the IP address in 'addr'. 58 * 59 * @param str buffer for the result string. 60 * @param size size of the result string buffer. 61 * @return NULL if failed, result string otherwise. 62 */ 63 const char* am_inet_ntop(const sockaddr_storage* addr, char* str, size_t size); 64 65 /** 66 * Print a string representation of the IP address in 'addr'. 67 * 68 * @return empty string if failed, result string otherwise. 69 */ 70 string am_inet_ntop(const sockaddr_storage* addr); 71 72 /** 73 * Print a string representation of the IP address in 'addr'. 74 * IPv6 addresses are surrounded by '[' and ']', as needed for SIP header fields. 75 * 76 * @param str buffer for the result string. 77 * @param size size of the result string buffer. 78 * @return NULL if failed, result string otherwise 79 */ 80 const char* am_inet_ntop_sip(const sockaddr_storage* addr, char* str, size_t size); 81 82 void am_set_port(struct sockaddr_storage* addr, short port); 83 unsigned short am_get_port(const sockaddr_storage* addr); 84 85 #endif 86