The Netsukuku Project  0.0.9
An Alternative routing method
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
pkts.h
Go to the documentation of this file.
1 /* This file is part of Netsukuku system
2  * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
3  *
4  * This source code is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This source code 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.
12  * Please refer to the GNU Public License for more details.
13  *
14  * You should have received a copy of the GNU Public License along with
15  * this source code; if not, write to:
16  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #ifndef PKTS_H
20 #define PKTS_H
21 
22 #include "if.h"
23 #include "request.h"
24 #include "llist.c"
25 
26 #define NETSUKUKU_ID "ntk"
27 #define MAXMSGSZ 65536
28 
29 /*\
30  *
31  * Pkt's op definitions
32  * (The requests and replies are in request.h)
33  *
34 \*/
35 
36 /* Pkt.sk_type */
37 #define SKT_TCP 1
38 #define SKT_UDP 2
39 #define SKT_BCAST 3
40 
41 /*
42  * Pkt.pkt_flags flags
43  */
44 #define PKT_BIND_DEV 1 /* Bind the pkt.sk socket to pkt.dev */
45 #define PKT_RECV_TIMEOUT (1<<1)
46 #define PKT_SEND_TIMEOUT (1<<2)
47 #define PKT_SET_LOWDELAY (1<<3)
48 #define PKT_COMPRESSED (1<<4) /* If set the packet will be Z
49  compressed before being sent */
50 #define PKT_KEEPALIVE (1<<5) /* Let the pkt.sk socket be alive */
51 #define PKT_NONBLOCK (1<<6) /* Socket must not block */
52 
53 /*
54  * Pkt.hdr flags
55  */
56 #define SEND_ACK 1
57 #define BCAST_PKT (1<<1) /* In this pkt there is encapsulated a
58  * broadcast/flood pkt. Woa */
59 #define HOOK_PKT (1<<2) /* All the pkts sent while hooking have
60  * this flag set */
61 #define ASYNC_REPLY (1<<3) /* Tells the receiver to reply with a new
62  connection. The reply pkt will be
63  handled by the pkt_queue. */
64 #define ASYNC_REPLIED (1<<4)
65 #define LOOPBACK_PKT (1<<5) /* This is a packet destinated to me */
66 #define RESTRICTED_PKT (1<<6) /* Packet sent from a node in restricted
67  mode */
68 #define COMPRESSED_PKT (1<<7) /* The whole packet is Z compressed */
69 
70 
71 /*
72  * Broacast ptk's flags
73  */
74 #define BCAST_TRACER_PKT 1 /*When a bcast is marked with this, it
75  acts as a tracer_pkt ;)*/
76 #define BCAST_TRACER_BBLOCK (1<<1) /*When set, the tracer pkt carries also
77  bnodes blocks.*/
78 #define BCAST_TRACER_STARTERS (1<<2) /*Tracer pkt bound to the qspn starter
79  continual group*/
80 #define QSPN_BNODE_CLOSED (1<<3) /*The last bnode, who forwarded this
81  qspn pkt has all its links closed.*/
82 #define QSPN_BNODE_OPENED (1<<4)
83 
84 /* General defines */
85 #define PKT_MAX_MSG_SZ 1048576 /* bytes */
86 #define PKT_COMPRESS_LEVEL Z_DEFAULT_COMPRESSION
87 #define PKT_COMPRESS_THRESHOLD 1024 /* If the flag PKT_COMPRESSED is set
88  and hdr.sz > PKT_COMPRESS_THRESHOLD,
89  then compress the packet */
90 
91 /*
92  * pkt_hdr: the pkt_hdr is always put at the very beginning of any netsukuku
93  * packets
94  */
95 typedef struct
96 {
97  char ntk_id[3];
98  int id;
99  u_char flags;
100  u_char op;
101  size_t sz; /* The size of the message */
102  size_t uncompress_sz; /* The size of the decompressed packet. */
103 }_PACKED_ pkt_hdr;
105  { INT_TYPE_32BIT, INT_TYPE_32BIT, INT_TYPE_32BIT },
106  { sizeof(char)*3, sizeof(char)*5+sizeof(int),
107  sizeof(char)*5+sizeof(int)+sizeof(size_t) },
108  { 1, 1, 1 }
109  };
110 #define PACKET_SZ(sz) (sizeof(pkt_hdr)+(sz))
111 
112 /*
113  * PACKET
114  *
115  * this struct is used only to represent internally a packet, which
116  * will be sent or received.
117  */
118 typedef struct
119 {
120  /* General informations of the packet */
121 
122  inet_prefix from; /* The sender ip of this packet */
123  inet_prefix to; /* Where to send this packet */
124 
125  interface *dev; /* Device used to send/receive the
126  packet. `sk' will be bound to it
127  if `dev' is not null and if the
128  PKT_BIND_DEV flag is set in
129  `pkt_flags'. `dev' is a pointer
130  to a struct contained in the
131  me.cur_ifs array. */
132 
133  int family;
134  int sk;
135  char sk_type;
136  u_short port;
137 
138  u_char pkt_flags; /*Flags for this PACKET*/
139  int flags; /*Flags used by send/recv*/
140 
141  u_int timeout; /*After `timeout' seconds give up the
142  send/recv of the packet.
143  The PKT_[RECV/SEND]_TIMEOUT flags are
144  used to determine its scope (send,
145  recv or both).*/
146 
147  /* Body of the packet */
148  pkt_hdr hdr;
149  char *msg;
150 } PACKET;
151 
152 /*Broadcast packet*/
153 typedef struct
154 {
155  u_char g_node; /*The gnode the brdcast_pkt is restricted to*/
156  u_char level; /*The level of the g_node*/
157  u_char gttl; /*Gnode ttl: How many gnodes the packet
158  can traverse*/
159  u_char sub_id; /*The sub_id is the node who sent the pkt,
160  but is only used by the qspn_open*/
161  size_t sz; /*Sizeof(the pkt)*/
162  char flags; /*Various flags*/
163 }_PACKED_ brdcast_hdr;
164 
165 INT_INFO brdcast_hdr_iinfo = { 1, { INT_TYPE_32BIT }, { sizeof(char)*4 }, { 1 } };
166 #define BRDCAST_SZ(pkt_sz) (sizeof(brdcast_hdr)+(pkt_sz))
167 #define BRDCAST_HDR_PTR(msg) ((brdcast_hdr *)(msg))
168 
169 
170 /*
171  * In this stable, each op (request or reply) is associated with a
172  * `pkt_exec_func', which pkt_exec() will use to handle the incoming packets of
173  * the same op.
174  * Each op is also associated with its specific socket type (udp, tcp, bcast)
175  * with `sk_type', and the `port' where the pkt will be sent or received.
176  * Each element in the table is equivalent to a request or reply, ie the
177  * function to handle the x request is at pkt_op_table[x].exec_func;
178  */
179 struct pkt_op_table {
180  char sk_type;
181  u_short port;
182  void *exec_func;
184 
185 /* pkt_queue's flags */
186 #define PKT_Q_MTX_LOCKED 1 /* We are waiting the reply */
187 #define PKT_Q_PKT_RECEIVED (1<<1) /* The reply was received */
188 #define PKT_Q_TIMEOUT (1<<2) /* None replied ._, */
189 #define PKT_Q_CHECK_FROM (1<<3) /* Check the from ip while
190  receiving the async pkt */
191 
192 /*
193  * The pkt_queue is used when a reply will be received with a completely new
194  * connection. This is how it works:
195  * The pkt.hdr.flags is ORed with ASYNC_REPLY, a new struct is added in the
196  * pkt_q linked list, pkt_q->pkt.hdr.id is set to the id of the outgoing pkt
197  * and pkt_q->pkt.hdr.op is set to the waited reply op.
198  * The function x() it's started as a new thread and the request is sent; to
199  * receive the reply, x() locks twice `mtx'. The thread is now freezed.
200  * The reply is received by pkt_exec() which passes the pkt to the function
201  * y(). y() searches in the pkt_q a struct which has the same pkt.hdr.id of
202  * the received pkt. The reply pkt is copied in the found struct and `mtx' is
203  * unlocked. x() can now continue to read the reply and unlocks `mtx'.
204  * Note that the reply pkt must have the ASYNC_REPLIED flag set in pkt.hdr.flags.
205  */
206 struct pkt_queue{
208 
210  pthread_mutex_t mtx;
211 
212  char flags;
213 };
214 typedef struct pkt_queue pkt_queue;
215 
218 
219 /*Functions' declarations*/
220 void pkts_init(interface *ifs, int ifs_n, int queue_init);
221 
222 void pkt_addfrom(PACKET *pkt, inet_prefix *from);
223 void pkt_addto(PACKET *pkt, inet_prefix *to);
224 void pkt_add_dev(PACKET *pkt, interface *dev, int bind_the_socket);
225 void pkt_addsk(PACKET *pkt, int family, int sk, int sk_type);
226 void pkt_addport(PACKET *pkt, u_short port);
227 void pkt_addflags(PACKET *pkt, int flags);
228 void pkt_addtimeout(PACKET *pkt, u_int timeout, int recv, int send);
229 void pkt_addcompress(PACKET *pkt);
230 void pkt_addlowdelay(PACKET *pkt);
231 void pkt_addnonblock(PACKET *pkt);
232 void pkt_addhdr(PACKET *pkt, pkt_hdr *hdr);
233 void pkt_addmsg(PACKET *pkt, char *msg);
234 void pkt_copy(PACKET *dst, PACKET *src);
235 void pkt_clear(PACKET *pkt);
236 
237 void pkt_free(PACKET *pkt, int close_socket);
238 char *pkt_pack(PACKET *pkt);
239 
241 ssize_t pkt_send(PACKET *pkt);
242 ssize_t pkt_recv(PACKET *pkt);
243 int pkt_tcp_connect(inet_prefix *host, short port, interface *dev);
244 
245 void pkt_fill_hdr(pkt_hdr *hdr, u_char flags, int id, u_char op, size_t sz);
246 
247 #define SEND_RQ_ERR -1
248 #define SEND_RQ_ERR_RQ -2
249 #define SEND_RQ_ERR_RE -3
250 #define SEND_RQ_ERR_PORT -4
251 #define SEND_RQ_ERR_TO -5
252 #define SEND_RQ_ERR_CONNECT -6
253 #define SEND_RQ_ERR_SEND -7
254 #define SEND_RQ_ERR_RECV -8
255 #define SEND_RQ_ERR_RECVOP -9
256 #define SEND_RQ_ERR_RECVID -10
257 #define SEND_RQ_ERR_REPLY -11
258 int send_rq(PACKET *pkt, int pkt_flags, u_char rq, int rq_id, u_char re,
259  int check_ack, PACKET *rpkt);
260 
261 int forward_pkt(PACKET rpkt, inet_prefix to);
262 int pkt_err(PACKET pkt, u_char err, int free_pkt);
263 
264 void add_pkt_op(u_char op, char sk_type, u_short port, int (*exec_f)(PACKET pkt));
265 int pkt_exec(PACKET pkt, int acpt_idx);
266 
267 void pkt_queue_init(void);
268 void pkt_queue_close(void);
269 int pkt_q_wait_recv(int id, inet_prefix *from, PACKET *rpkt, pkt_queue **ret_pq);
271 void pkt_q_del(pkt_queue *pq, int close_socket);
272 
273 #endif /*PKTS_H*/
pthread_mutex_t mtx
Definition: pkts.h:210
void pkt_addtimeout(PACKET *pkt, u_int timeout, int recv, int send)
Definition: pkts.c:94
u_char gttl
Definition: pkts.h:157
u_char op
Definition: pkts.h:100
inet_prefix from
Definition: pkts.h:122
void pkt_addto(PACKET *pkt, inet_prefix *to)
Definition: pkts.c:67
int id
Definition: pkts.h:98
char sk_type
Definition: pkts.h:180
PACKET pkt
Definition: pkts.h:209
u_int timeout
Definition: pkts.h:141
void pkt_addsk(PACKET *pkt, int family, int sk, int sk_type)
Definition: pkts.c:82
char sk_type
Definition: pkts.h:135
pkt_hdr hdr
Definition: pkts.h:148
void add_pkt_op(u_char op, char sk_type, u_short port, int(*exec_f)(PACKET pkt))
Definition: pkts.c:563
char * msg
Definition: pkts.h:149
int pkt_q_add_pkt(PACKET pkt)
Definition: pkts.c:1003
int pkt_q_counter
Definition: pkts.h:217
ssize_t pkt_recv(PACKET *pkt)
Definition: pkts.c:477
int send_rq(PACKET *pkt, int pkt_flags, u_char rq, int rq_id, u_char re, int check_ack, PACKET *rpkt)
Definition: pkts.c:604
void pkt_addflags(PACKET *pkt, int flags)
u_char sub_id
Definition: pkts.h:159
struct pkt_op_table pkt_op_tbl[(ACK_NEGATIVE+1)]
ssize_t pkt_send(PACKET *pkt)
Definition: pkts.c:335
Definition: inet.h:73
static const int_info brdcast_hdr_iinfo
Definition: pkts.h:165
Definition: pkts.h:206
void pkt_addport(PACKET *pkt, u_short port)
Definition: pkts.c:89
void pkt_free(PACKET *pkt, int close_socket)
Definition: pkts.c:162
char flags
Definition: pkts.h:162
void pkts_init(interface *ifs, int ifs_n, int queue_init)
Definition: pkts.c:44
int sk
Definition: pkts.h:134
Definition: if.h:29
u_short port
Definition: pkts.h:136
void pkt_addhdr(PACKET *pkt, pkt_hdr *hdr)
Definition: pkts.c:119
interface * dev
Definition: pkts.h:125
void pkt_queue_init(void)
Definition: pkts.c:878
Definition: pkts.h:179
#define TOTAL_OPS
Definition: request.h:102
void pkt_addmsg(PACKET *pkt, char *msg)
Definition: pkts.c:127
int family
Definition: pkts.h:133
static const int_info pkt_hdr_iinfo
Definition: pkts.h:104
u_char g_node
Definition: pkts.h:155
u_short port
Definition: pkts.h:181
void pkt_add_dev(PACKET *pkt, interface *dev, int bind_the_socket)
Definition: pkts.c:75
void pkt_copy(PACKET *dst, PACKET *src)
Definition: pkts.c:151
inet_prefix to
Definition: pkts.h:123
u_char pkt_flags
Definition: pkts.h:138
u_char flags
Definition: pkts.h:99
int pkt_tcp_connect(inet_prefix *host, short port, interface *dev)
Definition: pkts.c:502
#define INT_INFO
Definition: endianness.h:90
void * exec_func
Definition: pkts.h:182
size_t uncompress_sz
Definition: pkts.h:102
int pkt_err(PACKET pkt, u_char err, int free_pkt)
Definition: pkts.c:775
int pkt_q_wait_recv(int id, inet_prefix *from, PACKET *rpkt, pkt_queue **ret_pq)
Definition: pkts.c:946
void pkt_addfrom(PACKET *pkt, inet_prefix *from)
Definition: pkts.c:59
pkt_queue * pkt_q
Definition: pkts.h:216
void pkt_addcompress(PACKET *pkt)
Definition: pkts.c:104
void pkt_clear(PACKET *pkt)
Definition: pkts.c:138
Definition: pkts.h:118
size_t sz
Definition: pkts.h:101
#define LLIST_HDR(_struct)
Definition: llist.c:44
int family
Definition: if.c:34
int flags
Definition: if.c:39
void pkt_fill_hdr(pkt_hdr *hdr, u_char flags, int id, u_char op, size_t sz)
Definition: pkts.c:545
void pkt_addnonblock(PACKET *pkt)
Definition: pkts.c:114
void pkt_q_del(pkt_queue *pq, int close_socket)
Definition: pkts.c:1043
void pkt_queue_close(void)
Definition: pkts.c:886
#define INT_TYPE_32BIT
Definition: endianness.h:35
void pkt_addlowdelay(PACKET *pkt)
Definition: pkts.c:109
int pkt_exec(PACKET pkt, int acpt_idx)
Definition: pkts.c:813
int forward_pkt(PACKET rpkt, inet_prefix to)
Definition: pkts.c:756
char * pkt_pack(PACKET *pkt)
Definition: pkts.c:227
int pkt_verify_hdr(PACKET pkt)
Definition: pkts.c:320
int flags
Definition: pkts.h:139
Definition: bmap.h:89
char flags
Definition: pkts.h:212