/*
  ip.h

  Miguel Masmano <mmasmano@disca.upv.es>

  Copyright (C) OCERA Consortium Sept. 2003
  Released under the terms of the GPL v2 license
*/

#ifndef _IP_PROT_H_
#define _IP_PROT_H_

#include "ethernet.h"
#include "nic.h"

#define MAX_IP_DATA (ETH_MAX_MTU-20)
#define MAX_ICMP_DATA (MAX_IP_DATA-8)
#define MAX_UDP_DATA (MAX_IP_DATA-8)

#define IP              0x0800
#define ARP             0x0806
#define RARP            0x8035
#define UDP             17
#define ICMP            01

#define ARP_REQUEST     1
#define ARP_REPLY       2
#define	RARP_REQUEST	3
#define	RARP_REPLY	4

#define MAX_ARP           10 
#define WAIT_ARP_RESPONSE 1000 

/*
  ICMP ECHO, ICMP DESTINATION UNREACHABLE are implemented
*/

#define ICMP_ECHO_REPLY               0
#define ICMP_DESTINATION_UNREACHABLE  3
#define ICMP_SOURCE_QUECH             4 
#define ICMP_REDIRECT                 5
#define ICMP_ECHO_REQUEST             8 
#define ICMP_TIME_EXCEEDED           11
#define ICMP_PARAMETER_PROBLEM       12
#define ICMP_TIMESTAMP_REQUEST       13
#define ICMP_TIMESTAMP_REPLY         14
#define ICMP_INFORMATION_REQUEST     15
#define ICMP_INFORMATION_REPLY       16
#define ICMP_ADDRESS_MASK_REQUEST    17
#define ICMP_ADDRESS_MASK_REPLY      18

/*ICMP DESTINATION UNREACHABLE possible menssages */

#define NET_UNREACHABLE                 0
#define HOST_UNREACHABLE                1
#define PROTOCOL_UNREACHABLE            2
#define PORT_UNREACHABLE                3
#define FRAGMENTATION_NEEDED_AND_DF_SET 4
#define SOURCE_ROUTE_FAILED             5

#define IP_BROADCAST	0xFFFFFFFF

#define TICKS_PER_SEC           18

#define TIMEOUT                 (10*TICKS_PER_SEC)

/* IP address */

#ifdef MARTE_STANDALONE
struct in_addr {
  unsigned long	s_addr;
};
#else

#include <linux/slab.h>
#include <linux/in.h>

#endif

struct arp_hdr {
  unsigned short hwtype;
  unsigned short protocol;
  char hwlen;
  char protolen;
  unsigned short opcode;
  char shwaddr[6];
  char sipaddr[4];
  char thwaddr[6];
  char tipaddr[4];
};

struct udp_hdr {
  unsigned short src;
  unsigned short dest;
  unsigned short len;
  unsigned short chksum;
  char buffer[MAX_UDP_DATA];
};

union icmp_hdr{
  struct {
    char type;
    char code;
    unsigned short chksum;
    unsigned short id;
    unsigned short seq;
    char buffer[MAX_ICMP_DATA];
  } echo;
  struct {
    char type;
    char code;
    unsigned short chksum;
    unsigned int zero;
    char buffer[MAX_ICMP_DATA];
  } reports;
}; 

struct ip_hdr {
  char verhdrlen;
  char service;
  unsigned short len;
  unsigned short ident;
  unsigned short frags;
  unsigned char ttl;
  unsigned char protocol;
  unsigned short chksum;
  struct in_addr src;
  struct in_addr dest;
  union {
    union icmp_hdr icmp;
    struct udp_hdr udp;
  } data;
};

extern unsigned long inet_addr (char *p);

extern int inet_aton (char *p, struct in_addr *ipaddr);

extern int udp_send (struct in_addr ipad, 
		     unsigned short srcport, 
		     unsigned short destport, 
		     void *buffer, 
		     int len_b);

extern int rtl_register_net_driver (struct nic *nic_driver, 
		                    char *description,
				    char *ip_addr, 
				    char *ip_mask, int with_arp);

extern void rtl_unregister_net_driver (int position_value);

#endif
