Skip to content

POSIX & Networking Support #1271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks
kristate opened this issue Jul 21, 2018 · 3 comments
Closed
6 tasks

POSIX & Networking Support #1271

kristate opened this issue Jul 21, 2018 · 3 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@kristate
Copy link
Contributor

kristate commented Jul 21, 2018

Tracking issue for building-out std.os.posix.* and friends.

OS Support

Async API

Memos

// TODO build abstractions for other operating systems

kristate added a commit to kristate/zig that referenced this issue Jul 21, 2018
kristate added a commit to kristate/zig that referenced this issue Jul 21, 2018
kristate added a commit to kristate/zig that referenced this issue Jul 21, 2018
kristate added a commit to kristate/zig that referenced this issue Jul 21, 2018
@kristate kristate changed the title POSIX abstractions for other operating systems POSIX & Networking Support Jul 22, 2018
@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Jul 22, 2018
@andrewrk andrewrk added this to the 0.4.0 milestone Jul 22, 2018
@andrewrk andrewrk added the standard library This issue involves writing Zig code for the standard library. label Jul 22, 2018
@andrewrk andrewrk modified the milestones: 0.4.0, 0.5.0 Feb 15, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 20, 2019
@ghost
Copy link

ghost commented Sep 30, 2019

looks like sockets are already available?

zig/lib/std/net.zig

Lines 219 to 247 in 5026db1

pub fn connectUnixSocket(path: []const u8) !std.fs.File {
const opt_non_block = if (std.event.Loop.instance != null) os.SOCK_NONBLOCK else 0;
const sockfd = try os.socket(
os.AF_UNIX,
os.SOCK_STREAM | os.SOCK_CLOEXEC | opt_non_block,
0,
);
errdefer os.close(sockfd);
var sock_addr = os.sockaddr{
.un = os.sockaddr_un{
.family = os.AF_UNIX,
.path = undefined,
},
};
if (path.len > @typeOf(sock_addr.un.path).len) return error.NameTooLong;
mem.copy(u8, sock_addr.un.path[0..], path);
const size = @intCast(u32, @sizeOf(os.sa_family_t) + path.len);
if (std.event.Loop.instance) |loop| {
try os.connect_async(sockfd, &sock_addr, size);
try loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET);
try os.getsockoptError(sockfd);
} else {
try os.connect(sockfd, &sock_addr, size);
}
return std.fs.File.openHandle(sockfd);
}

@shawnl
Copy link
Contributor

shawnl commented Sep 30, 2019

@cup But that is not asynchronous.

@ghost
Copy link

ghost commented Sep 30, 2019

@shawnl yeah, it is:

try os.connect_async(sockfd, &sock_addr, size);

zig/lib/std/os.zig

Lines 1752 to 1776 in 5026db1

pub fn connect_async(sockfd: i32, sock_addr: *sockaddr, len: socklen_t) ConnectError!void {
while (true) {
switch (errno(system.connect(sockfd, sock_addr, len))) {
EINVAL => unreachable,
EINTR => continue,
0, EINPROGRESS => return,
EACCES => return error.PermissionDenied,
EPERM => return error.PermissionDenied,
EADDRINUSE => return error.AddressInUse,
EADDRNOTAVAIL => return error.AddressNotAvailable,
EAFNOSUPPORT => return error.AddressFamilyNotSupported,
EAGAIN => return error.SystemResources,
EALREADY => unreachable, // The socket is nonblocking and a previous connection attempt has not yet been completed.
EBADF => unreachable, // sockfd is not a valid open file descriptor.
ECONNREFUSED => return error.ConnectionRefused,
EFAULT => unreachable, // The socket structure address is outside the user's address space.
EISCONN => unreachable, // The socket is already connected.
ENETUNREACH => return error.NetworkUnreachable,
ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket.
EPROTOTYPE => unreachable, // The socket type does not support the requested communications protocol.
ETIMEDOUT => return error.ConnectionTimedOut,
else => |err| return unexpectedErrno(err),
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

3 participants