Skip to content

Commit

Permalink
cleanup MAC presentation address handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed Jan 27, 2024
1 parent 1c21013 commit 3b66028
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions Sources/CLinuxSockAddr/CLinuxSockAddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
28 changes: 13 additions & 15 deletions Sources/IORingUtils/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -593,16 +593,14 @@ extension sockaddr_ll: SocketAddress, @unchecked Sendable {
return byte
}

guard bytes.count == 6 else { throw Errno.invalidArgument }
guard bytes.count == ETH_ALEN else { throw Errno.invalidArgument }

try withUnsafeMutablePointer(to: &sll.sll_addr) { addr in
withUnsafeMutablePointer(to: &sll.sll_addr) { addr in
let start = addr.propertyBasePointer(to: \.0)!
let capacity = MemoryLayout.size(ofValue: addr.pointee)
if capacity <= presentationAddress.utf8.count {
throw Errno.outOfRange
}
precondition(capacity >= ETH_ALEN)
_ = start.withMemoryRebound(to: UInt8.self, capacity: capacity) { dst in
memcpy(UnsafeMutableRawPointer(mutating: dst), bytes, capacity)
memcpy(UnsafeMutableRawPointer(mutating: dst), bytes, Int(ETH_ALEN))
}
}
self = sll
Expand All @@ -614,15 +612,15 @@ extension sockaddr_ll: SocketAddress, @unchecked Sendable {

public var presentationAddress: String {
get throws {
String(
format: "%02x:%02x:%02x:%02x:%02x:%02x",
sll_addr.0,
sll_addr.1,
sll_addr.2,
sll_addr.3,
sll_addr.4,
sll_addr.5
)
var sll = self
return withUnsafeMutablePointer(to: &sll.sll_addr) { addr in
let start = addr.propertyBasePointer(to: \.0)!
let capacity = MemoryLayout.size(ofValue: addr.pointee)
return start.withMemoryRebound(to: UInt8.self, capacity: capacity) { dst in
UnsafeBufferPointer(start: dst, count: Int(ETH_ALEN)).map { String(format: "%02x", $0) }
.joined(separator: ":")
}
}
}
}

Expand Down

0 comments on commit 3b66028

Please sign in to comment.