Release: 1.0.3 -> 1.0.4cvs
[udpgate.git] / src / packet.h
1 /* $Id$
2  * Include file for the packet assembly and disassembly layer
3  * Copyright (C) 2004 Jan Kratochvil <project-udpgate@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #ifndef _UDPGATE_PACKET_H
21 #define _UDPGATE_PACKET_H 1
22
23
24 #include <glib/gtypes.h>
25 #include <glib/ghash.h>
26
27
28 G_BEGIN_DECLS
29
30 #define HOSTIP_GUINT32_TO_STRING(hostip_guint32) \
31                 (udpgate_printf_alloca("%d.%d.%d.%d", \
32                                 ((hostip_guint32)>>24U)&0xFFU, \
33                                 ((hostip_guint32)>>16U)&0xFFU, \
34                                 ((hostip_guint32)>> 8U)&0xFFU, \
35                                 ((hostip_guint32)>> 0U)&0xFFU))
36 /* Do not use nested udpgate_printf_alloca() - macros cannot nest! */
37 #define SOCKADDR_IN_TO_STRING(sockaddr_in_pointer) ({ \
38                 const gchar *_hostip_string=HOSTIP_GUINT32_TO_STRING(ntohl((sockaddr_in_pointer)->sin_addr.s_addr)); \
39                 udpgate_printf_alloca("%s:%d",_hostip_string,ntohs((sockaddr_in_pointer)->sin_port)); \
40                 })
41
42 #define PACKET_HEADER "UDPGate0"
43
44 #define PACKET_ELEM_ATTR_MANDATORY (0x80000000)
45 #define PACKET_ELEM_ATTR_OPTIONAL  (0x00000000)
46 enum packet_elem_type {
47         PACKET_ELEM_TYPE_PROGRAM_VERSION=(0x01|PACKET_ELEM_ATTR_MANDATORY),     /* 'VERSION' string */
48         PACKET_ELEM_TYPE_CLIENT_INADDR  =(0x02|PACKET_ELEM_ATTR_MANDATORY),     /* host order */
49         PACKET_ELEM_TYPE_CLIENT_PORT    =(0x03|PACKET_ELEM_ATTR_MANDATORY),     /* host order */
50         PACKET_ELEM_TYPE_DATA_GUINT32   =(0x04|PACKET_ELEM_ATTR_MANDATORY),     /* user data (e.g. unique id) */
51         };
52
53 gpointer packet_assembly(size_t *packet_length_pointer,GHashTable *values);
54 gboolean packet_recognized(gconstpointer packet,size_t packet_length);
55 GHashTable *packet_disassembly(gconstpointer packet,size_t packet_length);
56 void packet_disassembly_destroy(GHashTable *hash);
57
58 G_END_DECLS
59
60
61 #endif /* _UDPGATE_PACKET_H */