/*
 * RISC OS system types for the driver, made to work with both current
 * TCPIPLibs and releases from before 2002.
 *
 * Old TCPIPLibs typedef int32_t, int16_t and a struct int64_t in
 * machine/types.h and sys/types.h, which clash with <stdint.h>, and have
 * no socklen_t, struct sockaddr_storage or RFC 3493 resolver API.  Pull in
 * <stdint.h> and the TCPIPLibs type headers once here with those names
 * mapped aside (later includes are skipped by the headers' own guards),
 * then supply whatever is missing.
 */

#ifndef RISCOSTYPES_H
#define RISCOSTYPES_H

/*
 * The !ODBC manager does not export a resolver, so the driver carries an
 * IPv4 one built on gethostbyname() (c.netdb).  The renames keep it private
 * to the driver segment, and must be in place before <netdb.h>.
 */
#define getaddrinfo pg_riscos_getaddrinfo
#define freeaddrinfo pg_riscos_freeaddrinfo
#define getnameinfo pg_riscos_getnameinfo
#define gai_strerror pg_riscos_gai_strerror

#include <stddef.h>
#include <stdint.h>

#ifndef __CC_NORCROFT
/* GCCSDK (UnixLib) headers, as used by the test harness */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#else

#define int64_t tcpip_int64_t
#define int32_t tcpip_int32_t
#define int16_t tcpip_int16_t
#include <sys/types.h>
#undef int64_t
#undef int32_t
#undef int16_t

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

/* old socklib.h names this in a prototype; give it file scope */
struct osockaddr_in;

#ifndef _SOCKLEN_T_DECLARED
#define _SOCKLEN_T_DECLARED
typedef int socklen_t;
#endif

#ifndef _SS_MAXSIZE
#define _SS_MAXSIZE 128
struct sockaddr_storage
{
	u_char		ss_len;
	u_char		ss_family;
	char		__ss_pad1[6];
	long long	__ss_align;
	char		__ss_pad2[_SS_MAXSIZE - 16];
};
#endif

#ifndef INADDR_LOOPBACK
#define INADDR_LOOPBACK ((u_long) 0x7f000001)
#endif

#ifndef EAI_NONAME
struct addrinfo
{
	int			ai_flags;
	int			ai_family;
	int			ai_socktype;
	int			ai_protocol;
	size_t		ai_addrlen;
	char	   *ai_canonname;
	struct sockaddr *ai_addr;
	struct addrinfo *ai_next;
};

#define EAI_ADDRFAMILY	1
#define EAI_AGAIN		2
#define EAI_BADFLAGS	3
#define EAI_FAIL		4
#define EAI_FAMILY		5
#define EAI_MEMORY		6
#define EAI_NODATA		7
#define EAI_NONAME		8
#define EAI_SERVICE		9
#define EAI_SOCKTYPE	10
#define EAI_SYSTEM		11

#define AI_PASSIVE		0x00000001
#define AI_CANONNAME	0x00000002
#define AI_NUMERICHOST	0x00000004
#define AI_NUMERICSERV	0x00000008

#define NI_MAXHOST		1025
#define NI_MAXSERV		32
#define NI_NOFQDN		0x00000001
#define NI_NUMERICHOST	0x00000002
#define NI_NAMEREQD		0x00000004
#define NI_NUMERICSERV	0x00000008
#define NI_DGRAM		0x00000010

extern int	getaddrinfo(const char *hostname, const char *servname,
						const struct addrinfo *hints, struct addrinfo **res);
extern void freeaddrinfo(struct addrinfo *ai);
extern int	getnameinfo(const struct sockaddr *sa, socklen_t salen,
						char *host, size_t hostlen,
						char *serv, size_t servlen, int flags);
extern const char *gai_strerror(int ecode);
#endif

#endif							/* __CC_NORCROFT */

#endif
