code wiki / _hdl_build / nx_email_mta_daemon.nx

nx_email_mta_daemon.nx source

↩ module page · 47 lines · 2049 B

1// nx_email_mta_daemon.nx -- runnable receiving MTA: bind 127.0.0.1:2525 and 2// accept SMTP connections forever, delivering to the mailbox store. This is 3// the continuously-running server (vs the one-shot serve in the gate). 4// 5// Monotonic segid counter across connections (each connection's deliveries 6// use a fresh segid range) so segments never collide. Cross-restart segid 7// persistence is a follow-on. Loopback bind only -- public bind is a 8// separate operator opt-in (see EMAIL-D4). 9// license_tier: ORIGINAL 10import "nx_email_mta_io.nx" 11import "nx_syscalls.nx" 12 13const MD_PORT: i64 = 0x09DD // 2525 14const MD_PREFIX: *u8 = "knowledge/status/jbox/" 15const MD_DOMAIN: *u8 = "jasonewest.com" 16 17func md_addr(out: *u8, port: i64) -> i64 { 18 out[0] = 2 as u8; out[1] = 0 as u8 19 out[2] = ((port >> 8) & 0xff) as u8; out[3] = (port & 0xff) as u8 20 out[4] = 127 as u8; out[5] = 0 as u8; out[6] = 0 as u8; out[7] = 1 as u8 21 var i: i64 = 8 22 while i < 16 { out[i] = 0 as u8; i = i + 1 } 23 return 0 24} 25func md_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 26 27func main() -> i64 { 28 sys_mkdir("knowledge/status/jbox" as *u8, 511) 29 let addr: *u8 = sys_mmap(16) 30 md_addr(addr, MD_PORT) 31 let lfd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0) 32 if lfd < 0 { md_puts("socket fail\n" as *u8); sys_exit(1); return 1 } 33 if sys_bind(lfd, addr, 16) < 0 { md_puts("bind fail (port in use?)\n" as *u8); sys_exit(1); return 1 } 34 if sys_listen(lfd, 16) < 0 { md_puts("listen fail\n" as *u8); sys_exit(1); return 1 } 35 md_puts("nishi MTA listening on 127.0.0.1:2525 (domain: jasonewest.com)\n" as *u8) 36 var ctr: i64 = 1 37 var running: i64 = 1 38 while running == 1 { 39 let scfd: i64 = sys_accept(lfd) 40 if scfd < 0 { running = 1 } else { 41 let d: i64 = nx_mta_serve_conn(scfd, MD_PREFIX, MD_DOMAIN, 14, ctr) 42 sys_close(scfd) 43 if d > 0 { ctr = ctr + d } else { ctr = ctr + 1 } 44 } 45 } 46 return 0 47}