authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2019-10-21 14:26:15-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-21 23:12:35-04:00
log87b11617b55254abab267e8739ee2f0d926fbcf5
tree8677ff7d47b1140d05e5bcff8723ccbc175b0341
parent2550cb4638e12e94073cbca5c0ee3aa263c25dbe

Fix accept function API

The sockaddr pointer and size of the accept function points to a data structure that can only be determined at runtime. The only requirement is that it must be large enough to hold 2 bytes for the address family value. Typeical usage of the socket API is for UDP/TCP IPv4 and IPv6 sockets, which use sockaddr_in and sockaddr_in6. And some sockets can actually support both simultaneously in which case the app may want to have access to the size of the returned sockaddr. Operating systems can even support custom protocols where they use custom sockaddr data structures. In this case the standard library would have no knowledge of the actual size of the sockaddr being passed into the accept function. In this case the standard library should defer to the app to pass in the size of their structure.

1 files changed, 4 insertions(+), 6 deletions(-)

lib/std/os.zig+4-6
...@@ -1478,10 +1478,9 @@ pub const AcceptError = error{...@@ -1478,10 +1478,9 @@ pub const AcceptError = error{
14781478
1479/// Accept a connection on a socket. `fd` must be opened in blocking mode.1479/// Accept a connection on a socket. `fd` must be opened in blocking mode.
1480/// See also `accept4_async`.1480/// See also `accept4_async`.
1481pub fn accept4(fd: i32, addr: *sockaddr, flags: u32) AcceptError!i32 {1481pub fn accept4(fd: i32, addr: *sockaddr, addrSize: *usize, flags: u32) AcceptError!i32 {
1482 while (true) {1482 while (true) {
1483 var sockaddr_size = u32(@sizeOf(sockaddr));1483 const rc = system.accept4(fd, addr, addrSize, flags);
1484 const rc = system.accept4(fd, addr, &sockaddr_size, flags);
1485 switch (errno(rc)) {1484 switch (errno(rc)) {
1486 0 => return @intCast(i32, rc),1485 0 => return @intCast(i32, rc),
1487 EINTR => continue,1486 EINTR => continue,
...@@ -1506,10 +1505,9 @@ pub fn accept4(fd: i32, addr: *sockaddr, flags: u32) AcceptError!i32 {...@@ -1506,10 +1505,9 @@ pub fn accept4(fd: i32, addr: *sockaddr, flags: u32) AcceptError!i32 {
15061505
1507/// This is the same as `accept4` except `fd` is expected to be non-blocking.1506/// This is the same as `accept4` except `fd` is expected to be non-blocking.
1508/// Returns -1 if would block.1507/// Returns -1 if would block.
1509pub fn accept4_async(fd: i32, addr: *sockaddr, flags: u32) AcceptError!i32 {1508pub fn accept4_async(fd: i32, addr: *sockaddr, addrSize: *usize, flags: u32) AcceptError!i32 {
1510 while (true) {1509 while (true) {
1511 var sockaddr_size = u32(@sizeOf(sockaddr));1510 const rc = system.accept4(fd, addr, addrSize, flags);
1512 const rc = system.accept4(fd, addr, &sockaddr_size, flags);
1513 switch (errno(rc)) {1511 switch (errno(rc)) {
1514 0 => return @intCast(i32, rc),1512 0 => return @intCast(i32, rc),
1515 EINTR => continue,1513 EINTR => continue,