1 /**********************************************************************
2  * packet.h                                                    May 2003
3  * Horms                                             horms@verge.net.au
4  *
5  * Packets for map queries
6  *
7  * perdition
8  * Mail retrieval proxy server
9  * Copyright (C) 1999-2005  Horms
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of the
14  * License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  **********************************************************************/
26 
27 #ifndef PERDITION_PACKET_H
28 #define PERDITION_PACKET_H
29 
30 #include "perdition_globals.h"
31 
32 #include <sys/types.h>
33 #include <inttypes.h>
34 #include <string.h>
35 
36 #define PERDITION_SOCKET
37 
38 #define PERDITION_PACKET_MAGIC   0x07070707
39 #define PERDITION_PACKET_VERSION 0x00000001
40 
41 #define PERDITION_PACKET_REQ     0x0001
42 #define PERDITION_PACKET_RSP     0x0002
43 #define PERDITION_PACKET_STR_REQ 0x0003
44 
45 #define PERDITION_PACKET_CS_SHA1 0x00000004
46 #define PERDITION_PACKET_CS_MD5  0x00000002
47 #define PERDITION_PACKET_CS_ROLL 0x00000001
48 #define PERDITION_PACKET_CS_NONE 0x00000000
49 
50 typedef struct {
51 	uint32_t magic;
52 	uint16_t version;
53 	uint16_t flags;
54 	uint16_t length;
55 	uint16_t cs_type;
56 	uint32_t saddr;
57 	uint32_t daddr;
58 	uint16_t sport;
59 	uint16_t dport;
60 } perdition_packet_head_t;
61 
62 typedef struct {
63 	uint32_t time;
64 	char   cs[20]; /* 192 bit checksum */
65 } perdition_packet_tail_sha1_t;
66 
67 typedef struct {
68 	uint32_t time;
69 	char   cs[16]; /* 128 bit checksum */
70 } perdition_packet_tail_md5_t;
71 
72 typedef struct {
73 	uint32_t cs; /* 32 bit checksum */
74 } perdition_packet_tail_roll_t;
75 
76 typedef void perdition_packet_tail_none_t;
77 
78 typedef union {
79 	perdition_packet_tail_sha1_t sha1;
80 	perdition_packet_tail_md5_t md5;
81 	perdition_packet_tail_roll_t roll;
82 } perdition_packet_tail_t;
83 
84 #define PERDITION_PACKET_MAX_PACKET_LEN 65000 /* 64kbyes, less a bit
85 						 * for overhead */
86 #define PERDITION_PACKET_MAX_BODY_LEN \
87 	( PERDITION_PACKET_MAX_PACKET_LEN - sizeof(perdition_packet_tail_t) )
88 
89 typedef struct {
90 	perdition_packet_head_t head;
91 	char body[PERDITION_PACKET_MAX_BODY_LEN];
92 	perdition_packet_tail_t tail;
93 } perdition_packet_t;
94 
95 typedef struct {
96 	uint16_t length;
97 	const unsigned char *data;
98 } perdition_packet_str_t;
99 
perdition_packet_str_pack(perdition_packet_str_t * p_str,char * buf)100 static inline void perdition_packet_str_pack(perdition_packet_str_t *p_str,
101 					     char *buf)
102 {
103 	p_str->data = (unsigned char *) buf;
104 	p_str->length = buf ? strlen(buf) : 0;
105 }
106 
107 #define PERDITION_PACKET_STR_PACK(p_str, str) \
108 	perdition_packet_str_pack(&p_str, str);
109 
110 perdition_packet_t *
111 perdition_packet_create(void);
112 
113 void
114 perdition_packet_destroy(perdition_packet_t *packet);
115 
116 int
117 perdition_packet_init_v1_req(perdition_packet_t **packet,
118 		uint16_t cs_type,
119 		uint32_t saddr, uint16_t sport,
120 		uint32_t daddr, uint16_t dport,
121 		perdition_packet_str_t *key,
122 		perdition_packet_str_t *domain_delimiter);
123 
124 int
125 perdition_packet_init_v1_str_req(perdition_packet_t **packet,
126 				 uint16_t cs_type,
127 				 perdition_packet_str_t *saddr,
128 				 perdition_packet_str_t *sport,
129 				 perdition_packet_str_t *daddr,
130 				 perdition_packet_str_t *dport,
131 				 perdition_packet_str_t *key,
132 				 perdition_packet_str_t *domain_delimiter);
133 
134 int
135 perdition_packet_init_v1_rsp(perdition_packet_t **packet,
136 		uint16_t cs_type,
137 		perdition_packet_str_t *user,
138 		perdition_packet_str_t *server,
139 		perdition_packet_str_t *port);
140 
141 int
142 perdition_packet_verify_v1_req(perdition_packet_t *packet,
143 		size_t len, perdition_packet_str_t *key,
144 		perdition_packet_str_t *domain_delimiter);
145 
146 int
147 perdition_packet_verify_v1_str_req(perdition_packet_t *packet, size_t len,
148 				   perdition_packet_str_t *saddr,
149 				   perdition_packet_str_t *sport,
150 				   perdition_packet_str_t *daddr,
151 				   perdition_packet_str_t *dport,
152 				   perdition_packet_str_t *key,
153 				   perdition_packet_str_t *domain_delimiter);
154 
155 int
156 perdition_packet_verify_v1_rsp(perdition_packet_t *packet,
157 		size_t len, perdition_packet_str_t *user,
158 		perdition_packet_str_t *server,
159 		perdition_packet_str_t *port);
160 
161 #endif /* PERDITION_PACKET_H */
162