File: SysDepCommon.cpp

package info (click to toggle)
xmail 1.25-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,592 kB
  • ctags: 16,393
  • sloc: cpp: 33,249; ansic: 19,434; sh: 476; perl: 200; makefile: 107; sql: 31
file content (366 lines) | stat: -rw-r--r-- 9,249 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 *  XMail by Davide Libenzi ( Intranet and Internet mail server )
 *  Copyright (C) 1999,..,2004  Davide Libenzi
 *
 *  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
 *  (at your option) 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Davide Libenzi <davidel@xmailserver.org>
 *
 */

#include "SysInclude.h"
#include "SysDep.h"
#include "AppDefines.h"

#define SYS_ADDR_FAMILY(a)	((struct sockaddr *) (a)->Addr)->sa_family
#define SYS_IN4(a)		((struct sockaddr_in *) (a)->Addr)
#define SYS_IN6(a)		((struct sockaddr_in6 *) (a)->Addr)


static int SysMapGetAddrInfoError(int iError);


int SysInetAnySetup(SYS_INET_ADDR &AddrInfo, int iFamily, int iPortNo)
{
	switch (iFamily) {
	case AF_INET:
		ZeroData(AddrInfo);
		SYS_IN4(&AddrInfo)->sin_family = iFamily;
		SYS_IN4(&AddrInfo)->sin_port = htons(iPortNo);
		SYS_IN4(&AddrInfo)->sin_addr.s_addr = INADDR_ANY;
		AddrInfo.iSize = sizeof(struct sockaddr_in);
		break;

	case AF_INET6:
		ZeroData(AddrInfo);
		SYS_IN6(&AddrInfo)->sin6_family = iFamily;
		SYS_IN6(&AddrInfo)->sin6_port = htons(iPortNo);
		SYS_IN6(&AddrInfo)->sin6_addr = in6addr_any;
		AddrInfo.iSize = sizeof(struct sockaddr_in6);
		break;

	default:
		ErrSetErrorCode(ERR_INVALID_INET_ADDR);
		return ERR_INVALID_INET_ADDR;
	}

	return 0;
}

int SysGetAddrFamily(SYS_INET_ADDR const &AddrInfo)
{
	return SYS_ADDR_FAMILY(&AddrInfo);
}

int SysGetAddrPort(SYS_INET_ADDR const &AddrInfo)
{
	switch (SysGetAddrFamily(AddrInfo)) {
	case AF_INET:
		return ntohs(SYS_IN4(&AddrInfo)->sin_port);

	case AF_INET6:
		return ntohs(SYS_IN6(&AddrInfo)->sin6_port);
	}

	ErrSetErrorCode(ERR_INVALID_INET_ADDR);
	return ERR_INVALID_INET_ADDR;
}

int SysSetAddrPort(SYS_INET_ADDR &AddrInfo, int iPortNo)
{
	switch (SysGetAddrFamily(AddrInfo)) {
	case AF_INET:
		SYS_IN4(&AddrInfo)->sin_port = htons((short) iPortNo);
		break;

	case AF_INET6:
		SYS_IN6(&AddrInfo)->sin6_port = htons((short) iPortNo);
		break;

	default:
		ErrSetErrorCode(ERR_INVALID_INET_ADDR);
		return ERR_INVALID_INET_ADDR;
	}

	return 0;
}

static int SysMapGetAddrInfoError(int iError)
{
	switch (iError) {
	case EAI_NONAME:
		return ERR_BAD_SERVER_ADDR;

#if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
	case EAI_NODATA:
		return ERR_DNS_NOTFOUND;
#endif

	case EAI_MEMORY:
		return ERR_MEMORY;

	case EAI_BADFLAGS:
	case EAI_FAMILY:
		return ERR_INVALID_PARAMETER;
	}

	return ERR_NETWORK;
}

int SysGetHostByName(char const *pszName, int iFamily, SYS_INET_ADDR &AddrInfo)
{
	int iError;
	struct addrinfo *pCRes, *pRes, *pRes4 = NULL, *pRes6 = NULL;
	struct addrinfo AHints;

	ZeroData(AHints);
	AHints.ai_family = AF_UNSPEC;
	if ((iError = getaddrinfo(pszName, NULL, &AHints, &pRes)) != 0) {
		iError = SysMapGetAddrInfoError(iError);
		ErrSetErrorCode(iError, pszName);
		return iError;
	}
	for (pCRes = pRes, iError = ERR_BAD_SERVER_ADDR; pCRes != NULL;
	     pCRes = pCRes->ai_next) {
		if (pCRes->ai_addr->sa_family == AF_INET)
			pRes4 = pCRes;
		else if (pCRes->ai_addr->sa_family == AF_INET6)
			pRes6 = pCRes;
	}
	switch (iFamily) {
	case AF_INET:
		pCRes = pRes4;
		break;
	case AF_INET6:
		pCRes = pRes6;
		break;
	case SYS_INET46:
		if ((pCRes = pRes4) == NULL)
			pCRes = pRes6;
		break;
	case SYS_INET64:
		if ((pCRes = pRes6) == NULL)
			pCRes = pRes4;
		break;
	default:
		pCRes = NULL;
	}
	if (pCRes != NULL && sizeof(AddrInfo.Addr) >= pCRes->ai_addrlen) {
		ZeroData(AddrInfo);
		AddrInfo.iSize = pCRes->ai_addrlen;
		memcpy(AddrInfo.Addr, pCRes->ai_addr, pCRes->ai_addrlen);
	}
	freeaddrinfo(pRes);
	if (pCRes == NULL) {
		ErrSetErrorCode(ERR_BAD_SERVER_ADDR, pszName);
		return ERR_BAD_SERVER_ADDR;
	}

	return 0;
}

int SysGetHostByAddr(SYS_INET_ADDR const &AddrInfo, char *pszFQDN, int iSize)
{
	int iError;

	if ((iError = getnameinfo((struct sockaddr const *) AddrInfo.Addr, AddrInfo.iSize,
				  pszFQDN, iSize, NULL, 0, NI_NAMEREQD)) != 0) {
		char szIP[128];

		ErrSetErrorCode(ERR_GET_SOCK_HOST, SysInetNToA(AddrInfo, szIP, sizeof(szIP)));
		return ERR_GET_SOCK_HOST;
	}

	return 0;
}

int SysGetPeerInfo(SYS_SOCKET SockFD, SYS_INET_ADDR &AddrInfo)
{
	socklen_t InfoSize = sizeof(AddrInfo.Addr);

	ZeroData(AddrInfo);
	if (getpeername(SockFD, (struct sockaddr *) AddrInfo.Addr, &InfoSize) == -1) {
		ErrSetErrorCode(ERR_GET_PEER_INFO);
		return ERR_GET_PEER_INFO;
	}
	AddrInfo.iSize = (int) InfoSize;

	return 0;
}

int SysGetSockInfo(SYS_SOCKET SockFD, SYS_INET_ADDR &AddrInfo)
{
	socklen_t InfoSize = sizeof(AddrInfo.Addr);

	ZeroData(AddrInfo);
	if (getsockname(SockFD, (struct sockaddr *) AddrInfo.Addr, &InfoSize) == -1) {
		ErrSetErrorCode(ERR_GET_SOCK_INFO);
		return ERR_GET_SOCK_INFO;
	}
	AddrInfo.iSize = (int) InfoSize;

	return 0;
}

char *SysInetNToA(SYS_INET_ADDR const &AddrInfo, char *pszIP, int iSize)
{

	SetEmptyString(pszIP);
	if (getnameinfo((struct sockaddr const *) AddrInfo.Addr, AddrInfo.iSize,
			pszIP, iSize, NULL, 0, NI_NUMERICHOST) != 0)
		ErrSetErrorCode(ERR_INVALID_INET_ADDR);

	return pszIP;
}

char *SysInetRevNToA(SYS_INET_ADDR const &AddrInfo, char *pszRevIP, int iSize)
{
	int i;
	char *pszCur;
	SYS_UINT8 const *pAddr;

	SetEmptyString(pszRevIP);
	switch (SysGetAddrFamily(AddrInfo)) {
	case AF_INET:
		pAddr = (SYS_UINT8 const *) &SYS_IN4(&AddrInfo)->sin_addr;
		SysSNPrintf(pszRevIP, iSize, "%u.%u.%u.%u.",
			    pAddr[3], pAddr[2], pAddr[1], pAddr[0]);
		break;

	case AF_INET6:
		pAddr = (SYS_UINT8 const *) &SYS_IN6(&AddrInfo)->sin6_addr;
		for (i = 15, pszCur = pszRevIP; i >= 0 && iSize > 4;
		     i--, pszCur += 4, iSize -= 4)
			SysSNPrintf(pszCur, iSize, "%x.%x.", pAddr[i] & 0xf,
				    pAddr[i] >> 4);
		break;

	default:
		ErrSetErrorCode(ERR_INVALID_INET_ADDR);
	}

	return pszRevIP;
}

void const *SysInetAddrData(SYS_INET_ADDR const &AddrInfo, int *piSize)
{
	switch (SysGetAddrFamily(AddrInfo)) {
	case AF_INET:
		*piSize = (int) sizeof(SYS_IN4(&AddrInfo)->sin_addr);
		return &SYS_IN4(&AddrInfo)->sin_addr;

	case AF_INET6:
		*piSize = (int) sizeof(SYS_IN6(&AddrInfo)->sin6_addr);
		return &SYS_IN6(&AddrInfo)->sin6_addr;
	}

	ErrSetErrorCode(ERR_INVALID_INET_ADDR);
	return NULL;
}

int SysSameAddress(SYS_INET_ADDR const &NetAddr1, SYS_INET_ADDR const &NetAddr2)
{
	return NetAddr1.iSize == NetAddr2.iSize &&
		memcmp(NetAddr1.Addr, NetAddr2.Addr, NetAddr1.iSize) == 0;
}

int SysInetIPV6CompatIPV4(SYS_INET_ADDR const &Addr)
{
	int i, iASize;
	SYS_UINT8 const *pAData;

	if (SysGetAddrFamily(Addr) != AF_INET6 ||
	    (pAData = (SYS_UINT8 const *) SysInetAddrData(Addr, &iASize)) == NULL)
		return 0;
	/*
	 * First 80 bit must be zero ...
	 */
	for (i = 0; i < 10; i++)
		if (pAData[i])
			return 0;
	/*
	 * Then two 0xff must follow, or two 0x00
	 */
	return (pAData[i] == 0xff && pAData[i + 1] == 0xff) ||
		(pAData[i] == 0 && pAData[i + 1] == 0);
}

int SysInetIPV6ToIPV4(SYS_INET_ADDR const &SAddr, SYS_INET_ADDR &DAddr)
{
	if (!SysInetIPV6CompatIPV4(SAddr)) {
		ErrSetErrorCode(ERR_INVALID_INET_ADDR);
		return ERR_INVALID_INET_ADDR;
	}
	ZeroData(DAddr);
	SYS_IN4(&DAddr)->sin_family = AF_INET;
	SYS_IN4(&DAddr)->sin_port = SYS_IN6(&SAddr)->sin6_port;
	memcpy(&SYS_IN4(&DAddr)->sin_addr.s_addr,
	       (char const *) &SYS_IN6(&SAddr)->sin6_addr + 12, 4);
	DAddr.iSize = sizeof(struct sockaddr_in);

	return 0;
}

int SysInetAddrMatch(SYS_INET_ADDR const &Addr, SYS_UINT8 const *pMask, int iMaskSize,
		     SYS_INET_ADDR const &TestAddr)
{
	int i, iASize, iTASize, iAFamily, iTAFamily;
	SYS_UINT8 const *pAData, *pTAData;

	if ((pAData = (SYS_UINT8 const *) SysInetAddrData(Addr, &iASize)) == NULL ||
	    (pTAData = (SYS_UINT8 const *) SysInetAddrData(TestAddr, &iTASize)) == NULL ||
	    iMaskSize < iASize || iMaskSize < iTASize)
		return 0;
	iAFamily = SysGetAddrFamily(Addr);
	iTAFamily = SysGetAddrFamily(TestAddr);
	if (iAFamily != iTAFamily) {
		/*
		 * Need some marshaling here, since families are different.
		 * We only support IPV4 and IPV6, so it does not look that bad.
		 */
		if (iAFamily == AF_INET) {
			if (iTAFamily == AF_INET6) {
				if (!SysInetIPV6CompatIPV4(TestAddr))
					return 0;
				pTAData += iTASize - iASize;
			} else
				return 0;
		} else if (iAFamily == AF_INET6) {
			if (iTAFamily == AF_INET) {
				if (!SysInetIPV6CompatIPV4(Addr))
					return 0;
				pAData += iASize - iTASize;
				pMask += iASize - iTASize;
				iASize = iTASize;
			} else
				return 0;
		} else
			return 0;
	}
	for (i = 0; i < iASize; i++)
		if ((pAData[i] & pMask[i]) != (pTAData[i] & pMask[i]))
			return 0;

	return 1;
}

int SysInetAddrMatch(SYS_INET_ADDR const &Addr, SYS_INET_ADDR const &TestAddr)
{
	SYS_UINT8 Mask[sizeof(SYS_INET_ADDR)];

	memset(Mask, 0xff, sizeof(Mask));
	return SysInetAddrMatch(Addr, Mask, sizeof(Mask), TestAddr);
}