Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omtls/blob/main/src/net.om # omtls omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0). Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omtls / src / net.om` 475 lines ยท 9279 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note net.om: name resolution and TCP for omtls (prefix ot_dn_ and ot_io_). 3 note Resolution order: a dotted IPv4 literal; then /etc/hosts (IPv4 lines); then one DNS A query over UDP to the 4 note first IPv4 "nameserver" of /etc/resolv.conf (RFC 1035 4.1; recursion desired). The answer is not authenticated: 5 note TLS certificate validation of the host name is what protects the connection, not DNS. IPv6 is not supported in 6 note this slice (stated in the README). All failures are defined refusals (negative results), never traps. 7 note Results: the resolved address is written to the caller's 4-byte array; return 0, or 8 note -1 not a resolvable name, -2 no nameserver, -3 DNS I/O or timeout, -4 malformed or failed DNS answer. 9 global ot_dn_buf uchar `1048576` 10 global ot_dn_v ulong 8 11 text ot_dn_t_hosts "/etc/hosts" 12 text ot_dn_t_resolv "/etc/resolv.conf" 13 text ot_dn_t_ns "nameserver" 14 const OT_DN_NS long `5000000000` 15 16 note ---- dotted IPv4 literal: true and the 4 bytes when s is exactly a.b.c.d with each part 0..255 ---- 17 fn ot_dn_ipv4 s view uchar * a edit uchar 4 to bool 18 n = len s 19 pi = ulong 0 20 v = ulong 0 21 digits = ulong 0 22 for i ulong n 23 c = get s i 24 if eq c 46 25 if or eq digits 0 ge pi 3 26 ret false 27 end 28 put a pi uchar v 29 pi = add pi 1 30 v = 0 31 digits = 0 32 continue 33 end 34 if or lt c 48 gt c 57 35 ret false 36 end 37 v = add mul v 10 ulong sub c 48 38 digits = add digits 1 39 if or gt v 255 gt digits 3 40 ret false 41 end 42 end 43 if or ne pi 3 eq digits 0 44 ret false 45 end 46 put a 3 uchar v 47 ret true 48 end 49 50 note ---- read a small system file into ot_dn_buf; length or -1 ---- 51 fn ot_dn_load path view uchar * to long 52 fd = fopen path 0 len path 0 0 53 if lt fd 0 54 ret -1 55 end 56 n = ulong 0 57 cap = len ot_dn_buf 58 while lt n cap bound `9223372036854775807` 59 k = read int fd ot_dn_buf n sub cap n 60 if eq k -4 61 continue 62 end 63 if le k 0 64 break 65 end 66 n = add n ulong k 67 end 68 z = fclose int fd 69 ret long n 70 end 71 72 note ASCII case-insensitive comparison of ot_dn_buf[p, p+n) with s 73 fn ot_dn_bufeq p ulong n ulong s view uchar * to bool 74 if ne n len s 75 ret false 76 end 77 for i ulong n 78 x = get ot_dn_buf add p i 79 y = get s i 80 if and ge x 65 le x 90 81 x = add x 32 82 end 83 if and ge y 65 le y 90 84 y = add y 32 85 end 86 if ne x y 87 ret false 88 end 89 end 90 ret true 91 end 92 fn ot_dn_isspace c uchar to bool 93 ret or or eq c 32 eq c 9 eq c 13 94 end 95 96 note ---- /etc/hosts: first IPv4 line naming host ---- 97 fn ot_dn_hosts host view uchar * a edit uchar 4 to bool 98 n = ot_dn_load ot_dn_t_hosts 99 if lt n 0 100 ret false 101 end 102 e = ulong n 103 p = ulong 0 104 while lt p e bound `9223372036854775807` 105 q = p 106 while lt q e bound `9223372036854775807` 107 if eq get ot_dn_buf q 10 108 break 109 end 110 q = add q 1 111 end 112 if ot_dn_hostline p q host a 113 ret true 114 end 115 p = add q 1 116 end 117 ret false 118 end 119 note the address token ot_dn_buf[i, j) as IPv4 into a 120 fn ot_dn_tokip i ulong j ulong a edit uchar 4 to bool 121 if gt sub j i 64 122 ret false 123 end 124 t = new uchar 64 0 125 for k ulong sub j i 126 put t k get ot_dn_buf add i k 127 end 128 ret ot_dn_ipv4 part t 0 sub j i a 129 end 130 note the end of the token starting at i (space, tab, CR or '#' ends it) 131 fn ot_dn_tokend i ulong q ulong to ulong 132 j = i 133 while lt j q bound `9223372036854775807` 134 c = get ot_dn_buf j 135 if or ot_dn_isspace c eq c 35 136 break 137 end 138 j = add j 1 139 end 140 ret j 141 end 142 fn ot_dn_hostline p ulong q ulong host view uchar * a edit uchar 4 to bool 143 note tokens of one line; the first is the address, the others names; '#' starts a comment 144 i = p 145 first = true 146 while lt i q bound `9223372036854775807` 147 while lt i q bound `9223372036854775807` 148 if not ot_dn_isspace get ot_dn_buf i 149 break 150 end 151 i = add i 1 152 end 153 if ge i q 154 break 155 end 156 if eq get ot_dn_buf i 35 157 break 158 end 159 j = ot_dn_tokend i q 160 if first 161 first = false 162 if not ot_dn_tokip i j a 163 ret false 164 end 165 else 166 if ot_dn_bufeq i sub j i host 167 ret true 168 end 169 end 170 i = j 171 end 172 ret false 173 end 174 175 note ---- the first IPv4 nameserver of /etc/resolv.conf ---- 176 fn ot_dn_nameserver a edit uchar 4 to bool 177 n = ot_dn_load ot_dn_t_resolv 178 if lt n 0 179 ret false 180 end 181 e = ulong n 182 p = ulong 0 183 note (the nameserver line: "nameserver ADDRESS") 184 while lt p e bound `9223372036854775807` 185 q = p 186 while lt q e bound `9223372036854775807` 187 if eq get ot_dn_buf q 10 188 break 189 end 190 q = add q 1 191 end 192 m = len ot_dn_t_ns 193 if gt sub q p m 194 if ot_dn_bufeq p m ot_dn_t_ns 195 i = add p m 196 while lt i q bound `9223372036854775807` 197 if not ot_dn_isspace get ot_dn_buf i 198 break 199 end 200 i = add i 1 201 end 202 j = ot_dn_tokend i q 203 if gt j i 204 if ot_dn_tokip i j a 205 ret true 206 end 207 end 208 end 209 end 210 p = add q 1 211 end 212 ret false 213 end 214 215 note ---- DNS query for the A record of host ---- 216 note encode QNAME into b from offset 12; returns the end offset or 0 when host is not a valid name 217 fn ot_dn_qname host view uchar * b edit uchar 512 to ulong 218 n = len host 219 if gt n 0 220 if eq get host sub n 1 46 221 n = sub n 1 222 end 223 end 224 if or eq n 0 gt n 253 225 ret 0 226 end 227 o = ulong 12 228 s = ulong 0 229 for i ulong add n 1 230 brk = true 231 if lt i n 232 brk = eq get host i 46 233 end 234 if brk 235 o = ot_dn_label host s sub i s b o 236 if eq o 0 237 ret 0 238 end 239 s = add i 1 240 end 241 end 242 put b o 0 243 ret add o 1 244 end 245 note one label host[s, s+l) at b[o]: length byte then the bytes; the next offset, or 0 when the label is invalid 246 fn ot_dn_label host view uchar * s ulong l ulong b edit uchar 512 o ulong to ulong 247 if or eq l 0 gt l 63 248 ret 0 249 end 250 put b o uchar l 251 for k ulong l 252 c = get host add s k 253 if or or le c 32 ge c 127 eq c 46 254 ret 0 255 end 256 put b add add o 1 k c 257 end 258 ret add add o 1 l 259 end 260 note skip a (possibly compressed) name in the answer at offset o; returns the offset after it, or 0 when malformed 261 fn ot_dn_skip r view uchar 1500 n ulong o ulong to ulong 262 p = o 263 note every pass consumes at least 2 bytes of a 1500-byte answer, so 1500 passes always suffice 264 while true bound 1500 265 if ge p n 266 ret 0 267 end 268 c = get r p 269 if eq c 0 270 ret add p 1 271 end 272 if eq and c 192 192 273 if ge add p 1 n 274 ret 0 275 end 276 ret add p 2 277 end 278 if ne and c 192 0 279 ret 0 280 end 281 p = add add p 1 ulong c 282 end 283 ret 0 284 end 285 fn ot_dn_query host view uchar * ns view uchar 4 a edit uchar 4 to long 286 q = new uchar 512 0 287 r = new uchar 1500 0 288 k = random q 0 2 0 289 if ne k 2 290 ret -3 291 end 292 put q 2 1 293 put q 5 1 294 e = ot_dn_qname host q 295 if eq e 0 296 ret -1 297 end 298 put q add e 1 1 299 put q add e 3 1 300 ql = add e 4 301 put ot_dn_v 0 ql 302 got = ot_dn_xfer ns q ql r 303 if lt got 12 304 ret -3 305 end 306 ret ot_dn_answer r ulong got q a 307 end 308 note send the query q[0, ql) to ns:53 over UDP; the answer with the same id in r: its length, or -3 309 fn ot_dn_xfer ns view uchar 4 q view uchar 512 ql ulong r edit uchar 1500 to long 310 sa = new uchar 16 0 311 put sa 0 2 312 put sa 3 53 313 for i ulong 4 314 put sa add 4 i get ns i 315 end 316 fd = socknew 2 526338 0 317 if lt fd 0 318 ret -3 319 end 320 c = connect int fd sa 0 16 321 w = long -1 322 if eq c 0 323 w = send int fd q 0 ql 0 324 end 325 if ne w long ql 326 z = fclose int fd 327 ret -3 328 end 329 got = ot_dn_wait int fd q r 330 z = fclose int fd 331 ret got 332 end 333 fn ot_dn_wait fd int q view uchar 512 r edit uchar 1500 to long 334 until = add monons OT_DN_NS 335 note junk datagrams are bounded by the 5 s deadline, not by a count 336 while true bound `9223372036854775807` 337 left = sub until monons 338 if le left 0 339 ret -3 340 end 341 z = waitio fd 1 left 342 if eq z -4 343 continue 344 end 345 if le z 0 346 ret -3 347 end 348 got = recv fd r 0 1500 0 349 if ge got 12 350 if and eq get r 0 get q 0 eq get r 1 get q 1 351 ret got 352 end 353 end 354 end 355 ret -3 356 end 357 fn ot_dn_u16 r view uchar 1500 p ulong to ulong 358 ret or shl ulong get r p 8 ulong get r add p 1 359 end 360 note the answer must repeat our question (bytes 12 .. ql of the query, names compared ASCII case-insensitively) 361 fn ot_dn_answer r view uchar 1500 n ulong q view uchar 512 a edit uchar 4 to long 362 ql = get ot_dn_v 0 363 note header: QR set, opcode 0, RCODE 0, one question 364 if or ne and get r 2 248 128 ne and get r 3 15 0 365 ret -4 366 end 367 if or ne get r 4 0 ne get r 5 1 368 ret -4 369 end 370 if gt ql n 371 ret -4 372 end 373 if not ot_dn_qsame r q ql 374 ret -4 375 end 376 ret ot_dn_rrs r n ql a 377 end 378 fn ot_dn_qsame r view uchar 1500 q view uchar 512 ql ulong to bool 379 for i ulong sub ql 12 380 x = get r add 12 i 381 y = get q add 12 i 382 if and ge x 65 le x 90 383 x = add x 32 384 end 385 if and ge y 65 le y 90 386 y = add y 32 387 end 388 if ne x y 389 ret false 390 end 391 end 392 ret true 393 end 394 note the answer records after the question at ql: the first IN A record with 4 bytes of data 395 fn ot_dn_rrs r view uchar 1500 n ulong ql ulong a edit uchar 4 to long 396 an = ot_dn_u16 r 6 397 p = ql 398 for i ulong an 399 p = ot_dn_skip r n p 400 if or eq p 0 gt add p 10 n 401 ret -4 402 end 403 ty = ot_dn_u16 r p 404 cl = ot_dn_u16 r add p 2 405 rl = ot_dn_u16 r add p 8 406 p = add p 10 407 if gt add p rl n 408 ret -4 409 end 410 if and and eq ty 1 eq cl 1 eq rl 4 411 for j ulong 4 412 put a j get r add p j 413 end 414 ret 0 415 end 416 p = add p rl 417 end 418 ret -4 419 end 420 421 fn ot_dn_resolve host view uchar * a edit uchar 4 to long 422 if ot_dn_ipv4 host a 423 ret 0 424 end 425 if ot_dn_hosts host a 426 ret 0 427 end 428 ns = new uchar 4 0 429 if not ot_dn_nameserver ns 430 ret -2 431 end 432 ret ot_dn_query host ns a 433 end 434 435 note ---- TCP connect to a.b.c.d:port, nonblocking; fd or a negative errno ---- 436 fn ot_io_connect a view uchar 4 port ulong until long to long 437 sa = new uchar 16 0 438 pn = new uchar 32 0 439 put sa 0 2 440 put sa 2 uchar shr port 8 441 put sa 3 uchar and port 255 442 for i ulong 4 443 put sa add 4 i get a i 444 end 445 fd = socknew 2 526337 0 446 if lt fd 0 447 ret fd 448 end 449 r = connect int fd sa 0 16 450 if eq r -115 451 left = sub until monons 452 z = long 0 453 if gt left 0 454 z = waitio int fd 4 left 455 end 456 if le z 0 457 c = fclose int fd 458 ret -110 459 end 460 note writable is also how a refused connection reports itself: only a peer address proves it connected 461 if lt peername int fd pn 0 32 0 462 c = fclose int fd 463 ret -111 464 end 465 r = 0 466 end 467 if ne r 0 468 c = fclose int fd 469 ret r 470 end 471 one = new uchar 4 0 472 put one 0 1 473 s = sockopt int fd 6 1 one 0 4 474 ret fd 475 end