Skip to content
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

Clean up error throwing code in UDP sockets #16959

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions src/bun.js/api/bun/udp_socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,18 @@ pub const UDPSocket = struct {
return true;
}

pub fn setBroadcast(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue {
fn throwEBADFIfNecessary(this: *This, globalThis: *JSGlobalObject) bun.JSError!void {
if (this.closed) {
return globalThis.throwValue(bun.JSC.Maybe(void).errnoSys(@as(i32, @intCast(@intFromEnum(std.posix.E.BADF))), .setsockopt).?.toJS(globalThis));
return globalThis.throwValue(bun.sys.Error.fromCode(.BADF, .setsockopt).toJSC(globalThis));
}
}

fn throwEINVAL(globalThis: *JSGlobalObject) bun.JSError!void {
return globalThis.throwValue(bun.sys.Error.fromCode(.INVAL, .setsockopt).toJSC(globalThis));
}

pub fn setBroadcast(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue {
try this.throwEBADFIfNecessary(globalThis);

const arguments = callframe.arguments();
if (arguments.len < 1) {
Expand All @@ -415,9 +423,7 @@ pub const UDPSocket = struct {
}

pub fn setMulticastLoopback(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue {
if (this.closed) {
return globalThis.throwValue(bun.JSC.Maybe(void).errnoSys(@as(i32, @intCast(@intFromEnum(std.posix.E.BADF))), .setsockopt).?.toJS(globalThis));
}
try this.throwEBADFIfNecessary(globalThis);

const arguments = callframe.arguments();
if (arguments.len < 1) {
Expand All @@ -435,9 +441,7 @@ pub const UDPSocket = struct {
}

fn setMembership(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame, drop: bool) bun.JSError!JSValue {
if (this.closed) {
return globalThis.throwValue(bun.JSC.Maybe(void).errnoSys(@as(i32, @intCast(@intFromEnum(std.posix.E.BADF))), .setsockopt).?.toJS(globalThis));
}
try this.throwEBADFIfNecessary(globalThis);

const arguments = callframe.arguments();
if (arguments.len < 1) {
Expand All @@ -446,7 +450,7 @@ pub const UDPSocket = struct {

var addr: std.posix.sockaddr.storage = undefined;
if (!parseAddr(this, globalThis, JSC.jsNumber(0), arguments[0], &addr)) {
return globalThis.throwValue(bun.JSC.Maybe(void).errnoSys(@as(i32, @intCast(@intFromEnum(std.posix.E.INVAL))), .setsockopt).?.toJS(globalThis));
try throwEINVAL(globalThis);
}

var interface: std.posix.sockaddr.storage = undefined;
Expand Down Expand Up @@ -474,9 +478,7 @@ pub const UDPSocket = struct {
}

fn setSourceSpecificMembership(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame, drop: bool) bun.JSError!JSValue {
if (this.closed) {
return globalThis.throwValue(bun.JSC.Maybe(void).errnoSys(@as(i32, @intCast(@intFromEnum(std.posix.E.BADF))), .setsockopt).?.toJS(globalThis));
}
try this.throwEBADFIfNecessary(globalThis);

const arguments = callframe.arguments();
if (arguments.len < 2) {
Expand All @@ -485,19 +487,19 @@ pub const UDPSocket = struct {

var source_addr: std.posix.sockaddr.storage = undefined;
if (!parseAddr(this, globalThis, JSC.jsNumber(0), arguments[0], &source_addr)) {
return globalThis.throwValue(bun.JSC.Maybe(void).errnoSys(@as(i32, @intCast(@intFromEnum(std.posix.E.INVAL))), .setsockopt).?.toJS(globalThis));
try throwEINVAL(globalThis);
}

var group_addr: std.posix.sockaddr.storage = undefined;
if (!parseAddr(this, globalThis, JSC.jsNumber(0), arguments[1], &group_addr)) {
return globalThis.throwValue(bun.JSC.Maybe(void).errnoSys(@as(i32, @intCast(@intFromEnum(std.posix.E.INVAL))), .setsockopt).?.toJS(globalThis));
try throwEINVAL(globalThis);
}

if (source_addr.family != group_addr.family) {
return globalThis.throwInvalidArguments("Family mismatch between source and group addresses", .{});
}

var interface: std.posix.sockaddr.storage = undefined;
var interface: std.posix.sockaddr.storage = std.mem.zeroes(std.posix.sockaddr.storage);

const res = if (arguments.len > 2 and parseAddr(this, globalThis, JSC.jsNumber(0), arguments[2], &interface)) blk: {
if (source_addr.family != interface.family) {
Expand All @@ -522,16 +524,14 @@ pub const UDPSocket = struct {
}

pub fn setMulticastInterface(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue {
if (this.closed) {
return globalThis.throwValue(bun.JSC.Maybe(void).errnoSys(@as(i32, @intCast(@intFromEnum(std.posix.E.BADF))), .setsockopt).?.toJS(globalThis));
}
try this.throwEBADFIfNecessary(globalThis);

const arguments = callframe.arguments();
if (arguments.len < 1) {
return globalThis.throwInvalidArguments("Expected 1 argument, got {}", .{arguments.len});
}

var addr: std.posix.sockaddr.storage = undefined;
var addr: std.posix.sockaddr.storage = std.mem.zeroes(std.posix.sockaddr.storage);

if (!parseAddr(this, globalThis, JSC.jsNumber(0), arguments[0], &addr)) {
return .false;
Expand Down Expand Up @@ -578,9 +578,7 @@ pub const UDPSocket = struct {
}

fn setAnyTTL(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame, comptime function: fn (*uws.udp.Socket, i32) c_int) bun.JSError!JSValue {
if (this.closed) {
return globalThis.throwValue(bun.JSC.Maybe(void).errnoSys(@as(i32, @intCast(@intFromEnum(std.posix.E.BADF))), .setsockopt).?.toJS(globalThis));
}
try this.throwEBADFIfNecessary(globalThis);

const arguments = callframe.arguments();
if (arguments.len < 1) {
Expand Down Expand Up @@ -622,10 +620,10 @@ pub const UDPSocket = struct {
defer arena.deinit();
const alloc = arena.allocator();

var payloads = alloc.alloc([*]const u8, len) catch bun.outOfMemory();
var lens = alloc.alloc(usize, len) catch bun.outOfMemory();
var addr_ptrs = alloc.alloc(?*const anyopaque, len) catch bun.outOfMemory();
var addrs = alloc.alloc(std.posix.sockaddr.storage, len) catch bun.outOfMemory();
var payloads = alloc.alloc([*]const u8, len) catch return globalThis.throwOutOfMemory();
var lens = alloc.alloc(usize, len) catch return globalThis.throwOutOfMemory();
var addr_ptrs = alloc.alloc(?*const anyopaque, len) catch return globalThis.throwOutOfMemory();
var addrs = alloc.alloc(std.posix.sockaddr.storage, len) catch return globalThis.throwOutOfMemory();

var iter = arg.arrayIterator(globalThis);

Expand Down Expand Up @@ -678,6 +676,7 @@ pub const UDPSocket = struct {
if (this.closed) {
return globalThis.throw("Socket is closed", .{});
}

const arguments = callframe.arguments_old(3);
const dst: ?Destination = brk: {
if (this.connect_info != null) {
Expand Down