/*
    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/NetSocket.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 _NETSOCKET_H_
#define _NETSOCKET_H_

#include "IODevice.h"
#include "NetQueue.h"

typedef struct SocketDesc_t * SocketDescPtr;

typedef struct SocketDesc_t
{
  unsigned short    so_domain;	/* generic domain, AF_INET */
  unsigned short    so_type;		/* generic type, SOCK_STREAM */
  unsigned short    so_protocol;	/* generic protocol, TCP or UDP */
  unsigned short    so_options;	/* generic socket options */
  unsigned short    so_state;		/* state of socket */
  unsigned short    so_error;		/* last error */
  SocketDescPtr so_head;	/* back pointer to accept socket */
  SocketDescPtr so_q;		/* queue of incomming connections */
  short    so_qlen;		/* number of incomming connections */
  short    so_qlimit;		/* max number of queued connections */
  NetQueue so_rcv;		/* recieve buffer */
  NetQueue so_snd;		/* send buffer */
  ThreadId subscribed;	/* used for ioSubscribe */
}
SocketDesc;

Error socket_init (IODevice this);
Error socket_read (IODevice this, ThreadId threadId,
                   char *buffer, long int *size);
Error socket_getPort(IODevice this, ThreadId threadId, unsigned short *port);
Error socket_write (IODevice this, ThreadId threadId,
                    char *buffer, long int *size);
Error socket_close (IODevice this);
void socket_handleMsg (IODevice this, Message * msg);


/*
 * Definitions related to sockets: types, address families, options.
 * same as in *BSD
 */

/*
 * Address families.
 */
#define	AF_UNSPEC	0		/* unspecified */
#define	AF_LOCAL	1		/* local to host (pipes, portals) */
#define	AF_UNIX		AF_LOCAL	/* backward compatibility */
#define	AF_INET		2		/* internetwork: UDP, TCP, etc. */
#define	AF_INET6	24		/* IPv6 */


/*
 * Types
 */
#define	SOCK_STREAM	1		/* stream socket */
#define	SOCK_DGRAM	2		/* datagram socket */
#define	SOCK_RAW	3		/* raw-protocol interface */

/*
 * Protocol families, same as address families for now. Normaly not used
 */
#define	PF_UNSPEC	AF_UNSPEC
#define	PF_LOCAL	AF_LOCAL
#define	PF_UNIX		PF_LOCAL	/* backward compatibility */
#define	PF_INET		AF_INET
#define	PF_INET6	AF_INET6

/* Protocol are defined in Net/NetProtocols.h use those values */

/*
 * Socket state
 */
#define	ISINITIALIZED	0x1
#define ISCONNECTED		0x10
#define ISCONNECTING	0x20
#define ISCONFIRMING	0x22
#define ISDISCONECTING	0x24
#define ISLISTENING		0x28
#define ISINVALID		0xFF

#endif
