Use hint as getaddrinfo 2nd arg

This commit is contained in:
Rémy F 2025-06-29 12:40:10 +00:00
parent ce1c90dfbe
commit 9b4e078274
No known key found for this signature in database
3 changed files with 47 additions and 24 deletions

View File

@ -2,17 +2,23 @@
///@ts-check ///@ts-check
/// <reference path="../doc/globals.d.ts" /> /// <reference path="../doc/globals.d.ts" />
/// <reference path="../doc/os.d.ts" /> /// <reference path="../doc/os.d.ts" />
/// <reference path="../doc/std.d.ts" />
import * as os from "os"; import * as os from "os";
import * as std from "std";
/** @template T @param {os.Result<T>} result */ /** @template T @param {os.Result<T>} result */
function must(result) { function must(result) {
if (typeof result === "number" && result < 0) throw result; if (typeof result === "number" && result < 0) throw new Error(std.strerror(-result));
return /** @type {T} */ (result) return /** @type {T} */ (result)
} }
//USAGE: client.js wttr.in/paris
const sockfd = must(os.socket(os.AF_INET, os.SOCK_STREAM)); const uriRegexp = /^(?<host>[A-Za-z0-9\-\.]+)(?<port>:[0-9]+)?(?<query>.*)$/;
const addr = os.getaddrinfo("bellard.org",'80').find(a => a.family == os.AF_INET); const { host = "bellard.org", port = ":80", query = "/" } = scriptArgs[1]?.match(uriRegexp)?.groups || {};
console.log("sending GET on",{ host, port, query })
const [addr] = must(os.getaddrinfo(host, { service: port.slice(1) }));
const sockfd = must(os.socket(addr.family, addr.socktype));
await os.connect(sockfd, addr); await os.connect(sockfd, addr);
const httpReq = Uint8Array.from("GET / HTTP/1.0\r\n\r\n", c => c.charCodeAt(0)) const httpReq = Uint8Array.from(`GET ${query||'/'} HTTP/1.0\r\nHost: ${host}\r\nUser-Agent: curl\r\n\r\n`, c => c.charCodeAt(0))
must(await os.send(sockfd, httpReq.buffer) > 0); must(await os.send(sockfd, httpReq.buffer) > 0);
const chunk = new Uint8Array(512); const chunk = new Uint8Array(512);
const recvd = await os.recv(sockfd, chunk.buffer); const recvd = await os.recv(sockfd, chunk.buffer);

View File

@ -2,7 +2,9 @@
///@ts-check ///@ts-check
/// <reference path="../doc/globals.d.ts" /> /// <reference path="../doc/globals.d.ts" />
/// <reference path="../doc/os.d.ts" /> /// <reference path="../doc/os.d.ts" />
/// <reference path="../doc/std.d.ts" />
import * as os from "os"; import * as os from "os";
import * as std from "std";// for std.strerror
const MIMES = new Map([ const MIMES = new Map([
['html', 'text/html'], ['html', 'text/html'],
@ -17,7 +19,7 @@ const MIMES = new Map([
]); ]);
/** @template T @param {os.Result<T>} result */ /** @template T @param {os.Result<T>} result */
function must(result) { function must(result) {
if (typeof result === "number" && result < 0) throw result; if (typeof result === "number" && result < 0) throw new Error(std.strerror(-result));
return /** @type {T} */ (result) return /** @type {T} */ (result)
} }
/**@param {os.FileDescriptor} fd */ /**@param {os.FileDescriptor} fd */
@ -40,7 +42,7 @@ function sendLines(fd, lines) {
} }
//USAGE: qjs http_server.js [PORT=8080 [HOST=localhost]] //USAGE: qjs http_server.js [PORT=8080 [HOST=localhost]]
const [port = "8080", host = "localhost"] = scriptArgs.slice(1); const [port = "8080", host = "localhost"] = scriptArgs.slice(1);
const ai = os.getaddrinfo(host, port).find(a => a.family == os.AF_INET); const [ai] = must(os.getaddrinfo(host, { service: port }));
//if (!ai.length) throw `Unable to getaddrinfo(${host}, ${port})`; //if (!ai.length) throw `Unable to getaddrinfo(${host}, ${port})`;
const sock_srv = must(os.socket(os.AF_INET, os.SOCK_STREAM)); const sock_srv = must(os.socket(os.AF_INET, os.SOCK_STREAM));
must(os.setsockopt(sock_srv, os.SO_REUSEADDR, new Uint32Array([1]).buffer)); must(os.setsockopt(sock_srv, os.SO_REUSEADDR, new Uint32Array([1]).buffer));

View File

@ -3710,27 +3710,42 @@ static JSValue js_os_get_setsockopt(JSContext *ctx, JSValueConst this_val,
static JSValue js_os_getaddrinfo(JSContext *ctx, JSValueConst this_val, static JSValue js_os_getaddrinfo(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv) int argc, JSValueConst *argv)
{ {
int ret; int ret = -1;
socklen_t objLen; if (!JS_IsString(argv[0]))
JSValue obj, addrObj; return JS_EXCEPTION;
const char* node = NULL; const char* node = JS_ToCString(ctx, argv[0]);
const char* service = NULL; const char* service = NULL;
struct addrinfo *ai,*it; struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
if (!JS_IsNull(argv[0]) && !JS_IsUndefined(argv[0])) if (!JS_IsNull(argv[1]) && JS_IsObject(argv[1])) {
node = JS_ToCString(ctx, argv[0]); JSValue prop_service = JS_GetPropertyStr(ctx, argv[1], "service");
JSValue prop_family = JS_GetPropertyStr(ctx, argv[1], "family");
service = JS_ToCString(ctx, argv[1]); JSValue prop_socktype = JS_GetPropertyStr(ctx, argv[1], "socktype");
if (!service) if (JS_IsException(prop_service) ||
JS_IsException(prop_family) ||
JS_IsException(prop_socktype))
goto fail; goto fail;
if (!JS_IsUndefined(prop_service))
service = JS_ToCString(ctx, prop_service);
if (!JS_IsUndefined(prop_family))
JS_ToInt32(ctx, &hints.ai_family, prop_family);
if (!JS_IsUndefined(prop_socktype))
JS_ToInt32(ctx, &hints.ai_socktype, prop_socktype);
}
ret = js_get_sockerrno(getaddrinfo(node, service, NULL, &ai)); struct addrinfo *ai;
ret = js_get_sockerrno(getaddrinfo(node, service, &hints, &ai));
if (ret) if (ret)
goto fail; goto fail;
obj = JS_NewArray(ctx); struct addrinfo *it;
socklen_t objLen;
JSValue obj = JS_NewArray(ctx);
for (objLen = 0, it = ai; it; it = it->ai_next, objLen++) { for (objLen = 0, it = ai; it; it = it->ai_next, objLen++) {
addrObj = JS_ToSockaddrObj(ctx,(struct sockaddr_storage *)it->ai_addr); JSValue addrObj = JS_ToSockaddrObj(ctx,(struct sockaddr_storage *)it->ai_addr);
JSValue prop_socktype = JS_NewUint32(ctx, it->ai_socktype);
JS_DefinePropertyValueStr(ctx, addrObj, "socktype", prop_socktype, JS_PROP_C_W_E);
JS_SetPropertyUint32(ctx,obj,objLen,addrObj); JS_SetPropertyUint32(ctx,obj,objLen,addrObj);
} }
@ -3742,7 +3757,7 @@ fail:
JS_FreeValue(ctx, obj); JS_FreeValue(ctx, obj);
JS_FreeCString(ctx, service); JS_FreeCString(ctx, service);
JS_FreeCString(ctx, node); JS_FreeCString(ctx, node);
return JS_EXCEPTION; return JS_NewInt32(ctx, ret);
} }
static JSValue js_os_bind(JSContext *ctx, JSValueConst this_val, static JSValue js_os_bind(JSContext *ctx, JSValueConst this_val,