Skip to content

Commit bb3ce44

Browse files
Make slightly better use of SockAddr in TCP code. (project-chip#26483)
Since we have a union for our type-punning, we don't need to do reinterpret_cast.
1 parent 8635ca4 commit bb3ce44

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

src/inet/TCPEndPointImplSockets.cpp

+25-26
Original file line numberDiff line numberDiff line change
@@ -96,40 +96,42 @@ CHIP_ERROR TCPEndPointImplSockets::BindImpl(IPAddressType addrType, const IPAddr
9696

9797
if (res == CHIP_NO_ERROR)
9898
{
99+
SockAddr sa;
100+
memset(&sa, 0, sizeof(sa));
101+
socklen_t sockaddrsize = 0;
102+
99103
if (addrType == IPAddressType::kIPv6)
100104
{
101-
struct sockaddr_in6 sa;
102-
memset(&sa, 0, sizeof(sa));
103-
sa.sin6_family = AF_INET6;
104-
sa.sin6_port = htons(port);
105-
sa.sin6_flowinfo = 0;
106-
sa.sin6_addr = addr.ToIPv6();
107-
sa.sin6_scope_id = 0;
108-
109-
if (bind(mSocket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
110-
{
111-
res = CHIP_ERROR_POSIX(errno);
112-
}
105+
sa.in6.sin6_family = AF_INET6;
106+
sa.in6.sin6_port = htons(port);
107+
sa.in6.sin6_flowinfo = 0;
108+
sa.in6.sin6_addr = addr.ToIPv6();
109+
sa.in6.sin6_scope_id = 0;
110+
111+
sockaddrsize = sizeof(sa.in6);
113112
}
114113
#if INET_CONFIG_ENABLE_IPV4
115114
else if (addrType == IPAddressType::kIPv4)
116115
{
117-
struct sockaddr_in sa;
118-
memset(&sa, 0, sizeof(sa));
119-
sa.sin_family = AF_INET;
120-
sa.sin_port = htons(port);
121-
sa.sin_addr = addr.ToIPv4();
116+
sa.in.sin_family = AF_INET;
117+
sa.in.sin_port = htons(port);
118+
sa.in.sin_addr = addr.ToIPv4();
122119

123-
if (bind(mSocket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
124-
{
125-
res = CHIP_ERROR_POSIX(errno);
126-
}
120+
sockaddrsize = sizeof(sa.in);
127121
}
128122
#endif // INET_CONFIG_ENABLE_IPV4
129123
else
130124
{
131125
res = INET_ERROR_WRONG_ADDRESS_TYPE;
132126
}
127+
128+
if (res == CHIP_NO_ERROR)
129+
{
130+
if (bind(mSocket, &sa.any, sockaddrsize) != 0)
131+
{
132+
res = CHIP_ERROR_POSIX(errno);
133+
}
134+
}
133135
}
134136

135137
return res;
@@ -218,8 +220,7 @@ CHIP_ERROR TCPEndPointImplSockets::ConnectImpl(const IPAddress & addr, uint16_t
218220
int flags = fcntl(mSocket, F_GETFL, 0);
219221
fcntl(mSocket, F_SETFL, flags | O_NONBLOCK);
220222

221-
socklen_t sockaddrsize = 0;
222-
const sockaddr * sockaddrptr = nullptr;
223+
socklen_t sockaddrsize = 0;
223224

224225
SockAddr sa;
225226
memset(&sa, 0, sizeof(sa));
@@ -232,7 +233,6 @@ CHIP_ERROR TCPEndPointImplSockets::ConnectImpl(const IPAddress & addr, uint16_t
232233
sa.in6.sin6_addr = addr.ToIPv6();
233234
sa.in6.sin6_scope_id = intfId.GetPlatformInterface();
234235
sockaddrsize = sizeof(sockaddr_in6);
235-
sockaddrptr = reinterpret_cast<const sockaddr *>(&sa.in6);
236236
}
237237
#if INET_CONFIG_ENABLE_IPV4
238238
else if (addrType == IPAddressType::kIPv4)
@@ -241,15 +241,14 @@ CHIP_ERROR TCPEndPointImplSockets::ConnectImpl(const IPAddress & addr, uint16_t
241241
sa.in.sin_port = htons(port);
242242
sa.in.sin_addr = addr.ToIPv4();
243243
sockaddrsize = sizeof(sockaddr_in);
244-
sockaddrptr = reinterpret_cast<const sockaddr *>(&sa.in);
245244
}
246245
#endif // INET_CONFIG_ENABLE_IPV4
247246
else
248247
{
249248
return INET_ERROR_WRONG_ADDRESS_TYPE;
250249
}
251250

252-
int conRes = connect(mSocket, sockaddrptr, sockaddrsize);
251+
int conRes = connect(mSocket, &sa.any, sockaddrsize);
253252

254253
if (conRes == -1 && errno != EINPROGRESS)
255254
{

0 commit comments

Comments
 (0)