Skip to content

Commit

Permalink
Merge #1614
Browse files Browse the repository at this point in the history
1614: Improve the sockaddr interface: r=asomers a=asomers

* All sockaddr newtypes should be repr(transparent)
* All sockaddr newtypes should be opaque, so the user can't do something
  like change the sa_family field in a way that violates invariants.

This is a prerequisite for #1544.

Co-authored-by: Alan Somers <asomers@gmail.com>
  • Loading branch information
bors[bot] and asomers committed Dec 24, 2021
2 parents 6c6c576 + 867ac51 commit 7be98a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Fixed
### Removed

- Removed public access to the inner fields of `NetlinkAddr`, `AlgAddr`,
`SysControlAddr`, `LinkAddr`, and `VsockAddr`.
(#[1614](https://github.com/nix-rust/nix/pull/1614))

## [0.23.1] - 2021-12-16

### Added
Expand Down
21 changes: 14 additions & 7 deletions src/sys/socket/addr.rs
Expand Up @@ -473,6 +473,7 @@ impl fmt::Display for IpAddr {
*/

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(transparent)]
pub struct Ipv4Addr(pub libc::in_addr);

impl Ipv4Addr {
Expand Down Expand Up @@ -522,6 +523,7 @@ impl fmt::Display for Ipv4Addr {
*/

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(transparent)]
pub struct Ipv6Addr(pub libc::in6_addr);

// Note that IPv6 addresses are stored in big endian order on all architectures.
Expand Down Expand Up @@ -1062,7 +1064,8 @@ pub mod netlink {
use std::{fmt, mem};

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct NetlinkAddr(pub sockaddr_nl);
#[repr(transparent)]
pub struct NetlinkAddr(pub(in super::super) sockaddr_nl);

impl NetlinkAddr {
pub fn new(pid: u32, groups: u32) -> NetlinkAddr {
Expand Down Expand Up @@ -1099,7 +1102,8 @@ pub mod alg {
use std::ffi::CStr;

#[derive(Copy, Clone)]
pub struct AlgAddr(pub sockaddr_alg);
#[repr(transparent)]
pub struct AlgAddr(pub(in super::super) sockaddr_alg);

// , PartialEq, Eq, Debug, Hash
impl PartialEq for AlgAddr {
Expand Down Expand Up @@ -1179,9 +1183,9 @@ pub mod sys_control {

ioctl_readwrite!(ctl_info, CTL_IOC_MAGIC, CTL_IOC_INFO, ctl_ioc_info);

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SysControlAddr(pub libc::sockaddr_ctl);
#[repr(transparent)]
pub struct SysControlAddr(pub(in super::super) libc::sockaddr_ctl);

impl SysControlAddr {
pub const fn new(id: u32, unit: u32) -> SysControlAddr {
Expand Down Expand Up @@ -1238,7 +1242,8 @@ mod datalink {

/// Hardware Address
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct LinkAddr(pub libc::sockaddr_ll);
#[repr(transparent)]
pub struct LinkAddr(pub(in super::super) libc::sockaddr_ll);

impl LinkAddr {
/// Always AF_PACKET
Expand Down Expand Up @@ -1315,7 +1320,8 @@ mod datalink {

/// Hardware Address
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct LinkAddr(pub libc::sockaddr_dl);
#[repr(transparent)]
pub struct LinkAddr(pub(in super::super) libc::sockaddr_dl);

impl LinkAddr {
/// Total length of sockaddr
Expand Down Expand Up @@ -1408,7 +1414,8 @@ pub mod vsock {
use std::hash::{Hash, Hasher};

#[derive(Copy, Clone)]
pub struct VsockAddr(pub sockaddr_vm);
#[repr(transparent)]
pub struct VsockAddr(pub(in super::super) sockaddr_vm);

impl PartialEq for VsockAddr {
fn eq(&self, other: &Self) -> bool {
Expand Down

0 comments on commit 7be98a4

Please sign in to comment.