You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pkts.h 8.9KB

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