Changeset 26915 in vbox
- Timestamp:
- Mar 1, 2010 2:50:21 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/tcp.h
r26683 r26915 250 250 251 251 /** 252 * Gets the address of the local side. 253 * 254 * @returns IPRT status code. 255 * @param Sock Socket descriptor. 256 * @param pAddr Where to store the local address on success. 257 */ 258 RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET Sock, PRTNETADDR pAddr); 259 260 /** 252 261 * Gets the address of the other party. 253 262 * -
trunk/src/VBox/Runtime/r3/tcp.cpp
r26683 r26915 144 144 } RTTCPSERVER; 145 145 146 typedef union RTSOCKADDRUNION 147 { 148 struct sockaddr Addr; 149 struct sockaddr_in Ipv4; 150 #ifdef IPRT_WITH_TCPIP_V6 151 struct sockaddr_in6 Ipv6; 152 #endif 153 } RTSOCKADDRUNION; 146 154 147 155 /******************************************************************************* … … 153 161 static int rtTcpServerDestroySocket(RTSOCKET volatile *pSockClient, const char *pszMsg); 154 162 static int rtTcpClose(RTSOCKET Sock, const char *pszMsg, bool fTryGracefulShutdown); 163 static int rtTcpConvertAddress(RTSOCKADDRUNION *pSrc, size_t cbSrc, PRTNETADDR pAddr); 155 164 156 165 … … 1032 1041 1033 1042 1034 RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET Sock, PRTNETADDR pAddr) 1035 { 1036 union 1037 { 1038 struct sockaddr Addr; 1039 struct sockaddr_in Ipv4; 1043 static int rtTcpConvertAddress(RTSOCKADDRUNION *pSrc, size_t cbSrc, PRTNETADDR pAddr) 1044 { 1045 /* 1046 * Convert the address. 1047 */ 1048 if ( cbSrc == sizeof(struct sockaddr_in) 1049 && pSrc->Addr.sa_family == AF_INET) 1050 { 1051 RT_ZERO(*pAddr); 1052 pAddr->enmType = RTNETADDRTYPE_IPV4; 1053 pAddr->uPort = RT_N2H_U16(pSrc->Ipv4.sin_port); 1054 pAddr->uAddr.IPv4.u = pSrc->Ipv4.sin_addr.s_addr; 1055 } 1040 1056 #ifdef IPRT_WITH_TCPIP_V6 1041 struct sockaddr_in6 Ipv6; 1042 #endif 1043 } u; 1057 else if ( cbSrc == sizeof(struct sockaddr_in6) 1058 && pSrc->Addr.sa_family == AF_INET6) 1059 { 1060 RT_ZERO(*pAddr); 1061 pAddr->enmType = RTNETADDRTYPE_IPV6; 1062 pAddr->uPort = RT_N2H_U16(pSrc->Ipv6.sin6_port); 1063 pAddr->uAddr.IPv6.au32[0] = pSrc->Ipv6.sin6_addr.s6_addr32[0]; 1064 pAddr->uAddr.IPv6.au32[1] = pSrc->Ipv6.sin6_addr.s6_addr32[1]; 1065 pAddr->uAddr.IPv6.au32[2] = pSrc->Ipv6.sin6_addr.s6_addr32[2]; 1066 pAddr->uAddr.IPv6.au32[3] = pSrc->Ipv6.sin6_addr.s6_addr32[3]; 1067 } 1068 #endif 1069 else 1070 return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED; 1071 return VINF_SUCCESS; 1072 } 1073 1074 1075 RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET Sock, PRTNETADDR pAddr) 1076 { 1077 RTSOCKADDRUNION u; 1044 1078 #ifdef RT_OS_WINDOWS 1045 1079 int cbAddr = sizeof(u); … … 1048 1082 #endif 1049 1083 RT_ZERO(u); 1084 if (!getsockname(Sock, &u.Addr, &cbAddr)) 1085 return rtTcpConvertAddress(&u, cbAddr, pAddr); 1086 return rtTcpError(); 1087 } 1088 1089 1090 RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET Sock, PRTNETADDR pAddr) 1091 { 1092 RTSOCKADDRUNION u; 1093 #ifdef RT_OS_WINDOWS 1094 int cbAddr = sizeof(u); 1095 #else 1096 socklen_t cbAddr = sizeof(u); 1097 #endif 1098 RT_ZERO(u); 1050 1099 if (!getpeername(Sock, &u.Addr, &cbAddr)) 1051 { 1052 /* 1053 * Convert the address. 1054 */ 1055 if ( cbAddr == sizeof(struct sockaddr_in) 1056 && u.Addr.sa_family == AF_INET) 1057 { 1058 RT_ZERO(*pAddr); 1059 pAddr->enmType = RTNETADDRTYPE_IPV4; 1060 pAddr->uPort = RT_N2H_U16(u.Ipv4.sin_port); 1061 pAddr->uAddr.IPv4.u = u.Ipv4.sin_addr.s_addr; 1062 } 1063 #ifdef IPRT_WITH_TCPIP_V6 1064 else if ( cbAddr == sizeof(struct sockaddr_in6) 1065 && u.Addr.sa_family == AF_INET6) 1066 { 1067 RT_ZERO(*pAddr); 1068 pAddr->enmType = RTNETADDRTYPE_IPV6; 1069 pAddr->uPort = RT_N2H_U16(u.Ipv6.sin6_port); 1070 pAddr->uAddr.IPv6.au32[0] = u.Ipv6.sin6_addr.s6_addr32[0]; 1071 pAddr->uAddr.IPv6.au32[1] = u.Ipv6.sin6_addr.s6_addr32[1]; 1072 pAddr->uAddr.IPv6.au32[2] = u.Ipv6.sin6_addr.s6_addr32[2]; 1073 pAddr->uAddr.IPv6.au32[3] = u.Ipv6.sin6_addr.s6_addr32[3]; 1074 } 1075 #endif 1076 else 1077 return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED; 1078 return VINF_SUCCESS; 1079 } 1100 return rtTcpConvertAddress(&u, cbAddr, pAddr); 1080 1101 return rtTcpError(); 1081 1102 }
Note:
See TracChangeset
for help on using the changeset viewer.