Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/tscore/ink_inet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,16 @@ struct IpAddr {
size_t len ///< [in] Size of buffer.
) const;

/** Write the address to a socket address.
*
* @param dest Socket address to update.
* @return @a dest
*
* The address and family are updated. The port is unchanged. @a dest is assumed to be the
* appropriate underlying type for the family of @a this.
*/
sockaddr *toSockAddr(sockaddr *dest) const;

/// Equality.
bool
operator==(self const &that) const
Expand Down
21 changes: 21 additions & 0 deletions src/tscore/ink_inet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,27 @@ IpAddr::toString(char *dest, size_t len) const
return dest;
}

sockaddr *
IpAddr::toSockAddr(sockaddr *dest) const
{
if (AF_INET == _family) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use a switch(_family)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only two values and those values are not compact, not sure a switch makes it any better.

dest->sa_family = AF_INET;
ats_ip4_addr_cast(dest) = _addr._ip4;
#if HAVE_STRUCT_SOCKADDR_SA_LEN
dest->sa_len = sizeof(sockaddr_in);
#endif
} else if (AF_INET6 == _family) {
dest->sa_family = AF_INET6;
ats_ip6_addr_cast(dest) = _addr._ip6;
#if HAVE_STRUCT_SOCKADDR_SA_LEN
dest->sa_len = sizeof(sockaddr_in6);
#endif
} else {
dest->sa_family = AF_UNSPEC;
Copy link
Contributor

@cukiernik cukiernik May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe return nullptr, as a fail indicator?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends on whether you want to crash on use of an invalid instance. The common use case will be something like

sockaddr_storage sa;
int result = bind(sock_fd, addr.toSockAddr(&sa), options);

Currently this will fail on an invalid addr and be indicated by result having a error value. If instead nullptr was returned the invalid addr would cause a crash. As a user of this, I'd prefer the former.

}
return dest;
}

bool
IpAddr::isMulticast() const
{
Expand Down