mxf-remote.h (6912B)
1 #ifndef MXF_REMOTE_H 2 #define MXF_REMOTE_H 3 4 #include "ga_foreach.h" 5 6 #include "typeallocdefs.h" 7 #include "typealloc.h" 8 // I fold, I will use TypeAlloc 9 10 #include <noreturn.h> 11 #include <control.h> 12 // Needs control.o from NMMail 13 14 #include <netstrings.h> 15 // Needs netstrings.o from NMMail 16 17 #include <str.h> 18 // Needs str from NMMail 19 20 #include <auto_qmail.h> 21 // Needs auto-qmail from NMMail 22 23 #include <constmap.h> 24 // Needs constmap from NMMail 25 26 #include <ipme.h> 27 // Needs IPMe from NMMail 28 29 #include <scan.h> 30 // Needs scan from NMMail 31 32 #include <fmt.h> 33 // Needs fmt from NMMail 34 35 #include <uthash.h> 36 #undef uthash_fatal 37 #define uthash_fatal(msg) temp_nomem() 38 // Special - needs UTHash for mutable hash tables. 39 #ifndef USING_SKALIBS 40 #error "You must use skalibs, s6-networking and s6-dns for Nightmare Remote." 41 #else 42 #include <stdlib.h> 43 #include <unistd.h> 44 #include <signal.h> 45 #include <poll.h> 46 #include <limits.h> 47 #include <skalibs/tai.h> 48 #include <sys/types.h> 49 #include <skalibs/djbunix.h> 50 #include <skalibs/types.h> 51 #include <skalibs/iopause.h> 52 #include <skalibs/stralloc.h> 53 #include <skalibs/sig.h> 54 #include <skalibs/selfpipe.h> 55 //#include <skalibs/strerr2.h> 56 #include <skalibs/genalloc.h> 57 #include <skalibs/socket.h> 58 #include <skalibs/ip46.h> 59 #include <s6-dns/s6dns.h> 60 #include <s6-dns/skadns.h> 61 #endif 62 63 #ifndef UCSPITLSC 64 #define UCSPITLSC "/package/net/s6-networking/command/s6-ucspitlsc" 65 #endif 66 67 #ifndef TLSC 68 #define TLSC "/package/net/s6-networking/command/s6-tlsc" 69 #endif 70 71 /* we are tcpclient, so we don't need this 72 #ifndef TCPCLIENT 73 #define TCPCLIENT "/package/net/s6-networking/command/s6-tcpclient" 74 #endif 75 */ 76 77 // expected to be in PATH and to be ucspi client tools 78 /* already configured elsewhere 79 #ifndef QMTP_CLIENT 80 #define QMTP_CLIENT "mxf-remote-qmtpc" 81 #endif 82 83 #ifndef SMTP_CLIENT 84 #define SMTP_CLIENT "mxf-remote-smtpc" 85 #endif 86 */ 87 #define DIE_RESOURCES 111 88 #define DIE_BADPROTO 100 89 90 // Only use these on verified MXPS distances! 91 // xmxps_prio will never be used, it is just an example 92 //#define _xmxps_prio(distance) (((distance - MXPS_MAGIC_NO) & 0xfff0) >> 4) 93 #define _mxps_prio(distance) ((distance & 0x00f0) >> 4) 94 #define _mxps_slice(distance) (distance & 0x000f) 95 #define _is_mxps(distance) ((distance + 1 > MXPS_MAGIC_NO) && (distance - 1 < MXPS_END)) 96 97 #define MXPS_MAGIC_NO 0x3200 98 #define MXPS_END 0x32ff 99 /* 100 #define SRV_TCP "tcp" 101 #define SRV_QMTP "qmtp" 102 #define SRV_QMTPS "qmtps" 103 #define SRV_SMTP "smtp" 104 #define SRV_SMTPS "smtps" // officially deregistered by IANA; continued here 105 #define SLICE_LAST 0x3f 106 #define SLICE_SRV_SMTP 0x010 // all +16 from the kosher slices 107 #define SLICE_SRV_QMTP 0x011 108 #define SLICE_SRV_SMTPS 0x012 109 #define SLICE_SRV_QMTPS 0x013 110 #define SLICE_SMTP 0x00 111 #define SLICE_QMTP 0x01 112 #define SLICE_SMTPS 0x02 // we propose the use of 2 and 3 for -s versions of protocols. 113 #define SLICE_QMTPS 0x03 114 */ 115 #define NOMXPS_SLICE 0x0 116 #define MAXSLICES 16 // there will never be more than 16 slices. advancement in internet mail is likely to use SRV records 117 118 // defaults, in case not a SRV record 119 /* 120 #define SMTP_PORT 25 121 #define SMTPS_PORT 465 // Should be all but extinct; most SMTPS servers support STARTTLS 122 #define QMTP_PORT 209 123 #define QMTPS_PORT 6209 // Not extinct at all; QMTP has no STARTTLS support and will never grow it. 124 */ 125 #define BREAK -1 // call this from an event callback to break the loop 126 #define FALSE 0 127 #define TRUE 1 128 #define MAXSRVLEN 64 129 130 /* stages of the general automaton 131 */ 132 //#define 133 134 struct descriptor_t_s; 135 typedef struct descriptor_t_s descriptor_t; 136 137 struct protocol_t_s; 138 typedef struct protocol_t_s protocol_t; 139 140 struct process_t_s; 141 typedef struct process_t_s process_t; 142 143 struct dnsq_t_s; 144 typedef struct dnsq_t_s dnsq_t; 145 146 typedef struct { 147 ip46full ip; // ip46full from skalibs 148 } ipmx_t; 149 150 TypeAlloc_typedef(ipmxAlloc,ipmx_t,ipmx,len,a) 151 152 #define MXRESULT_SRV_ZERO {0, 0, 0, FALSE, FALSE, FALSE, FALSE, STRALLOC_ZERO, TA_ZERO} 153 #define MXRESULT_MX_ZERO {0, 0, 0, FALSE, FALSE, TRUE, FALSE, STRALLOC_ZERO, TA_ZERO} 154 #define MXRESULT_SLIP_ZERO {0, 0, 0, TRUE, TRUE, FALSE, FALSE, STRALLOC_ZERO, TA_ZERO} 155 156 typedef struct { 157 uint16_t port; 158 uint16_t prio; // 0 for A/AAAA and SLIP; as published for MX and SRV 159 uint16_t weight; // 0 for MX, A/AAAA, and SLIP; as published for SRV 160 unsigned int lookupdone:1; // dns - 1 if SLIP - means ip lookups were done 161 unsigned int hasresults:2; // dns - >1 if SLIP - means has IPs 162 unsigned int ismx:1; // dns - 0 (SRV or SLIP) is better 163 unsigned int isme:1; // if, by some tragedy, after prefsorting, this comes to be the first item in a protocol, balk. 164 stralloc name; // may be a hostname or a SLIP; will not contain ports or priorities. 165 ipmxAlloc ipmx; // this'll contain the IPs of this !!single hostname!! 166 } mxresult_t; // stores processed results; SRV hostnames and ports, MX hostnames and ports, IPs (A/AAAA legacy, string literal IPs) 167 168 //TypeAlloc_typedef(dnslAlloc,dnsl_t,dnsl,len,a) 169 TypeAlloc_typedef(mxrAlloc,mxresult_t,mxr,len,a) 170 TypeAlloc_typedef(protoAlloc,protocol_t,pt,len,a) 171 TypeAlloc_typedef(saAlloc,stralloc,sa,len,a) 172 TypeAlloc_typedef(pfdAlloc,struct pollfd,pfd,len,a) 173 174 #include <typeallocs.h> 175 176 struct descriptor_t_s { 177 struct pollfd fd; // stored directly in this struct; used solely as an index for the uthash 178 void* opaque; // a WEAK pointer to an opaque; may be anything or NULL 179 int (*fdcb) (descriptor_t *, short, void *); 180 UT_hash_handle hh; 181 }; 182 183 struct process_t_s { 184 pid_t pid; 185 void* opaque; // a WEAK pointer to an opaque 186 int (*pdiecb) (process_t *, int, void *); 187 UT_hash_handle hh; 188 }; // callback when a protegé reports ("child dies") 189 190 struct protocol_t_s { 191 char srvservice[MAXSRVLEN]; // by happy coincidence we can also use this as the prefix for control/<service>routes filenames 192 uint16_t defport; // default port 193 signed char slice; // plain mx = 0 194 unsigned int tls:1; // TLSC 195 unsigned int starttls:1; // UCSPITLSC 196 unsigned int lookupdone:1; // dns - means lookups were done 197 unsigned int hasresults:1; // dns - means has names 198 stralloc app; 199 struct constmap maproutes; // control/<service>routes 200 mxrAlloc mxresults; // results of MX DNS lookups 201 }; 202 203 struct dnsq_t_s { 204 uint16_t dnsq; 205 void* opaque; // a WEAK pointer to an opaque 206 void* opaque2; // a WEAK pointer to an opaque 207 int (*qcb) (skadns_t *, dnsq_t *, void *, void *); 208 UT_hash_handle hh; 209 }; /* this is our ephemeral dns query structure 210 * opaque should probably be a dnsl_t 211 */ 212 /* 213 struct dnsl_t_s { 214 protocol_t *proto; // ptr to the protocol this dnsl relates to 215 char success; // 0 if not, 1 if so 216 mxrAlloc mxr; // mxr->len ptrs to mxresult_t structures 217 }; */ 218 219 /* typedef struct { 220 uint32_t prio; // lower is to try sooner 221 genalloc mxresults; // &(mxresults.s) is of type mxresult_t 222 } mxprio_t; */ 223 224 //extern protocol_t protocols; // 16 slices, plus the 4 SRV pseudo slices plus unknown more 225 //extern buffer errbuf, errbufsmall; 226 extern char const *PROG; 227 228 #endif