/* vim: set filetype=c:
 *
 * in.h86  -  Internet network definitions
 *
 * Copyright (C) 2003-2007 Gero Kuhlmann   <gero@gkminix.han.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: in.h86,v 1.7 2007/01/06 18:30:54 gkminix Exp $
 */

#ifndef NET_IN_H86
#define NET_IN_H86


/*
 * This file can only get included into target C or assembler source files.
 */
#define GCC_INCLUDE
#include <compiler.h>



/*
 * Internet address length, and type:
 */
#ifndef _ASSEMBLY_
typedef __u32 t_ipaddr;
struct in_addr {
	t_ipaddr s_addr;
};
#endif

#define IP_ALEN	4



/*
 * Standard well-known IP protocols
 */
#define IPPROTO_ICMP	1	/* Internet Control Message Protocol */
#define IPPROTO_IGMP	2	/* Internet Group Management Protocol */
#define IPPROTO_TCP	6	/* Transmission Control Protocol */
#define IPPROTO_UDP	17	/* User Datagram Protocol */
#define IPPROTO_RAW	255	/* Raw IP packets */



/*
 * Standard well-known IP ports (some useful ports, not the whole
 * /etc/services file ;-))
 */
#define IPPORT_ECHO	7
#define IPPORT_SYSTAT	11
#define IPPORT_TELNET	23
#define IPPORT_TIME	37
#define IPPORT_DOMAIN	53
#define IPPORT_BOOTPS	67
#define IPPORT_BOOTPC	68
#define IPPORT_TFTP	69
#define IPPORT_HTTP	80
#define IPPORT_SUNRPC	111
#define IPPORT_AUTH	113
#define IPPORT_SNMP	161
#define IPPORT_SNMPTRAP	162
#define IPPORT_SYSLOG	514
#define IPPORT_USER	1024



/*
 * Special internet addresses:
 */
#ifndef _ASSEMBLY_

/* Address to accept any incoming messages */
#define	IP_ANY		((t_ipaddr) 0x00000000UL)

/* Address for a default route */
#define IP_DEFAULT	((t_ipaddr) 0x00000000UL)

/* Address to send to all hosts */
#define	IP_BROADCAST	((t_ipaddr) 0xffffffffUL)

/* Address to loopback in software to local host  */
#define	IP_LOCALHOST	((t_ipaddr) 0x7f000001UL)

/* Address used in point-to-point links */
#define	IP_POINTOPOINT	((t_ipaddr) 0xffffffffUL)

/* Multicast addresses */
#define IP_UNSPEC	((t_ipaddr) 0xe0000000UL)
#define IP_ALLHOSTS	((t_ipaddr) 0xe0000001UL)
#define IP_ALLRTRS	((t_ipaddr) 0xe0000002UL)
#define IP_MAX_LOCAL	((t_ipaddr) 0xe00000ffUL)


/* Netmask for class A network */
#define IN_CLASS_A(a)	(((a) & 0x80000000UL) == 0UL)
#define IP_CLASS_A	((t_ipaddr) 0xff000000UL)

/* Netmask for class B network */
#define IN_CLASS_B(a)	(((a) & 0xc0000000UL) == 0x80000000UL)
#define IP_CLASS_B	((t_ipaddr) 0xffff0000UL)

/* Netmask for class C network */
#define IN_CLASS_C(a)	(((a) & 0xe0000000UL) == 0xc0000000UL)
#define IP_CLASS_C	((t_ipaddr) 0xffffff00UL)

/* Netmask for class D network */
#define IN_CLASS_D(a)	(((a) & 0xf0000000UL) == 0xe0000000UL)
#define IN_MULTICAST(a)	IN_CLASS_D(a)

#else

/* For assembly code we specify a mask and value for the first IP addr byte */
#define IP_CLASS_A_MASK	0x80
#define IP_CLASS_A_VAL	0x00

#define IP_CLASS_B_MASK	0xC0
#define IP_CLASS_B_VAL	0x80

#define IP_CLASS_C_MASK	0xE0
#define IP_CLASS_C_VAL	0xC0

#define IP_CLASS_D_MASK	0xF0
#define IP_CLASS_D_VAL	0xE0

#endif



/*
 * Routines to convert the host byte order into network byte order:
 */
#ifndef _ASSEMBLY_

#define __constant_swap16(x) \
	((__u16)( \
		(((__u16)(x) & (__u16)0x00ffU) << 8) | \
		(((__u16)(x) & (__u16)0xff00U) >> 8) ))
#define __constant_swap32(x) \
	((__u32)( \
		(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
		(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
		(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
		(((__u32)(x) & (__u32)0xff000000UL) >> 24) ))

#define const_ntohs __constant_swap16
#define const_htons __constant_swap16
#define const_ntohl __constant_swap32
#define const_htonl __constant_swap32


__attribute__((always_inline,const)) static inline __u32 __swap32(__u32 x)
{
  __asm__(
	"xchgb %b0,%h0\n\t"
	"rorl $16,%0\n\t"
	"xchgb %b0,%h0"
	:"=q" (x)
	: "0" (x));
  return(x);
}


__attribute__((always_inline,const)) static inline __u16 __swap16(__u16 x)
{
  __asm__(
	"xchgb %b0,%h0"
	:"=q" (x)
	: "0" (x));
  return(x);
}


#define htons(a)	(unsigned short)(__builtin_constant_p(a) ? \
					__constant_swap16((__u16)(a)) : \
					__swap16((__u16)(a)))

#define htonl(a)	(unsigned long)(__builtin_constant_p(a) ? \
					__constant_swap32((__u32)(a)) : \
					__swap32((__u32)(a)))
#define ntohs(a)	htons(a)
#define ntohl(a)	htonl(a)

#endif

#endif

