1 /* hiproto.h  -  Protocol constants for hiios
2  * Copyright (c) 2006,2012 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.
3  * This is confidential unpublished proprietary source code of the author.
4  * NO WARRANTY, not even implied warranties. Contains trade secrets.
5  * Distribution prohibited unless authorized in writing. See file COPYING.
6  * Special grant: hiios.c may be used with zxid open source project under
7  * same licensing terms as zxid itself.
8  * $Id$
9  *
10  * 15.4.2006, created over Easter holiday --Sampo
11  * 16.8.2012, modified license grant to allow use with ZXID.org --Sampo
12  * 17.8.2012, added STOMP 1.1 definitions  --Sampo
13  */
14 
15 #ifndef _hiproto_h
16 #define _hiproto_h
17 
18 struct hi_thr;
19 struct hi_io;
20 struct hi_pdu;
21 struct hiios;
22 
23 #include <pthread.h>
24 
25 #define HIPROTO_POLL_OFF 0  /* no protocol specified, or in poll_tok inhibit poll */
26 #define HIPROTO_POLL_ON  1  /* a special value for poll_tok to trigger poll */
27 #define HIPROTO_SIS  2
28 #define HIPROTO_DTS  3
29 #define HIPROTO_SMTP 4
30 #define HIPROTO_HTTP 5
31 #define HIPROTO_TEST_PING 6
32 #define HIPROTO_STOMP 7
33 #define HIPROTO_STOMPS 8
34 
35 #ifdef ENA_S5066
36 /* Application SAP IDs. See Annex F. */
37 
38 #define SAP_ID_SUBNET_MGMT 0
39 #define SAP_ID_ACKD_MSG    1
40 #define SAP_ID_UNACKD_MSG  2
41 #define SAP_ID_HMTP        3
42 #define SAP_ID_HFPOP       4
43 #define SAP_ID_OP_OW       5
44 #define SAP_ID_STREAM      6
45 #define SAP_ID_DATAGRAM    7
46 #define SAP_ID_PPP         8
47 #define SAP_ID_IP          9
48 /* 10,11 reserved for future assignment */
49 /* 12,13,14,15 local uses, ad-hoc */
50 #define SAP_ID_WALKIE_TALKIE 12   /* Half Duplex Digital Voice and chat */
51 #define SAP_ID_RFID          13   /* RFID tracking application */
52 
53 /* Clients talking SIS are required to split long application data to several PDUs */
54 #define SIS_MIN_PDU_SIZE 5     /* preamble, version and length field */
55 #define SIS_UNIHDR_SIZE 12
56 #define SIS_MTU 2048           /* Reliable service MTU for u_pdu, see p. A-7 */
57 #define SIS_BCAST_MTU 4096     /* Maximum size of broadcast MTU for u_pdu, see p. A-7 */
58 #define SIS_MAX_PDU_SIZE (SIS_MIN_PDU_SIZE + SIS_UNIHDR_SIZE + SIS_BCAST_MTU)
59 
60 #define DTS_SEG_SIZE 800  /* arbitrarily tunable below 1k (10 bits, see C.3.2.10, p. C-14) */
61 
62 /* N.B. In practise segment size is limited by 8 bit EOT (End Of Transmission) field
63  * that has range of 127.5 seconds. Given slow data rate, a PDU can take long time
64  * to transmit. For example: 127 seconds is 1190 bytes @ 75bps or 38KB @ 2400bps.
65  * See C.3.2.3, p. C-9 for discussion. */
66 
67 /* D_PDU type constants */
68 
69 #define DTS_DATA_ONLY  0
70 #define DTS_ACK_ONLY   1
71 #define DTS_DATA_ACK   2
72 #define DTS_RESET      3
73 #define DTS_EDATA_ONLY 4
74 #define DTS_EACK_ONLY  5
75 #define DTS_MGMT       6
76 #define DTS_NONARQ     7
77 #define DTS_ENONARQ    8
78 /* 9-14 reserved */
79 #define DTS_WARNING   15
80 
81 #define SIS_UNIDATA_IND_MIN_HDR 22  /* min == no error and no non-rx'd blocks */
82 
83 /* S_PDU type constants (these are distinct from primitives) */
84 
85 #define S_PDU_DATA               0
86 #define S_PDU_OK                 1
87 #define S_PDU_FAIL               2
88 #define S_PDU_HARD_LINK_REQ      3
89 #define S_PDU_HARD_LINK_OK       4
90 #define S_PDU_HARD_LINK_REJ      5
91 #define S_PDU_HARD_LINK_TERM     6
92 #define S_PDU_HARD_LINK_TERM_OK  7
93 
94 /* C_PDU type constants (already shifted for use in C_PCI */
95 
96 #define C_PDU_DATA           0x00
97 #define C_PDU_LINK_REQ       0x10
98 #define C_PDU_LINK_OK        0x20
99 #define C_PDU_LINK_REJ       0x30
100 #define C_PDU_LINK_BREAK     0x40
101 #define C_PDU_LINK_BREAK_OK  0x50
102 
103 int sis_decode(struct hi_thr* hit, struct hi_io* io);
104 int dts_decode(struct hi_thr* hit, struct hi_io* io);
105 void dts_send_uni(struct hi_thr* hit, struct hi_io* io, struct hi_pdu* req, int len, char* d);
106 void sis_send_bind(struct hi_thr* hit, struct hi_io* io, int sap, int rank, int svc_type);
107 struct hi_pdu* sis_encode_start(struct hi_thr* hit, int op, int len);
108 
109 struct sis_sap {
110   struct hi_io* io;
111   char rank;
112   char tx_mode;
113   char flags;
114   char n_re_tx;
115 };
116 
117 #define SIS_MAX_SAP_ID 16
118 extern struct sis_sap saptab[SIS_MAX_SAP_ID];
119 extern pthread_mutex_t saptab_mut;
120 
121 struct dts_conn {
122   char remote_station_addr[4];
123   int c_pdu_id;
124   int rx_lwe;
125   int rx_uwe;
126   char acks[32];  /* Bitmap of acks, kept on receiving end */
127   /* *** Do we need "memory ACK" array for misreceived PDUs? */
128   struct hi_pdu* nonarq_pdus[4096];  /* The c_pdu_id is 12 bits */
129 
130   int tx_lwe;
131   int tx_uwe;
132   struct hi_pdu* tx_pdus[256];  /* Hold PDUs so we can re_tx them if they are not ack'd */
133 };
134 #endif
135 
136 struct u_pdu {
137   short len;
138   char* data;
139 };
140 
141 struct c_pdu_buf {
142   int size;
143 #ifdef ENA_S5066
144   char map[SIS_MAX_PDU_SIZE/8];  /* bitmap of bytes received so we know if we have received all */
145   char m[SIS_MAX_PDU_SIZE];
146 #endif
147 };
148 
149 void test_ping(struct hi_thr* hit, struct hi_io* io);
150 int http_decode(struct hi_thr* hit, struct hi_io* io);
151 
152 /* SMTP support */
153 
154 #define SMTP_MIN_PDU_SIZE 5     /* preamble and length field */
155 #define SMTP_MAX_PDU_SIZE 4096
156 
157 /* States of SMTP parsing for client */
158 #define SMTP_INIT 0  /* wait for 220 greet\n (server: send 220 greet) */
159 #define SMTP_EHLO 1  /* 220 greet seen, issue EHLO<sp>s\n */
160 #define SMTP_RDY  2  /* wait for 250 from EHLO */
161 #define SMTP_MAIL 3  /*  send piped SMTP payload, MAIL FROM:<a>\n */
162 #define SMTP_RCPT 4  /*  RCPT TO:<a>\n or DATA\n */
163 #define SMTP_DATA 5  /*  Data entry stage (354) */
164 #define SMTP_SEND 6  /* wait for 354, send the message and \r\n.\r\n */
165 #define SMTP_SENT 7  /* Mail sent (250 after 354) */
166 #define SMTP_QUIT 8  /* wait for 221 goodbye */
167 
168 /* States of SMTP parsing for server */
169 #define SMTP_START  11  /* expect EHLO, send 250 */
170 #define SMTP_MAIN   12  /* expect MAIL FROM, process RCPT TOs, DATA, and \r\n.\r\n */
171 #define SMTP_TO     13
172 #define SMTP_MORE0  14  /* Long mail. Keep sending body as SIS primitives. Noting seen. */
173 #define SMTP_MORE1  15  /* Assume "\r\n" has been seen and ".\r\n" needs to be seen. */
174 #define SMTP_MORE2  16  /* Assume "\r\n." has been seen and "\r\n" needs to be seen. */
175 #define SMTP_WAIT   17  /* expect 354 */
176 #define SMTP_STATUS 18  /* expect staus of message: 250 = sent, others error */
177 #define SMTP_END    19  /* expect QUIT and send 221 bye. If get MAIL FROM move to SMTP_MAIN. */
178 
179 #define SMTP_GREET_DOMAIN "zxid.org"  /* *** config domain */
180 #define SMTP_EHLO_CLI "Beautiful"
181 
182 int smtp_decode_req(struct hi_thr* hit, struct hi_io* io);
183 int smtp_decode_resp(struct hi_thr* hit, struct hi_io* io);
184 
185 /* STOMP 1.1 Support */
186 
187 /* States of STOMP parsing for client */
188 #define STOMP_INIT 0  /* Issue STOMP */
189 #define STOMP_CONN 1  /* wait for CONNECTED  (server: send CONNECTED) */
190 #define STOMP_RDY  2  /*  wait for any message */
191 #define STOMP_SEND 3  /*  SEND message */
192 #define STOMP_RCPT 4  /*  wait for RECEIPT */
193 #define STOMP_ACK  5  /*  ACK */
194 #define STOMP_SUB  6  /*  SUBSCRIBE */
195 #define STOMP_UNSB 7  /*  UNSUBSCRIBE */
196 #define STOMP_DISC 8  /* DISCONNECT */
197 #define STOMP_QUIT 9  /* wait for RECEIPT in response to DISCONNECT */
198 
199 /* States of STOMP parsing for server */
200 #define STOMP_START  20  /* expect STOMP (or CONNECT), send CONNECTED */
201 #define STOMP_MAIN   21  /* expect any command, e.g. SEND or SUBSCRIBE */
202 #define STOMP_MSG    22  /* Wait for ACK from MESSAGE */
203 #define STOMP_END    23  /* RECEPIT in response to DISCONNECT sent, linger before close */
204 
205 int stomp_parse_pdu(struct hi_pdu* pdu);
206 void stomp_parse_header(struct hi_pdu* req, char* hdr, char* val);
207 int stomp_decode(struct hi_thr* hit, struct hi_io* io);
208 void stomp_msg_deliver(struct hi_thr* hit, struct hi_pdu* db_pdu);
209 int stomp_err(struct hi_thr* hit, struct hi_io* io, struct hi_pdu* req, const char* ecode, const char* emsg);
210 void stomp_send_receipt(struct hi_thr* hit, struct hi_io* io, struct hi_pdu* req);
211 
212 struct hi_ch* zxbus_find_ch(struct hiios* shf, int len, const char* dest);
213 struct hi_ent* zxbus_load_ent(struct hiios* shf, int len, const char* eid);
214 int zxbus_login_ent(struct hi_thr* hit, struct hi_io* io, struct hi_pdu* req);
215 int zxbus_retire(struct hi_thr* hit, struct hi_pdu* db_pdu);
216 int zxbus_persist(struct hi_thr* hit, struct hi_io* io, struct hi_pdu* req);
217 int zxbus_subscribe(struct hi_thr* hit, struct hi_io* io, struct hi_pdu* req);
218 int zxbus_load_subs(struct hiios* shf);
219 void zxbus_sched_pending_delivery(struct hi_thr* hit, const char* dest);
220 
221 #endif /* _hiproto_h */
222