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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. {
  86. char ntk_id[3];
  87. int id;
  88. u_char flags;
  89. u_char op;
  90. size_t sz; /* The size of the message */
  91. size_t uncompress_sz; /* The size of the decompressed packet. */
  92. }_PACKED_ pkt_hdr;
  93. INT_INFO pkt_hdr_iinfo = { 3,
  94. { INT_TYPE_32BIT, INT_TYPE_32BIT, INT_TYPE_32BIT },
  95. { sizeof(char)*3, sizeof(char)*5+sizeof(int),
  96. sizeof(char)*5+sizeof(int)+sizeof(size_t) },
  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. {
  108. /* General informations of the packet */
  109. inet_prefix from; /* The sender ip of this packet */
  110. inet_prefix to; /* Where to send this packet */
  111. interface *dev; /* Device used to send/receive the
  112. packet. `sk' will be bound to it
  113. if `dev' is not null and if the
  114. PKT_BIND_DEV flag is set in
  115. `pkt_flags'. `dev' is a pointer
  116. to a struct contained in the
  117. me.cur_ifs array. */
  118. int family;
  119. int sk;
  120. char sk_type;
  121. u_short port;
  122. u_char pkt_flags; /*Flags for this PACKET*/
  123. int flags; /*Flags used by send/recv*/
  124. u_int timeout; /*After `timeout' seconds give up the
  125. send/recv of the packet.
  126. The PKT_[RECV/SEND]_TIMEOUT flags are
  127. used to determine its scope (send,
  128. recv or both).*/
  129. /* Body of the packet */
  130. pkt_hdr hdr;
  131. char *msg;
  132. } PACKET;
  133. /*Broadcast packet*/
  134. typedef struct
  135. {
  136. u_char g_node; /*The gnode the brdcast_pkt is restricted to*/
  137. u_char level; /*The level of the g_node*/
  138. u_char gttl; /*Gnode ttl: How many gnodes the packet
  139. can traverse*/
  140. u_char sub_id; /*The sub_id is the node who sent the pkt,
  141. but is only used by the qspn_open*/
  142. size_t sz; /*Sizeof(the pkt)*/
  143. char flags; /*Various flags*/
  144. }_PACKED_ brdcast_hdr;
  145. INT_INFO brdcast_hdr_iinfo = { 1, { INT_TYPE_32BIT }, { sizeof(char)*4 }, { 1 } };
  146. #define BRDCAST_SZ(pkt_sz) (sizeof(brdcast_hdr)+(pkt_sz))
  147. #define BRDCAST_HDR_PTR(msg) ((brdcast_hdr *)(msg))
  148. /*
  149. * In this stable, each op (request or reply) is associated with a
  150. * `pkt_exec_func', which pkt_exec() will use to handle the incoming packets of
  151. * the same op.
  152. * Each op is also associated with its specific socket type (udp, tcp, bcast)
  153. * with `sk_type', and the `port' where the pkt will be sent or received.
  154. * Each element in the table is equivalent to a request or reply, ie the
  155. * function to handle the x request is at pkt_op_table[x].exec_func;
  156. */
  157. struct pkt_op_table {
  158. char sk_type;
  159. u_short port;
  160. void *exec_func;
  161. } pkt_op_tbl[TOTAL_OPS];
  162. /* pkt_queue's flags */
  163. #define PKT_Q_MTX_LOCKED 1 /* We are waiting the reply */
  164. #define PKT_Q_PKT_RECEIVED (1<<1) /* The reply was received */
  165. #define PKT_Q_TIMEOUT (1<<2) /* None replied ._, */
  166. #define PKT_Q_CHECK_FROM (1<<3) /* Check the from ip while
  167. receiving the async pkt */
  168. /*
  169. * The pkt_queue is used when a reply will be received with a completely new
  170. * connection. This is how it works:
  171. * The pkt.hdr.flags is ORed with ASYNC_REPLY, a new struct is added in the
  172. * pkt_q linked list, pkt_q->pkt.hdr.id is set to the id of the outgoing pkt
  173. * and pkt_q->pkt.hdr.op is set to the waited reply op.
  174. * The function x() it's started as a new thread and the request is sent; to
  175. * receive the reply, x() locks twice `mtx'. The thread is now freezed.
  176. * The reply is received by pkt_exec() which passes the pkt to the function
  177. * y(). y() searches in the pkt_q a struct which has the same pkt.hdr.id of
  178. * the received pkt. The reply pkt is copied in the found struct and `mtx' is
  179. * unlocked. x() can now continue to read the reply and unlocks `mtx'.
  180. * Note that the reply pkt must have the ASYNC_REPLIED flag set in pkt.hdr.flags.
  181. */
  182. struct pkt_queue{
  183. LLIST_HDR (struct pkt_queue);
  184. PACKET pkt;
  185. pthread_mutex_t mtx;
  186. char flags;
  187. };
  188. typedef struct pkt_queue pkt_queue;
  189. pkt_queue *pkt_q;
  190. int pkt_q_counter;
  191. /*Functions' declarations*/
  192. void pkts_init(interface *ifs, int ifs_n, int queue_init);
  193. void pkt_addfrom(PACKET *pkt, inet_prefix *from);
  194. void pkt_addto(PACKET *pkt, inet_prefix *to);
  195. void pkt_add_dev(PACKET *pkt, interface *dev, int bind_the_socket);
  196. void pkt_addsk(PACKET *pkt, int family, int sk, int sk_type);
  197. void pkt_addport(PACKET *pkt, u_short port);
  198. void pkt_addflags(PACKET *pkt, int flags);
  199. void pkt_addtimeout(PACKET *pkt, u_int timeout, int recv, int send);
  200. void pkt_addcompress(PACKET *pkt);
  201. void pkt_addlowdelay(PACKET *pkt);
  202. void pkt_addnonblock(PACKET *pkt);
  203. void pkt_addhdr(PACKET *pkt, pkt_hdr *hdr);
  204. void pkt_addmsg(PACKET *pkt, char *msg);
  205. void pkt_copy(PACKET *dst, PACKET *src);
  206. void pkt_clear(PACKET *pkt);
  207. void pkt_free(PACKET *pkt, int close_socket);
  208. char *pkt_pack(PACKET *pkt);
  209. int pkt_verify_hdr(PACKET pkt);
  210. ssize_t pkt_send(PACKET *pkt);
  211. ssize_t pkt_recv(PACKET *pkt);
  212. int pkt_tcp_connect(inet_prefix *host, short port, interface *dev);
  213. void pkt_fill_hdr(pkt_hdr *hdr, u_char flags, int id, u_char op, 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, int (*exec_f)(PACKET pkt));
  230. int pkt_exec(PACKET pkt, int acpt_idx);
  231. void pkt_queue_init(void);
  232. void pkt_queue_close(void);
  233. int pkt_q_wait_recv(int id, inet_prefix *from, PACKET *rpkt, pkt_queue **ret_pq);
  234. int pkt_q_add_pkt(PACKET pkt);
  235. void pkt_q_del(pkt_queue *pq, int close_socket);
  236. #endif /*PKTS_H*/