// ============================================================================ // neonet.yab - Yabasic Native C FFI Wrapper for Bananarchy NeoNet (libcurl) // ============================================================================ // Exposes easy HTTP/HTTPS requests (GET, POST, PUT, PATCH, DELETE, HEAD, // custom methods, custom headers, file downloads, URL encode/decode, and // response header inspection) as well as raw TCP, TLS, and UDP socket data // exchange to Yabasic programs via libffi (frnfn_call / frnfn_call$). // // Usage in your Yabasic program: // import neonet // body$ = neonet_get$("https://example.com/") // print "HTTP ", neonet_status(), " (", neonet_body_len(), " bytes)" // ============================================================================ CURL_LIB$ = "/usr/lib/libcurl.so.4" LIBC_SO$ = "/lib/ld-musl-armhf.so.1" // ---------------------------------------------------------------------------- // Socket Protocol Constants for neonet_connect(proto, host$, port) // ---------------------------------------------------------------------------- PROTO_TCP = 1 PROTO_TLS = 2 PROTO_UDP = 3 // ---------------------------------------------------------------------------- // Common libcurl Error Codes (CURLcode) // ---------------------------------------------------------------------------- CURLE_OK = 0 CURLE_UNSUPPORTED_PROTOCOL = 1 CURLE_FAILED_INIT = 2 CURLE_URL_MALFORMAT = 3 CURLE_COULDNT_RESOLVE_PROXY = 5 CURLE_COULDNT_RESOLVE_HOST = 6 CURLE_COULDNT_CONNECT = 7 CURLE_HTTP_RETURNED_ERROR = 22 CURLE_WRITE_ERROR = 23 CURLE_OPERATION_TIMEDOUT = 28 CURLE_SSL_CONNECT_ERROR = 35 CURLE_TOO_MANY_REDIRECTS = 47 CURLE_GOT_NOTHING = 52 CURLE_SEND_ERROR = 55 CURLE_RECV_ERROR = 56 CURLE_PEER_FAILED_VERIFICATION = 60 CURLE_AGAIN = 81 // ---------------------------------------------------------------------------- // Internal libcurl Option & Info Constants // ---------------------------------------------------------------------------- CURLOPT_WRITEDATA = 10001 CURLOPT_URL = 10002 CURLOPT_PORT = 3 CURLOPT_PROXY = 10004 CURLOPT_USERPWD = 10005 CURLOPT_USERAGENT = 10018 CURLOPT_HTTPHEADER = 10023 CURLOPT_HEADERDATA = 10029 CURLOPT_CUSTOMREQUEST = 10036 CURLOPT_NOBODY = 44 CURLOPT_POST = 47 CURLOPT_FOLLOWLOCATION = 52 CURLOPT_POSTFIELDSIZE = 60 CURLOPT_SSL_VERIFYPEER = 64 CURLOPT_MAXREDIRS = 68 CURLOPT_HTTPGET = 80 CURLOPT_SSL_VERIFYHOST = 81 CURLOPT_HTTP_VERSION = 84 CURLOPT_NOSIGNAL = 99 CURLOPT_ACCEPT_ENCODING = 10102 CURLOPT_TCP_NODELAY = 121 CURLOPT_CONNECT_ONLY = 141 CURLOPT_TIMEOUT_MS = 155 CURLOPT_CONNECTTIMEOUT_MS = 156 CURLOPT_COPYPOSTFIELDS = 10165 CURLOPT_SSL_ENABLE_ALPN = 226 CURL_HTTP_VERSION_1_1 = 2 CURLINFO_EFFECTIVE_URL = 1048577 CURLINFO_RESPONSE_CODE = 2097154 CURLINFO_CONTENT_TYPE = 1048594 CURLINFO_PRIMARY_IP = 1048608 CURLINFO_ACTIVESOCKET = 5242924 // ---------------------------------------------------------------------------- // Internal Module State // ---------------------------------------------------------------------------- INITIALIZED = 0 TIMEOUT_MS = 15000 CONNECT_TIMEOUT_MS = 8000 FOLLOW_REDIRECTS = 1 MAX_REDIRECTS = 10 INSECURE_TLS = 0 USER_AGENT$ = "Bananarchy-NeoNet/1.0 (libcurl)" PROXY_URL$ = "" BASIC_AUTH$ = "" MAX_HEADERS = 64 CUSTOM_HDR_COUNT = 0 dim CUSTOM_HDRS$(MAX_HEADERS) LAST_STATUS = 0 LAST_CODE = 0 LAST_ERROR$ = "" LAST_BODY$ = "" LAST_BODY_LEN = 0 LAST_HEADERS$ = "" LAST_CONTENT_TYPE$ = "" LAST_EFFECTIVE_URL$ = "" LAST_PRIMARY_IP$ = "" LAST_IO_LEN = 0 MAX_SOCKETS = 16 dim SOCK_ACTIVE(MAX_SOCKETS) dim SOCK_PROTO(MAX_SOCKETS) dim SOCK_FD(MAX_SOCKETS) dim SOCK_CURL$(MAX_SOCKETS) // ============================================================================ // Internal Helpers // ============================================================================ sub _neonet_trim$(s$) local n, a, b, c$ n = len(s$) a = 1 while a <= n c$ = mid$(s$, a, 1) if c$ <> " " and c$ <> "\t" and c$ <> chr$(13) and c$ <> chr$(10) then break fi a = a + 1 wend b = n while b >= a c$ = mid$(s$, b, 1) if c$ <> " " and c$ <> "\t" and c$ <> chr$(13) and c$ <> chr$(10) then break fi b = b - 1 wend if b < a then return "" fi return mid$(s$, a, b - a + 1) end sub sub _neonet_append_hdr_block$(slist$, hdr_block$, has_slist) local norm$, n, i, ch$, start_pos, line$, null_ptr$ if hdr_block$ = "" then return slist$ fi norm$ = "" n = len(hdr_block$) for i = 1 to n ch$ = mid$(hdr_block$, i, 1) if ch$ = chr$(10) then norm$ = norm$ + "|" elsif ch$ <> chr$(13) then norm$ = norm$ + ch$ fi next i start_pos = 1 n = len(norm$) while start_pos <= n i = instr(norm$, "|", start_pos) if i = 0 then line$ = _neonet_trim$(mid$(norm$, start_pos, n - start_pos + 1)) start_pos = n + 1 else line$ = _neonet_trim$(mid$(norm$, start_pos, i - start_pos)) start_pos = i + 1 fi if line$ <> "" then if has_slist = 0 and slist$ = "" then null_ptr$ = frnbf_alloc$(-1) slist$ = frnfn_call$(CURL_LIB$, "buffer", "curl_slist_append", "buffer", null_ptr$, "string", line$) else slist$ = frnfn_call$(CURL_LIB$, "buffer", "curl_slist_append", "buffer", slist$, "string", line$) fi fi wend return slist$ end sub sub _neonet_get_info_str$(curl$, info_id) local box$, r, addr, p$, val$ box$ = frnbf_alloc$(4) r = frnfn_call(CURL_LIB$, "int", "curl_easy_getinfo", "buffer", curl$, "int", info_id, "buffer", box$) val$ = "" if r = 0 then addr = frnbf_get(box$, 0, "uint32") if addr <> 0 then p$ = frnbf_get_buffer$(box$, 0) val$ = frnfn_call$(LIBC_SO$, "string", "strdup", "buffer", p$) fi fi frnbf_free box$ return val$ end sub sub _neonet_poll_fd(fd, events, timeout_ms) local pfd$, pr if fd < 0 then return -1 fi pfd$ = frnbf_alloc$(8) frnbf_set pfd$, 0, "int", fd frnbf_set pfd$, 4, "short", events frnbf_set pfd$, 6, "short", 0 pr = frnfn_call(LIBC_SO$, "int", "poll", "buffer", pfd$, "int", 1, "int", timeout_ms) frnbf_free pfd$ return pr end sub // ============================================================================ // 1. Lifecycle, Version & Configuration // ============================================================================ export sub neonet_init() local rc, i if INITIALIZED = 1 then return 1 fi rc = frnfn_call(CURL_LIB$, "int", "curl_global_init", "int", 3) if rc <> 0 then LAST_CODE = rc LAST_ERROR$ = "curl_global_init failed (" + str$(rc) + ")" return 0 fi for i = 0 to MAX_SOCKETS - 1 SOCK_ACTIVE(i) = 0 SOCK_PROTO(i) = 0 SOCK_FD(i) = -1 SOCK_CURL$(i) = "" next i INITIALIZED = 1 LAST_CODE = 0 LAST_ERROR$ = "" return 1 end sub export sub neonet_cleanup() local i, r if INITIALIZED = 0 then return 1 fi for i = 0 to MAX_SOCKETS - 1 if SOCK_ACTIVE(i) = 1 then r = neonet_close(i) fi next i CUSTOM_HDR_COUNT = 0 r = frnfn_call(CURL_LIB$, "int", "curl_global_cleanup") INITIALIZED = 0 return 1 end sub export sub neonet_version$() return frnfn_call$(CURL_LIB$, "string", "curl_version", "options", "copy_string_result") end sub export sub neonet_strerror$(curl_code) return frnfn_call$(CURL_LIB$, "string", "curl_easy_strerror", "int", curl_code, "options", "copy_string_result") end sub export sub neonet_set_timeout(timeout_ms) if timeout_ms > 0 then TIMEOUT_MS = timeout_ms fi return TIMEOUT_MS end sub export sub neonet_set_connect_timeout(connect_timeout_ms) if connect_timeout_ms > 0 then CONNECT_TIMEOUT_MS = connect_timeout_ms fi return CONNECT_TIMEOUT_MS end sub export sub neonet_set_user_agent(ua$) USER_AGENT$ = ua$ return 1 end sub export sub neonet_set_follow_redirects(enabled) if enabled <> 0 then FOLLOW_REDIRECTS = 1 else FOLLOW_REDIRECTS = 0 fi return FOLLOW_REDIRECTS end sub export sub neonet_set_max_redirects(max_redirs) if max_redirs >= 0 then MAX_REDIRECTS = max_redirs fi return MAX_REDIRECTS end sub export sub neonet_set_insecure(insecure) if insecure <> 0 then INSECURE_TLS = 1 else INSECURE_TLS = 0 fi return INSECURE_TLS end sub export sub neonet_set_proxy(proxy_url$) PROXY_URL$ = proxy_url$ return 1 end sub export sub neonet_set_basic_auth(user_pwd$) BASIC_AUTH$ = user_pwd$ return 1 end sub // ============================================================================ // 2. Custom Request Headers & URL Encoding // ============================================================================ export sub neonet_clear_headers() CUSTOM_HDR_COUNT = 0 return 1 end sub export sub neonet_add_header(header_line$) local clean$ clean$ = _neonet_trim$(header_line$) if clean$ = "" then return CUSTOM_HDR_COUNT fi if CUSTOM_HDR_COUNT < MAX_HEADERS then CUSTOM_HDR_COUNT = CUSTOM_HDR_COUNT + 1 CUSTOM_HDRS$(CUSTOM_HDR_COUNT) = clean$ fi return CUSTOM_HDR_COUNT end sub export sub neonet_url_encode$(text$) local r, curl$, res$ if INITIALIZED = 0 then r = neonet_init() fi if text$ = "" then return "" fi curl$ = frnfn_call$(CURL_LIB$, "buffer", "curl_easy_init") res$ = frnfn_call$(CURL_LIB$, "string", "curl_easy_escape", "buffer", curl$, "string", text$, "int", len(text$)) r = frnfn_call(CURL_LIB$, "int", "curl_easy_cleanup", "buffer", curl$) return res$ end sub export sub neonet_url_decode$(text$) local r, curl$, outlen$, res$ if INITIALIZED = 0 then r = neonet_init() fi if text$ = "" then return "" fi curl$ = frnfn_call$(CURL_LIB$, "buffer", "curl_easy_init") outlen$ = frnbf_alloc$(4) res$ = frnfn_call$(CURL_LIB$, "string", "curl_easy_unescape", "buffer", curl$, "string", text$, "int", len(text$), "buffer", outlen$) frnbf_free outlen$ r = frnfn_call(CURL_LIB$, "int", "curl_easy_cleanup", "buffer", curl$) return res$ end sub // ============================================================================ // 3. HTTP / HTTPS Request Execution // ============================================================================ export sub neonet_request$(method$, url$, body$, headers$) local r, i, verb$, curl$, slist$, null_ptr$ local body_ptr_box$, body_len_box$, body_fp$, body_buf$ local hdr_ptr_box$, hdr_len_box$, hdr_fp$, hdr_buf$, hdr_len local code_box$ if INITIALIZED = 0 then r = neonet_init() fi LAST_STATUS = 0 LAST_CODE = 0 LAST_ERROR$ = "" LAST_BODY$ = "" LAST_BODY_LEN = 0 LAST_HEADERS$ = "" LAST_CONTENT_TYPE$ = "" LAST_EFFECTIVE_URL$ = "" LAST_PRIMARY_IP$ = "" LAST_IO_LEN = 0 verb$ = upper$(_neonet_trim$(method$)) if verb$ = "" then verb$ = "GET" fi curl$ = frnfn_call$(CURL_LIB$, "buffer", "curl_easy_init") if curl$ = "" then LAST_CODE = CURLE_FAILED_INIT LAST_ERROR$ = "curl_easy_init failed" return "" fi body_ptr_box$ = frnbf_alloc$(4) body_len_box$ = frnbf_alloc$(4) body_fp$ = frnfn_call$(LIBC_SO$, "buffer", "open_memstream", "buffer", body_ptr_box$, "buffer", body_len_box$) hdr_ptr_box$ = frnbf_alloc$(4) hdr_len_box$ = frnbf_alloc$(4) hdr_fp$ = frnfn_call$(LIBC_SO$, "buffer", "open_memstream", "buffer", hdr_ptr_box$, "buffer", hdr_len_box$) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_URL, "string", url$) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_NOSIGNAL, "int", 1) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_TIMEOUT_MS, "int", TIMEOUT_MS) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_CONNECTTIMEOUT_MS, "int", CONNECT_TIMEOUT_MS) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_FOLLOWLOCATION, "int", FOLLOW_REDIRECTS) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_MAXREDIRS, "int", MAX_REDIRECTS) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_ACCEPT_ENCODING, "string", "") r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_WRITEDATA, "buffer", body_fp$) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_HEADERDATA, "buffer", hdr_fp$) if USER_AGENT$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_USERAGENT, "string", USER_AGENT$) fi if PROXY_URL$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_PROXY, "string", PROXY_URL$) fi if BASIC_AUTH$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_USERPWD, "string", BASIC_AUTH$) fi if INSECURE_TLS <> 0 then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_SSL_VERIFYPEER, "int", 0) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_SSL_VERIFYHOST, "int", 0) else r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_SSL_VERIFYPEER, "int", 1) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_SSL_VERIFYHOST, "int", 2) fi // Build curl_slist from queued headers and inline headers$ slist$ = "" for i = 1 to CUSTOM_HDR_COUNT if slist$ = "" then null_ptr$ = frnbf_alloc$(-1) slist$ = frnfn_call$(CURL_LIB$, "buffer", "curl_slist_append", "buffer", null_ptr$, "string", CUSTOM_HDRS$(i)) else slist$ = frnfn_call$(CURL_LIB$, "buffer", "curl_slist_append", "buffer", slist$, "string", CUSTOM_HDRS$(i)) fi next i if headers$ <> "" then slist$ = _neonet_append_hdr_block$(slist$, headers$, slist$ <> "") fi if slist$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_HTTPHEADER, "buffer", slist$) fi // Configure HTTP verb and body if verb$ = "GET" and body$ = "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_HTTPGET, "int", 1) elsif verb$ = "HEAD" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_NOBODY, "int", 1) elsif verb$ = "POST" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_POST, "int", 1) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_POSTFIELDSIZE, "int", len(body$)) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_COPYPOSTFIELDS, "string", body$) else r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_CUSTOMREQUEST, "string", verb$) if body$ <> "" or verb$ = "PUT" or verb$ = "PATCH" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_POSTFIELDSIZE, "int", len(body$)) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_COPYPOSTFIELDS, "string", body$) fi fi LAST_CODE = frnfn_call(CURL_LIB$, "int", "curl_easy_perform", "buffer", curl$) r = frnfn_call(LIBC_SO$, "int", "fclose", "buffer", body_fp$) r = frnfn_call(LIBC_SO$, "int", "fclose", "buffer", hdr_fp$) if LAST_CODE = 0 then code_box$ = frnbf_alloc$(4) r = frnfn_call(CURL_LIB$, "int", "curl_easy_getinfo", "buffer", curl$, "int", CURLINFO_RESPONSE_CODE, "buffer", code_box$) LAST_STATUS = frnbf_get(code_box$, 0, "int") frnbf_free code_box$ LAST_CONTENT_TYPE$ = _neonet_get_info_str$(curl$, CURLINFO_CONTENT_TYPE) LAST_EFFECTIVE_URL$ = _neonet_get_info_str$(curl$, CURLINFO_EFFECTIVE_URL) LAST_PRIMARY_IP$ = _neonet_get_info_str$(curl$, CURLINFO_PRIMARY_IP) else LAST_ERROR$ = neonet_strerror$(LAST_CODE) fi LAST_BODY_LEN = frnbf_get(body_len_box$, 0, "int") LAST_IO_LEN = LAST_BODY_LEN body_buf$ = frnbf_get_buffer$(body_ptr_box$, 0) if LAST_BODY_LEN > 0 then LAST_BODY$ = frnbf_get$(body_buf$, 0, LAST_BODY_LEN) fi r = frnfn_call(LIBC_SO$, "int", "free", "buffer", body_buf$) hdr_len = frnbf_get(hdr_len_box$, 0, "int") hdr_buf$ = frnbf_get_buffer$(hdr_ptr_box$, 0) if hdr_len > 0 then LAST_HEADERS$ = frnbf_get$(hdr_buf$, 0, hdr_len) fi r = frnfn_call(LIBC_SO$, "int", "free", "buffer", hdr_buf$) frnbf_free body_ptr_box$ frnbf_free body_len_box$ frnbf_free hdr_ptr_box$ frnbf_free hdr_len_box$ if slist$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_slist_free_all", "buffer", slist$) fi r = frnfn_call(CURL_LIB$, "int", "curl_easy_cleanup", "buffer", curl$) return LAST_BODY$ end sub export sub neonet_get$(url$) return neonet_request$("GET", url$, "", "") end sub export sub neonet_get_ex$(url$, headers$) return neonet_request$("GET", url$, "", headers$) end sub export sub neonet_post$(url$, body$, content_type$, headers$) local hdrs$ hdrs$ = headers$ if content_type$ <> "" then if hdrs$ <> "" then hdrs$ = "Content-Type: " + content_type$ + "|" + hdrs$ else hdrs$ = "Content-Type: " + content_type$ fi fi return neonet_request$("POST", url$, body$, hdrs$) end sub export sub neonet_put$(url$, body$, content_type$, headers$) local hdrs$ hdrs$ = headers$ if content_type$ <> "" then if hdrs$ <> "" then hdrs$ = "Content-Type: " + content_type$ + "|" + hdrs$ else hdrs$ = "Content-Type: " + content_type$ fi fi return neonet_request$("PUT", url$, body$, hdrs$) end sub export sub neonet_patch$(url$, body$, content_type$, headers$) local hdrs$ hdrs$ = headers$ if content_type$ <> "" then if hdrs$ <> "" then hdrs$ = "Content-Type: " + content_type$ + "|" + hdrs$ else hdrs$ = "Content-Type: " + content_type$ fi fi return neonet_request$("PATCH", url$, body$, hdrs$) end sub export sub neonet_delete$(url$, headers$) return neonet_request$("DELETE", url$, "", headers$) end sub export sub neonet_head(url$, headers$) local unused$ unused$ = neonet_request$("HEAD", url$, "", headers$) return LAST_STATUS end sub export sub neonet_download(url$, dest_path$, headers$) local r, i, curl$, fp$, slist$, null_ptr$, code_box$, addr local hdr_ptr_box$, hdr_len_box$, hdr_fp$, hdr_buf$, hdr_len if INITIALIZED = 0 then r = neonet_init() fi LAST_STATUS = 0 LAST_CODE = 0 LAST_ERROR$ = "" LAST_BODY$ = "" LAST_BODY_LEN = 0 LAST_HEADERS$ = "" fp$ = frnfn_call$(LIBC_SO$, "buffer", "fopen", "string", dest_path$, "string", "wb") if fp$ = "frnbf:-1:(nil)" or fp$ = "frnbf:-1:0x0" or fp$ = "frnbf:-1:0" then LAST_CODE = CURLE_WRITE_ERROR LAST_ERROR$ = "Cannot open file for writing: " + dest_path$ return 0 fi curl$ = frnfn_call$(CURL_LIB$, "buffer", "curl_easy_init") hdr_ptr_box$ = frnbf_alloc$(4) hdr_len_box$ = frnbf_alloc$(4) hdr_fp$ = frnfn_call$(LIBC_SO$, "buffer", "open_memstream", "buffer", hdr_ptr_box$, "buffer", hdr_len_box$) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_URL, "string", url$) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_NOSIGNAL, "int", 1) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_TIMEOUT_MS, "int", TIMEOUT_MS) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_CONNECTTIMEOUT_MS, "int", CONNECT_TIMEOUT_MS) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_FOLLOWLOCATION, "int", FOLLOW_REDIRECTS) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_MAXREDIRS, "int", MAX_REDIRECTS) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_ACCEPT_ENCODING, "string", "") r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_WRITEDATA, "buffer", fp$) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_HEADERDATA, "buffer", hdr_fp$) if USER_AGENT$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_USERAGENT, "string", USER_AGENT$) fi if PROXY_URL$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_PROXY, "string", PROXY_URL$) fi if BASIC_AUTH$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_USERPWD, "string", BASIC_AUTH$) fi if INSECURE_TLS <> 0 then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_SSL_VERIFYPEER, "int", 0) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_SSL_VERIFYHOST, "int", 0) fi slist$ = "" for i = 1 to CUSTOM_HDR_COUNT if slist$ = "" then null_ptr$ = frnbf_alloc$(-1) slist$ = frnfn_call$(CURL_LIB$, "buffer", "curl_slist_append", "buffer", null_ptr$, "string", CUSTOM_HDRS$(i)) else slist$ = frnfn_call$(CURL_LIB$, "buffer", "curl_slist_append", "buffer", slist$, "string", CUSTOM_HDRS$(i)) fi next i if headers$ <> "" then slist$ = _neonet_append_hdr_block$(slist$, headers$, slist$ <> "") fi if slist$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_HTTPHEADER, "buffer", slist$) fi LAST_CODE = frnfn_call(CURL_LIB$, "int", "curl_easy_perform", "buffer", curl$) r = frnfn_call(LIBC_SO$, "int", "fclose", "buffer", fp$) r = frnfn_call(LIBC_SO$, "int", "fclose", "buffer", hdr_fp$) if LAST_CODE = 0 then code_box$ = frnbf_alloc$(4) r = frnfn_call(CURL_LIB$, "int", "curl_easy_getinfo", "buffer", curl$, "int", CURLINFO_RESPONSE_CODE, "buffer", code_box$) LAST_STATUS = frnbf_get(code_box$, 0, "int") frnbf_free code_box$ LAST_CONTENT_TYPE$ = _neonet_get_info_str$(curl$, CURLINFO_CONTENT_TYPE) LAST_EFFECTIVE_URL$ = _neonet_get_info_str$(curl$, CURLINFO_EFFECTIVE_URL) LAST_PRIMARY_IP$ = _neonet_get_info_str$(curl$, CURLINFO_PRIMARY_IP) else LAST_ERROR$ = neonet_strerror$(LAST_CODE) fi hdr_len = frnbf_get(hdr_len_box$, 0, "int") hdr_buf$ = frnbf_get_buffer$(hdr_ptr_box$, 0) if hdr_len > 0 then LAST_HEADERS$ = frnbf_get$(hdr_buf$, 0, hdr_len) fi r = frnfn_call(LIBC_SO$, "int", "free", "buffer", hdr_buf$) frnbf_free hdr_ptr_box$ frnbf_free hdr_len_box$ if slist$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_slist_free_all", "buffer", slist$) fi r = frnfn_call(CURL_LIB$, "int", "curl_easy_cleanup", "buffer", curl$) if LAST_CODE = 0 and LAST_STATUS >= 200 and LAST_STATUS < 400 then return 1 fi return 0 end sub // ============================================================================ // 4. HTTP Response Metadata & Header Inspection // ============================================================================ export sub neonet_status() return LAST_STATUS end sub export sub neonet_last_code() return LAST_CODE end sub export sub neonet_error$() return LAST_ERROR$ end sub export sub neonet_body$() return LAST_BODY$ end sub export sub neonet_body_len() return LAST_BODY_LEN end sub export sub neonet_last_len() return LAST_IO_LEN end sub export sub neonet_response_headers$() return LAST_HEADERS$ end sub export sub neonet_content_type$() return LAST_CONTENT_TYPE$ end sub export sub neonet_effective_url$() return LAST_EFFECTIVE_URL$ end sub export sub neonet_primary_ip$() return LAST_PRIMARY_IP$ end sub export sub neonet_header$(name$) local target$, tlen, pos, nl, line$, colon, k$, matched$ target$ = lower$(_neonet_trim$(name$)) tlen = len(target$) if tlen = 0 or LAST_HEADERS$ = "" then return "" fi matched$ = "" pos = 1 while pos <= len(LAST_HEADERS$) nl = instr(LAST_HEADERS$, chr$(10), pos) if nl = 0 then line$ = _neonet_trim$(mid$(LAST_HEADERS$, pos, len(LAST_HEADERS$) - pos + 1)) pos = len(LAST_HEADERS$) + 1 else line$ = _neonet_trim$(mid$(LAST_HEADERS$, pos, nl - pos)) pos = nl + 1 fi colon = instr(line$, ":") if colon > 1 then k$ = lower$(_neonet_trim$(left$(line$, colon - 1))) if k$ = target$ then matched$ = _neonet_trim$(mid$(line$, colon + 1, len(line$) - colon)) fi fi wend return matched$ end sub // ============================================================================ // 5. TCP, TLS & UDP Socket Data Exchange // ============================================================================ export sub neonet_connect(proto, host$, port) local r, slot, i, url$, curl$, sock_box$, fd local hints$, res_box$, ai$, fam, stype, iproto, addrlen, addr$ if INITIALIZED = 0 then r = neonet_init() fi LAST_CODE = 0 LAST_ERROR$ = "" LAST_IO_LEN = 0 slot = -1 for i = 0 to MAX_SOCKETS - 1 if SOCK_ACTIVE(i) = 0 then slot = i break fi next i if slot < 0 then LAST_ERROR$ = "No free NeoNet socket slots available" return -1 fi if proto = PROTO_TCP or proto = PROTO_TLS then if proto = PROTO_TLS then url$ = "https://" + host$ + ":" + str$(port) else url$ = "http://" + host$ + ":" + str$(port) fi curl$ = frnfn_call$(CURL_LIB$, "buffer", "curl_easy_init") r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_URL, "string", url$) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_NOSIGNAL, "int", 1) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_CONNECTTIMEOUT_MS, "int", CONNECT_TIMEOUT_MS) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_TIMEOUT_MS, "int", TIMEOUT_MS) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_TCP_NODELAY, "int", 1) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_HTTP_VERSION, "int", CURL_HTTP_VERSION_1_1) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_SSL_ENABLE_ALPN, "int", 0) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_CONNECT_ONLY, "int", 1) if PROXY_URL$ <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_PROXY, "string", PROXY_URL$) fi if INSECURE_TLS <> 0 then r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_SSL_VERIFYPEER, "int", 0) r = frnfn_call(CURL_LIB$, "int", "curl_easy_setopt", "buffer", curl$, "int", CURLOPT_SSL_VERIFYHOST, "int", 0) fi LAST_CODE = frnfn_call(CURL_LIB$, "int", "curl_easy_perform", "buffer", curl$) if LAST_CODE <> 0 then LAST_ERROR$ = neonet_strerror$(LAST_CODE) r = frnfn_call(CURL_LIB$, "int", "curl_easy_cleanup", "buffer", curl$) return -1 fi sock_box$ = frnbf_alloc$(4) r = frnfn_call(CURL_LIB$, "int", "curl_easy_getinfo", "buffer", curl$, "int", CURLINFO_ACTIVESOCKET, "buffer", sock_box$) fd = frnbf_get(sock_box$, 0, "int") frnbf_free sock_box$ LAST_PRIMARY_IP$ = _neonet_get_info_str$(curl$, CURLINFO_PRIMARY_IP) SOCK_ACTIVE(slot) = 1 SOCK_PROTO(slot) = proto SOCK_FD(slot) = fd SOCK_CURL$(slot) = curl$ return slot elsif proto = PROTO_UDP then hints$ = frnbf_alloc$(32) frnbf_set hints$, 4, "int", 0 // AF_UNSPEC (IPv4 or IPv6) frnbf_set hints$, 8, "int", 2 // SOCK_DGRAM frnbf_set hints$, 12, "int", 17 // IPPROTO_UDP res_box$ = frnbf_alloc$(4) r = frnfn_call(LIBC_SO$, "int", "getaddrinfo", "string", host$, "string", str$(port), "buffer", hints$, "buffer", res_box$) frnbf_free hints$ if r <> 0 then frnbf_free res_box$ LAST_CODE = CURLE_COULDNT_RESOLVE_HOST LAST_ERROR$ = "UDP getaddrinfo failed for " + host$ + ":" + str$(port) return -1 fi ai$ = frnbf_get_buffer$(res_box$, 0) fam = frnbf_get(ai$, 4, "int") stype = frnbf_get(ai$, 8, "int") iproto = frnbf_get(ai$, 12, "int") addrlen = frnbf_get(ai$, 16, "int") addr$ = frnbf_get_buffer$(ai$, 20) fd = frnfn_call(LIBC_SO$, "int", "socket", "int", fam, "int", stype, "int", iproto) if fd < 0 then r = frnfn_call(LIBC_SO$, "int", "freeaddrinfo", "buffer", ai$) frnbf_free res_box$ LAST_CODE = CURLE_COULDNT_CONNECT LAST_ERROR$ = "UDP socket() creation failed" return -1 fi r = frnfn_call(LIBC_SO$, "int", "connect", "int", fd, "buffer", addr$, "int", addrlen) r2 = frnfn_call(LIBC_SO$, "int", "freeaddrinfo", "buffer", ai$) frnbf_free res_box$ if r <> 0 then r2 = frnfn_call(LIBC_SO$, "int", "close", "int", fd) LAST_CODE = CURLE_COULDNT_CONNECT LAST_ERROR$ = "UDP connect() failed" return -1 fi SOCK_ACTIVE(slot) = 1 SOCK_PROTO(slot) = PROTO_UDP SOCK_FD(slot) = fd SOCK_CURL$(slot) = "" return slot fi LAST_ERROR$ = "Invalid socket protocol: " + str$(proto) return -1 end sub export sub neonet_tcp_connect(host$, port) return neonet_connect(PROTO_TCP, host$, port) end sub export sub neonet_tls_connect(host$, port) return neonet_connect(PROTO_TLS, host$, port) end sub export sub neonet_udp_open(host$, port) return neonet_connect(PROTO_UDP, host$, port) end sub export sub neonet_send(sock_id, data$) local total, slen, rem$, sent_box$, rc, n, pr if sock_id < 0 or sock_id >= MAX_SOCKETS then LAST_ERROR$ = "Invalid socket handle" return -1 fi if SOCK_ACTIVE(sock_id) = 0 then LAST_ERROR$ = "Socket is not open" return -1 fi slen = len(data$) if slen = 0 then return 0 fi if SOCK_PROTO(sock_id) = PROTO_UDP then n = frnfn_call(LIBC_SO$, "int", "send", "int", SOCK_FD(sock_id), "string", data$, "int", slen, "int", 0) if n < 0 then LAST_CODE = CURLE_SEND_ERROR LAST_ERROR$ = "UDP send() failed" return -1 fi LAST_IO_LEN = n return n fi sent_box$ = frnbf_alloc$(4) total = 0 rem$ = data$ while total < slen rc = frnfn_call(CURL_LIB$, "int", "curl_easy_send", "buffer", SOCK_CURL$(sock_id), "string", rem$, "int", len(rem$), "buffer", sent_box$) if rc = 0 then n = frnbf_get(sent_box$, 0, "int") total = total + n if total < slen then rem$ = right$(data$, slen - total) fi elsif rc = CURLE_AGAIN then pr = _neonet_poll_fd(SOCK_FD(sock_id), 4, TIMEOUT_MS) if pr <= 0 then frnbf_free sent_box$ LAST_CODE = CURLE_OPERATION_TIMEDOUT LAST_ERROR$ = "Socket send timed out" return -1 fi else frnbf_free sent_box$ LAST_CODE = rc LAST_ERROR$ = neonet_strerror$(rc) return -1 fi wend frnbf_free sent_box$ LAST_IO_LEN = total return total end sub export sub neonet_send_bytes(sock_id, frn_buf$, byte_len) local sent_box$, rc, n, pr if sock_id < 0 or sock_id >= MAX_SOCKETS then LAST_ERROR$ = "Invalid socket handle" return -1 fi if SOCK_ACTIVE(sock_id) = 0 then LAST_ERROR$ = "Socket is not open" return -1 fi if byte_len <= 0 then return 0 fi if SOCK_PROTO(sock_id) = PROTO_UDP then n = frnfn_call(LIBC_SO$, "int", "send", "int", SOCK_FD(sock_id), "buffer", frn_buf$, "int", byte_len, "int", 0) if n < 0 then LAST_CODE = CURLE_SEND_ERROR LAST_ERROR$ = "UDP send_bytes() failed" return -1 fi LAST_IO_LEN = n return n fi sent_box$ = frnbf_alloc$(4) while 1 rc = frnfn_call(CURL_LIB$, "int", "curl_easy_send", "buffer", SOCK_CURL$(sock_id), "buffer", frn_buf$, "int", byte_len, "buffer", sent_box$) if rc = 0 then n = frnbf_get(sent_box$, 0, "int") frnbf_free sent_box$ LAST_IO_LEN = n return n elsif rc = CURLE_AGAIN then pr = _neonet_poll_fd(SOCK_FD(sock_id), 4, TIMEOUT_MS) if pr <= 0 then frnbf_free sent_box$ LAST_CODE = CURLE_OPERATION_TIMEDOUT LAST_ERROR$ = "Socket send_bytes timed out" return -1 fi else frnbf_free sent_box$ LAST_CODE = rc LAST_ERROR$ = neonet_strerror$(rc) return -1 fi wend end sub export sub neonet_recv_bytes(sock_id, frn_buf$, max_bytes, timeout_ms) local rx_box$, rc, n, pr, wait_ms, elapsed, step_ms LAST_IO_LEN = 0 if sock_id < 0 or sock_id >= MAX_SOCKETS then LAST_ERROR$ = "Invalid socket handle" return -1 fi if SOCK_ACTIVE(sock_id) = 0 then LAST_ERROR$ = "Socket is not open" return -1 fi if max_bytes <= 0 then return 0 fi wait_ms = timeout_ms if wait_ms < 0 then wait_ms = TIMEOUT_MS fi if SOCK_PROTO(sock_id) = PROTO_UDP then pr = _neonet_poll_fd(SOCK_FD(sock_id), 1, wait_ms) if pr <= 0 then LAST_CODE = CURLE_OPERATION_TIMEDOUT return 0 fi n = frnfn_call(LIBC_SO$, "int", "recv", "int", SOCK_FD(sock_id), "buffer", frn_buf$, "int", max_bytes, "int", 0) if n < 0 then LAST_CODE = CURLE_RECV_ERROR LAST_ERROR$ = "UDP recv() failed" return -1 fi LAST_IO_LEN = n return n fi rx_box$ = frnbf_alloc$(4) elapsed = 0 step_ms = 200 while 1 rc = frnfn_call(CURL_LIB$, "int", "curl_easy_recv", "buffer", SOCK_CURL$(sock_id), "buffer", frn_buf$, "int", max_bytes, "buffer", rx_box$) if rc = 0 then n = frnbf_get(rx_box$, 0, "int") frnbf_free rx_box$ LAST_IO_LEN = n LAST_CODE = 0 return n elsif rc = CURLE_AGAIN then if elapsed >= wait_ms then frnbf_free rx_box$ LAST_CODE = CURLE_AGAIN return 0 fi if wait_ms - elapsed < step_ms then step_ms = wait_ms - elapsed fi pr = _neonet_poll_fd(SOCK_FD(sock_id), 1, step_ms) elapsed = elapsed + step_ms if pr < 0 then frnbf_free rx_box$ LAST_CODE = CURLE_RECV_ERROR return -1 fi else frnbf_free rx_box$ LAST_CODE = rc LAST_ERROR$ = neonet_strerror$(rc) return -1 fi wend end sub export sub neonet_recv$(sock_id, max_bytes, timeout_ms) local cap, rx_buf$, n, out$ cap = max_bytes if cap <= 0 then cap = 4096 fi rx_buf$ = frnbf_alloc$(cap) n = neonet_recv_bytes(sock_id, rx_buf$, cap, timeout_ms) out$ = "" if n > 0 then out$ = frnbf_get$(rx_buf$, 0, n) fi frnbf_free rx_buf$ return out$ end sub export sub neonet_recv_all$(sock_id, max_bytes, timeout_ms) local cap, chunk_sz, rx_buf$, n, out$, wait_ms cap = max_bytes if cap <= 0 then cap = 65536 fi chunk_sz = 4096 if cap < chunk_sz then chunk_sz = cap fi rx_buf$ = frnbf_alloc$(chunk_sz) out$ = "" wait_ms = timeout_ms if wait_ms <= 0 then wait_ms = TIMEOUT_MS fi while len(out$) < cap n = neonet_recv_bytes(sock_id, rx_buf$, chunk_sz, wait_ms) if n <= 0 then break fi out$ = out$ + frnbf_get$(rx_buf$, 0, n) // After receiving the first chunk, use a shorter idle window for remaining chunks if wait_ms > 800 then wait_ms = 800 fi wend frnbf_free rx_buf$ LAST_IO_LEN = len(out$) return out$ end sub export sub neonet_close(sock_id) local r if sock_id < 0 or sock_id >= MAX_SOCKETS then return 0 fi if SOCK_ACTIVE(sock_id) = 0 then return 0 fi if SOCK_PROTO(sock_id) = PROTO_UDP then if SOCK_FD(sock_id) >= 0 then r = frnfn_call(LIBC_SO$, "int", "close", "int", SOCK_FD(sock_id)) fi else if SOCK_CURL$(sock_id) <> "" then r = frnfn_call(CURL_LIB$, "int", "curl_easy_cleanup", "buffer", SOCK_CURL$(sock_id)) fi fi SOCK_ACTIVE(sock_id) = 0 SOCK_PROTO(sock_id) = 0 SOCK_FD(sock_id) = -1 SOCK_CURL$(sock_id) = "" return 1 end sub // ============================================================================ // 6. One-Shot TCP, TLS & UDP Exchange Helpers // ============================================================================ export sub neonet_tcp_exchange$(host$, port, payload$, timeout_ms) local s, r, resp$ s = neonet_tcp_connect(host$, port) if s < 0 then return "" fi if payload$ <> "" then r = neonet_send(s, payload$) if r < 0 then r = neonet_close(s) return "" fi fi resp$ = neonet_recv_all$(s, 32768, timeout_ms) r = neonet_close(s) return resp$ end sub export sub neonet_tls_exchange$(host$, port, payload$, timeout_ms) local s, r, resp$ s = neonet_tls_connect(host$, port) if s < 0 then return "" fi if payload$ <> "" then r = neonet_send(s, payload$) if r < 0 then r = neonet_close(s) return "" fi fi resp$ = neonet_recv_all$(s, 32768, timeout_ms) r = neonet_close(s) return resp$ end sub export sub neonet_udp_exchange$(host$, port, payload$, timeout_ms) local s, r, resp$ s = neonet_udp_open(host$, port) if s < 0 then return "" fi if payload$ <> "" then r = neonet_send(s, payload$) if r < 0 then r = neonet_close(s) return "" fi fi resp$ = neonet_recv$(s, 4096, timeout_ms) r = neonet_close(s) return resp$ end sub export sub neonet_system_dns$() local fp$, buf$, p$, line$, ns$, r fp$ = frnfn_call$(LIBC_SO$, "buffer", "fopen", "string", "/etc/resolv.conf", "string", "r") if fp$ = "frnbf:-1:(nil)" or fp$ = "frnbf:-1:0x0" or fp$ = "frnbf:-1:0" then return "1.1.1.1" fi buf$ = frnbf_alloc$(256) ns$ = "1.1.1.1" while 1 p$ = frnfn_call$(LIBC_SO$, "buffer", "fgets", "buffer", buf$, "int", 255, "buffer", fp$) if p$ = "frnbf:-1:(nil)" or p$ = "frnbf:-1:0x0" or p$ = "frnbf:-1:0" then break fi line$ = _neonet_trim$(frnbf_get$(buf$, 0, 255)) if left$(line$, 10) = "nameserver" then ns$ = _neonet_trim$(mid$(line$, 11, len(line$) - 10)) if ns$ <> "" then break fi fi wend frnbf_free buf$ r = frnfn_call(LIBC_SO$, "int", "fclose", "buffer", fp$) return ns$ end sub export sub neonet_udp_dns_a$(dns_server$, domain$, timeout_ms) local srv$, clean_dom$, s, tx$, rx$, pos, i, n, ch$, lbl_start, lbl_len local sent, recvd, ancount, qdcount, rcode, b, rtype, rdlen, ip$, r srv$ = _neonet_trim$(dns_server$) if srv$ = "" then srv$ = neonet_system_dns$() fi clean_dom$ = _neonet_trim$(domain$) if clean_dom$ = "" then LAST_ERROR$ = "Empty domain name" return "" fi s = neonet_udp_open(srv$, 53) if s < 0 then return "" fi tx$ = frnbf_alloc$(512) // DNS Header: ID=0x424e ("BN"), Flags=0x0100 (Standard query, RD=1), QDCOUNT=1 frnbf_set tx$, 0, "uint8", 66 frnbf_set tx$, 1, "uint8", 78 frnbf_set tx$, 2, "uint8", 1 frnbf_set tx$, 3, "uint8", 0 frnbf_set tx$, 4, "uint8", 0 frnbf_set tx$, 5, "uint8", 1 pos = 12 lbl_start = 1 n = len(clean_dom$) for i = 1 to n + 1 if i <= n then ch$ = mid$(clean_dom$, i, 1) else ch$ = "." fi if ch$ = "." then lbl_len = i - lbl_start if lbl_len > 0 and lbl_len < 64 and pos + lbl_len + 1 < 500 then frnbf_set tx$, pos, "uint8", lbl_len frnbf_set tx$, pos + 1, mid$(clean_dom$, lbl_start, lbl_len) pos = pos + 1 + lbl_len fi lbl_start = i + 1 fi next i // Root label (0) + QTYPE=1 (A) + QCLASS=1 (IN) frnbf_set tx$, pos, "uint8", 0 frnbf_set tx$, pos + 1, "uint8", 0 frnbf_set tx$, pos + 2, "uint8", 1 frnbf_set tx$, pos + 3, "uint8", 0 frnbf_set tx$, pos + 4, "uint8", 1 pos = pos + 5 sent = neonet_send_bytes(s, tx$, pos) frnbf_free tx$ if sent < 0 then r = neonet_close(s) return "" fi rx$ = frnbf_alloc$(512) recvd = neonet_recv_bytes(s, rx$, 512, timeout_ms) r = neonet_close(s) if recvd < 12 then frnbf_free rx$ if LAST_ERROR$ = "" then LAST_ERROR$ = "UDP DNS query timed out (" + srv$ + ":53)" fi return "" fi rcode = mod(frnbf_get(rx$, 3, "uint8"), 16) qdcount = frnbf_get(rx$, 4, "uint8") * 256 + frnbf_get(rx$, 5, "uint8") ancount = frnbf_get(rx$, 6, "uint8") * 256 + frnbf_get(rx$, 7, "uint8") if rcode <> 0 or ancount <= 0 then frnbf_free rx$ LAST_ERROR$ = "DNS RCODE=" + str$(rcode) + " ANCOUNT=" + str$(ancount) return "" fi pos = 12 for i = 1 to qdcount while pos < recvd b = frnbf_get(rx$, pos, "uint8") if b = 0 then pos = pos + 1 break elsif b >= 192 then pos = pos + 2 break else pos = pos + 1 + b fi wend pos = pos + 4 next i ip$ = "" for i = 1 to ancount if pos >= recvd then break fi while pos < recvd b = frnbf_get(rx$, pos, "uint8") if b = 0 then pos = pos + 1 break elsif b >= 192 then pos = pos + 2 break else pos = pos + 1 + b fi wend if pos + 10 > recvd then break fi rtype = frnbf_get(rx$, pos, "uint8") * 256 + frnbf_get(rx$, pos + 1, "uint8") rdlen = frnbf_get(rx$, pos + 8, "uint8") * 256 + frnbf_get(rx$, pos + 9, "uint8") pos = pos + 10 if rtype = 1 and rdlen = 4 and pos + 4 <= recvd then ip$ = str$(frnbf_get(rx$, pos, "uint8")) + "." + str$(frnbf_get(rx$, pos + 1, "uint8")) + "." + str$(frnbf_get(rx$, pos + 2, "uint8")) + "." + str$(frnbf_get(rx$, pos + 3, "uint8")) break fi pos = pos + rdlen next i frnbf_free rx$ if ip$ = "" and LAST_ERROR$ = "" then LAST_ERROR$ = "No IPv4 A record in DNS response" fi return ip$ end sub