/*

 esp_route.h

 Author: Liraz Siri <liraz@bigfoot.com>

 Copyright (c) 1998 Liraz Siri <liraz@bigfoot.com> Ariel, Israel.
                    All rights reserved.

 Created: Sun Sep 27 12:00:49 GMT 1998
 Updated: Mon Sep 28 11:56:01 GMT 1998

 Packet router mechanism. ssh_esp extension.
*/

#include <netinet/in.h>
#include "esp.h"

typedef struct {
 struct in_addr sin_addr;
 u_char protocol;
 u_short srcport;
 u_short dstport;
} pktrec; 

/* This is meant to be a sort of pipe. If the packet received matched
   'source', then reassemble the packet to fit dest specifications,
   and send it off */

typedef struct {
 int ttl;
 pktrec source;
 pktrec dest;
} routerule;

struct esp_protodata {
 u_long sequence;
 u_char code;
 u_char *payload;
 int payload_len;
};

#define ROUTE_RULES	32
#define MAX_PROTOS	5

/******************************
 * Public routine prototypes: *
 ******************************/

/*-- return routing_supported --*/
int esp_route_supported(void);

/*-- Get rid of the unused routing sockets in a child process --*/
void esp_route_close_sockets(void);

/*-- Initialize internal route structures, buffers. shared memory, etc. --*/
int esp_route_init(void);

/*-- Remove ALL the rules, free all protocols. --*/
void esp_route_rm_rules(void);

/*   
 * Remove a rule:
 * - Destroy the rule structure.
 * - Free any memory used by the rule.
 * - Remove any unused protocol sockets. 
 */			
int esp_route_rm_rule(routerule *rule, int free_memory);

/*------------------------------------------------------------------* 
   Set route rule. packets processed by the routing mechanism
   will be crossreferenced with the source field of the routerule.
   If a match is found, they will be reassembled using the dest
   field of the routerule and send off. note that setrule also
   initializes a matching protocol socket based on the dest.protocol
   routerule field.
 *------------------------------------------------------------------*/
int esp_route_setrule(routerule *rule);

/*-----------------------------------------------------------------*
   handle raw ip packet (assumption: handling valid ip packets).
   We parse the packet, try to find a matching rule for the packet,
   reassemble the packet with the 'dest' side of the rule, and
   transmit the packet.
 *-----------------------------------------------------------------*/
int esp_route_handle_packet(const u_char *packet, int len);




/*******************************
 * PRIVATE routine prototypes: *
 *******************************/

/*-- Find the slot in the protos area matching the protocol proto --*/
int esp_route_find_proto(u_char proto);

/*-- Try to find the slot of a rule transmitting with protocol 'proto',
     return -1 if failed to find such a rule --*/
int esp_route_find_rule_proto(u_char proto);

/*-- Remove an obsolete protocol socket (used for transmission) --*/
int esp_route_rm_proto(u_char proto);

/* Returns true if pktrec1 and pktrec2 match, otherwise returns false */
int cmp_pktrec(pktrec *pktrec1, pktrec *pktrec2);

/*-- Find the slot of a rule with a matching source field --*/
int esp_route_find_rule(pktrec *source);

/* Return 0 if already initialized, 1 if initialized successfully, and -1
   on socket failure/no more space in protos array */
int esp_route_initproto(u_char proto);

void update_protocols(void);
/*-- decrement ttl unless of negative value. expire the rule when ttl
     hits null. --*/
void age_rule(routerule *rule);

/*--                                                                  --*
   parse packet into a pktrec, to be crossreferenced with our set of
   rules, and protodata, which holds all our valuable protocol data,
   including sequence, code, payload and payload length.
 *--                                                                  --*/
int parse_pkt(pktrec *pkt_rec, struct esp_protodata *protodata,
              const u_char *packet, int len);

/*-- Assemble a raw protocol/ip packet --*/
int assemble_pkt(u_char **packet, pktrec *pkt_rec,
                 struct esp_protodata *protodata);

/*-- reassemble/route packet --*/
int esp_route_transmit(pktrec *pkt_rec,struct esp_protodata *protodata);

