1 /*
2 * Project: udptunnel
3 * File: message.h
4 *
5 * Copyright (C) 2009 Daniel Meekins
6 * Contact: dmeekins - gmail
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #ifndef MESSAGE_H
23 #define MESSAGE_H
24
25 #ifndef WIN32
26 #include <inttypes.h>
27 #include <arpa/inet.h>
28 #endif /*WIN32*/
29
30 #include "common.h"
31 #include "socket.h"
32
33 #define MSG_MAX_LEN 1024 /* max bytes to send in body of message (16 bits) */
34 #define KEEP_ALIVE_SECS 60
35 #define KEEP_ALIVE_TIMEOUT_SECS (7*60+1) /* has 7 tries to send a keep alive */
36
37 /* Message types: max 8 bits */
38 #define MSG_TYPE_GOODBYE 0x01
39 #define MSG_TYPE_HELLO 0x02
40 #define MSG_TYPE_HELLOACK 0x03
41 #define MSG_TYPE_KEEPALIVE 0x04
42 #define MSG_TYPE_DATA0 0x05
43 #define MSG_TYPE_DATA1 0x06
44 #define MSG_TYPE_ACK0 0x07
45 #define MSG_TYPE_ACK1 0x08
46
47 #ifndef WIN32
48 struct msg_hdr
49 {
50 uint16_t client_id;
51 uint8_t type;
52 uint16_t length;
53 } __attribute__ ((__packed__));
54 #else
55 #pragma pack(push, 1)
56 struct msg_hdr
57 {
58 uint16_t client_id;
59 uint8_t type;
60 uint16_t length;
61 };
62 #pragma pack(pop)
63 #endif /*WIN32*/
64
65 typedef struct msg_hdr msg_hdr_t;
66
67 int msg_send_msg(socket_t *to, uint16_t client_id, uint8_t type,
68 char *data, int data_len);
69 int msg_send_hello(socket_t *to, char *host, char *port, uint16_t req_id);
70 int msg_recv_msg(socket_t *sock, socket_t *from,
71 char *data, int data_len,
72 uint16_t *client_id, uint8_t *type, uint16_t *length);
73
74 /* Inline functions for working with the message header struct */
msg_init_header(msg_hdr_t * hdr,uint16_t client_id,uint8_t type,uint16_t len)75 static _inline_ void msg_init_header(msg_hdr_t *hdr, uint16_t client_id,
76 uint8_t type, uint16_t len)
77 {
78 hdr->client_id = htons(client_id);
79 hdr->type = type;
80 hdr->length = htons(len);
81 }
82
msg_get_client_id(msg_hdr_t * h)83 static _inline_ uint16_t msg_get_client_id(msg_hdr_t *h)
84 {
85 return ntohs(h->client_id);
86 }
87
msg_get_type(msg_hdr_t * h)88 static _inline_ uint8_t msg_get_type(msg_hdr_t *h)
89 {
90 return h->type;
91 }
92
msg_get_length(msg_hdr_t * h)93 static _inline_ uint16_t msg_get_length(msg_hdr_t *h)
94 {
95 return ntohs(h->length);
96 }
97
98 #endif /* MESSAGE_H */
99