summaryrefslogtreecommitdiff
path: root/comm/Address.h
blob: d0e083de3c0f69b0a90ffbc94d2aeab938c2939b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1997
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Addresses
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef Address_H_
#define Address_H_

#include "cplus_bugs.h"
#include "ccu/bool.h"
#include "ccu/word.h"
#include "ccu/String.h"

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

#ifdef UNIX_SOCK
#include <sys/un.h>
#endif

#ifndef AF_UNIX
#define AF_UNIX	AF_UNSPEC
#endif

union gen_addr {
	struct sockaddr		sa;		// default
#ifdef UNIX_SOCK
	struct sockaddr_un	su;		// unix
#endif
	struct sockaddr_in	si;		// inet
};

typedef struct sockaddr SockAddr;
/*	this union is useful for adresses returned by system calls */
typedef union gen_addr GEN_ADDR;

class UchAddress {
protected:
	bool	Valid;

public:
		UchAddress ();
virtual		~UchAddress ();
	
	bool		IsValid () const		{ return Valid; }
virtual	int		Family () = 0;		// AF_UNSPEC, AF_UNIX, AF_INET
virtual	int		Length () = 0;
virtual	SockAddr*	GetSockAddr () = 0;
virtual	char*		StrRepr (char* buf) = 0;

static	UchAddress*	Decode (GEN_ADDR*, int);
};

class UchInetAddress : public UchAddress {
protected:
	CcuString		HostName;
	bool		HasHostName;
	struct sockaddr_in	Addr;

	const char*     GetNoHostName ();

public:
		UchInetAddress ();
		UchInetAddress (lword, sword);		// hostid, port#
		UchInetAddress (const char*, sword = 0);	// hostname, port#
		~UchInetAddress ();
	
	int		Family ();
	int		Length ();
	SockAddr*	GetSockAddr ();
inline	sword		Port ()		{ return Addr.sin_port; }
inline	lword		Host ()		{ return Addr.sin_addr.s_addr; }
	char*		StrRepr (char* buf = 0);
	const char*	GetHostName ();
	
static	lword		LoopBack ();
};

#ifndef INADDR_LOOPBACK
#define INADDR_LOOPBACK	(INET_ADDR::LoopBack ())
#endif

#define ANYADDR	((lword) INADDR_ANY)
#define LOOPBACK	((lword) INADDR_LOOPBACK)
#define BROADCAST	((lword) INADDR_BROADCAST)


#ifdef UNIX_SOCK

class UchUnixAddress : public UchAddress {
protected:
	struct sockaddr_un	Addr;

public:
		UchUnixAddress ();
		UchUnixAddress (const char*);
		~UchUnixAddress ();
	
	int		Family ();
	int		Length ();
	SockAddr*	GetSockAddr ();
	char*		StrRepr (char* buf);
};

#endif	/* UNIX_ADDR */


#endif /* Address_H_ */