summaryrefslogtreecommitdiff
path: root/comm/Address.cc
blob: 779ae0805249e939c4bc0548d2001bec7bc12b70 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 *	Ivy League
 *
 *	Socket addresses
 *
 *	Copyright 1990-2000
 *	Laboratoire de Recherche en Informatique (LRI)
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	original code by Michel Beaudouin-Lafon,
 *	modified by Stephane Chatty and Stephane Sire
 *
 *	$Id$
 *
 */

#include "Address.h"
#include "error.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <memory.h>

/* all this is necessary for function Decode only. This should be investigated */
#include "UnixAddress.h"
#include "InetAddress.h"
#include "IrdaAddress.h"




/*?class IvlAddress
This class is a virtual base class: no objects of this class are ever created.
It implements the Unix type \typ{^{struct sockaddr}}, used for creating sockets.
Addresses are used mainly as arguments of constructors for the class
\typ{IvlSocket} and its derived classes.
All its member functions are virtual.
These functions exist for all the derived classes but are only described here.
An address can be valid or invalid, depending on the constructor being able to create the address or not.
There are currently two derived classes, for Unix domain addresses and Internet domain addresses.

Addresses are "ONE WAY" instances, once created they cannot be modified.
This implies that instead of beeing changed, new ones are created with
static member function \fun{Decode} for example when a socket is bound.
?*/

/*?
Construct an invalid address.
?*/
IvlAddress :: IvlAddress ()
: Valid (false)
{
}

/*?nodoc?*/
IvlAddress :: ~IvlAddress ()
{
}

#ifdef DOC
// fake entries for inline functions

/*?
Return true if the address is valid.
?*/
bool 
IvlAddress :: IsValid ()
{
}

/*?
Return one of \var{AF\_UNSPEC}, \var{AF\_UNIX} or \var{AF\_INET}.
Other values may be defined if supported by the system.
?*/
int
IvlAddress :: Family ()
{
}

/*?nextdoc?*/
int
IvlAddress :: Length ()
{
}

/*?
Return the address structure and its length.
\typ{^{SockAddr}} is a typedef for \typ{^{struct sockaddr}}.
?*/
SockAddr*
IvlAddress :: GetSockAddr ()
{
}

/*?nodoc?*/
char*
IvlAddress :: StrRepr (char* buf)
{
}
#endif	/* DOC */



/*?
This is a global function (static member of class \typ{IvlAddress}).
It creates an object of a derived class of \typ{IvlAddress} from a generic address
(thus is cannot be replaced by a constructor).
A generic address is the following union of address structures
(it is typically returned by system calls like \fun{recvfrom}):
\begin{ccode}
typedef union {
    struct sockaddr sa;        // default
    struct sockaddr_un su;     // Unix
    struct sockaddr_irda sir;     // irda
    struct sockaddr_in si;     // inet
} GEN_ADDR;
\end{ccode}
?*/
IvlAddress*
IvlAddress :: Decode (GEN_ADDR* addr, int alen)
{
	switch (addr->sa.sa_family) {
#ifdef UNIX_SOCK
		case AF_UNIX :
			addr->su.sun_path [alen] = 0;
			return new IvlUnixAddress (addr->su.sun_path);
#endif
#ifdef IRDA_SOCK
		case AF_IRDA :
			return new IvlIrdaAddress (addr->sir.sir_addr,
						addr->sir.sir_name);
#endif
		case AF_INET :
			return new IvlInetAddress (ntohl (addr->si.sin_addr.s_addr),
						ntohs (addr->si.sin_port));
		
		default :
			return 0;
	}
}