diff options
author | Greg Hudson <ghudson@mit.edu> | 2014-06-07 15:16:23 -0400 |
---|---|---|
committer | Greg Hudson <ghudson@mit.edu> | 2014-06-27 19:36:39 -0400 |
commit | ddc1c61566cc5ececc5678088f0761ce2da61ee4 (patch) | |
tree | befc666dfddc4ebddafe4f3cc87e45dce5a151b7 /src/include | |
parent | 1776fd19120d230115527febbd22979eb64ee1ff (diff) | |
download | krb5-ddc1c61566cc5ececc5678088f0761ce2da61ee4.zip krb5-ddc1c61566cc5ececc5678088f0761ce2da61ee4.tar.gz krb5-ddc1c61566cc5ececc5678088f0761ce2da61ee4.tar.bz2 |
Add socket-utils.h helpers for ports
Add sa_setport, sa_getport, and sa_is_inet helpers for conveniently
manipulating port numbers on IPv4 and IPv6 socket addresses.
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/socket-utils.h | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/include/socket-utils.h b/src/include/socket-utils.h index 484e294..e3fa429 100644 --- a/src/include/socket-utils.h +++ b/src/include/socket-utils.h @@ -89,6 +89,36 @@ static inline struct sockaddr_in6 *ss2sin6 (struct sockaddr_storage *ss) return (struct sockaddr_in6 *) ss; } +/* Set the IPv4 or IPv6 port on sa to port. Do nothing if sa is not an + * Internet socket. */ +static inline void +sa_setport(struct sockaddr *sa, uint16_t port) +{ + if (sa->sa_family == AF_INET) + sa2sin(sa)->sin_port = htons(port); + else if (sa->sa_family == AF_INET6) + sa2sin6(sa)->sin6_port = htons(port); +} + +/* Get the Internet port number of sa, or 0 if it is not an Internet socket. */ +static inline uint16_t +sa_getport(struct sockaddr *sa) +{ + if (sa->sa_family == AF_INET) + return ntohs(sa2sin(sa)->sin_port); + else if (sa->sa_family == AF_INET6) + return ntohs(sa2sin6(sa)->sin6_port); + else + return 0; +} + +/* Return true if sa is an IPv4 or IPv6 socket address. */ +static inline int +sa_is_inet(struct sockaddr *sa) +{ + return sa->sa_family == AF_INET || sa->sa_family == AF_INET6; +} + #if !defined (socklen) /* socklen_t socklen (struct sockaddr *) */ # ifdef HAVE_SA_LEN |