File indexing completed on 2024-04-21 04:58:42
0001 /* This file is part of the KDE project 0002 Copyright (C) 2009 Collabora Ltd <info@collabora.co.uk> 0003 @author George Goldberg <george.goldberg@collabora.co.uk> 0004 Copyright (C) 2007 Alessandro Praduroux <pradu@pradu.it> 0005 Copyright (C) 2001-2003 by Tim Jansen <tim@tjansen.de> 0006 0007 This program is free software; you can redistribute it and/or 0008 modify it under the terms of the GNU General Public 0009 License as published by the Free Software Foundation; either 0010 version 2 of the License, or (at your option) any later version. 0011 0012 This program is distributed in the hope that it will be useful, 0013 but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 General Public License for more details. 0016 0017 You should have received a copy of the GNU General Public License 0018 along with this program; see the file COPYING. If not, write to 0019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0020 Boston, MA 02110-1301, USA. 0021 */ 0022 0023 #include "sockethelpers.h" 0024 0025 #include <sys/socket.h> 0026 #include <netinet/in.h> 0027 #include <arpa/inet.h> 0028 0029 QString peerAddress(int sock) 0030 { 0031 const int ADDR_SIZE = 50; 0032 struct sockaddr sa = {}; 0033 socklen_t salen = sizeof(struct sockaddr); 0034 0035 if (getpeername(sock, &sa, &salen) == 0) { 0036 if (sa.sa_family == AF_INET) { 0037 auto si = (struct sockaddr_in *)&sa; 0038 return QString::fromLatin1(inet_ntoa(si->sin_addr)); 0039 } 0040 0041 if (sa.sa_family == AF_INET6) { 0042 char inetbuf[ADDR_SIZE]; 0043 inet_ntop(sa.sa_family, &sa, inetbuf, ADDR_SIZE); 0044 return QString::fromLatin1(inetbuf); 0045 } 0046 0047 return QStringLiteral("not a network address"); 0048 } 0049 0050 return QStringLiteral("unable to determine..."); 0051 } 0052 0053 unsigned short peerPort(int sock) 0054 { 0055 struct sockaddr sa = {}; 0056 socklen_t salen = sizeof(struct sockaddr); 0057 0058 if (getpeername(sock, &sa, &salen) == 0) { 0059 auto si = (struct sockaddr_in *)&sa; 0060 return ntohs(si->sin_port); 0061 } 0062 0063 return 0; 0064 } 0065 0066 QString localAddress(int sock) 0067 { 0068 const int ADDR_SIZE = 50; 0069 struct sockaddr sa = {}; 0070 socklen_t salen = sizeof(struct sockaddr); 0071 0072 if (getsockname(sock, &sa, &salen) == 0) { 0073 if (sa.sa_family == AF_INET) { 0074 auto si = (struct sockaddr_in *)&sa; 0075 return QString::fromLatin1(inet_ntoa(si->sin_addr)); 0076 } 0077 0078 if (sa.sa_family == AF_INET6) { 0079 char inetbuf[ADDR_SIZE]; 0080 inet_ntop(sa.sa_family, &sa, inetbuf, ADDR_SIZE); 0081 return QString::fromLatin1(inetbuf); 0082 } 0083 0084 return QStringLiteral("not a network address"); 0085 } 0086 0087 return QStringLiteral("unable to determine..."); 0088 } 0089 0090 unsigned short localPort(int sock) 0091 { 0092 struct sockaddr sa = {}; 0093 socklen_t salen = sizeof(struct sockaddr); 0094 0095 if (getsockname(sock, &sa, &salen) == 0) { 0096 auto si = (struct sockaddr_in *)&sa; 0097 return ntohs(si->sin_port); 0098 } 0099 0100 return 0; 0101 } 0102