/*
    Copyright 2000 (c) by Claudio Jeker,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public 
    Licence. A full copy of the GNU GPL is provided in the file COPYING 
    found in the development root of Topsy.
*/
/*
	
    File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Net/NetPCB.h,v $
    Author(s):             
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 2922 $
    Creation Date:         
    Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $  by: $Author: baderma $
 
*/
#ifndef _NETPCB_H_
#define _NETPCB_H_

#include "NetAttr.h"
#include "Syscall.h"
#include "NetProtocols.h"

typedef struct NetPCB *NetPCB;
struct NetPCB
{
  NetPCB		next, *prev;	/* doubly linked list */
  NetPCB		hashNext, 		/* address to pcb */
  *hashPrev;		/* hash table for fast access */
  NetAttrs	attributes;		/* all needed information about this connection */
  ThreadId	socket;			/* "back pointer" to socket */
  Address		privatePCB;		/* protocol specific PCB */
  unsigned short    so_domain;	/* generic domain, AF_INET */
  unsigned short    so_protocol;	/* generic protocol, TCP or UDP */
  /* XXX the queue of incomming connections is only needed for TCP
     connections in the listen state, so those variables should be added
     in the privatePCB of the TCP PCB */
#if 0

  NetPCB		q;				/* queue of incomming connections */
  NetPCB		q0;				/* queue of half open connections */
  short		qlen;			/* number of incomming connections */
  short		q0len;			/* number of half open connections */
  short		qlimit;			/* max number of queued incomming connections */
#endif
};

struct NetPCBTable
{
  NetPCB			first;		/* doubly linked list */
  NetPCB			*hashTable;	/* hash table for fast access */
  unsigned long	hashMask;	/* hash table mask = size - 1 */
  unsigned short	lastPort;	/* the last used port */
  unsigned short	pad; /*XXX*/
};
typedef struct NetPCBTable *NetPCBTable;

#define PCB_UDP_HASHSIZE 64
#define PCB_TCP_HASHSIZE 1 /* XXX to save mem  not yet used */

/* init functions */
int netInitPCB(void);
int netConfPCB(NetPCB pcb, ThreadId socket, Address privatePCB,
               unsigned short    so_domain,
               unsigned short    so_protocol);
/* lookup functions */
NetPCBTable netGetPCBTable(unsigned short type, unsigned short protocol);
NetPCB netSocket2PCB(ThreadId socket);
NetPCB netAddress2PCB(NetPCBTable head, long l1, long l2, long l3,
                      int (*compare)(NetAttrs pcb_attr, NetAttrs find), NetAttrs find);
NetPCB netV6Address2PCB(NetPCBTable head, long l1[4], long l2, long l3,
                        int (*compare)(NetAttrs pcb_attr, NetAttrs find), NetAttrs find);
/* configuration functions */
void netUnsetOptPCB(NetPCB pcb, NetAttr *attrVec, unsigned int len);
int netSetOptPCB(NetPCB pcb, NetAttr *attrVec, unsigned int len);
int netBindPCB(NetPCBTable head, NetPCB pcb, int id,
               NetAttr *attrVec, unsigned int len);
/* alloc and remove stuff */
int netAddPCB(NetPCBTable head, NetPCB pcb);
void netUnlinkPCB(NetPCB pcb);
NetPCB netAllocPCB(void);
void netFreePCB(NetPCB pcb);

#endif
