summaryrefslogtreecommitdiffstats
path: root/src/util/sock_addr.h
blob: 1f5407a4be9bcf86b0091fe26775ff8d05ea2a0a (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
#ifndef _SOCK_ADDR_EQ_H_INCLUDED_
#define _SOCK_ADDR_EQ_H_INCLUDED_

/*++
/* NAME
/*	sock_addr 3h
/* SUMMARY
/*	socket address utilities
/* SYNOPSIS
/*	#include <sock_addr.h>
/* DESCRIPTION
/* .nf

 /*
  * System library.
  */
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

 /*
  * External interface.
  */
#define SOCK_ADDR_PTR(ptr)	((struct sockaddr *)(ptr))
#define SOCK_ADDR_FAMILY(ptr)	SOCK_ADDR_PTR(ptr)->sa_family
#ifdef HAS_SA_LEN
#define SOCK_ADDR_LEN(ptr)	SOCK_ADDR_PTR(ptr)->sa_len
#endif

#define SOCK_ADDR_IN_PTR(sa)	((struct sockaddr_in *)(sa))
#define SOCK_ADDR_IN_FAMILY(sa)	SOCK_ADDR_IN_PTR(sa)->sin_family
#define SOCK_ADDR_IN_PORT(sa)	SOCK_ADDR_IN_PTR(sa)->sin_port
#define SOCK_ADDR_IN_ADDR(sa)	SOCK_ADDR_IN_PTR(sa)->sin_addr
#define IN_ADDR(ia)		(*((struct in_addr *) (ia)))

extern int sock_addr_cmp_addr(const struct sockaddr *, const struct sockaddr *);
extern int sock_addr_cmp_port(const struct sockaddr *, const struct sockaddr *);
extern int sock_addr_in_loopback(const struct sockaddr *);

#ifdef HAS_IPV6

#ifndef HAS_SA_LEN
#define SOCK_ADDR_LEN(sa) \
    (SOCK_ADDR_PTR(sa)->sa_family == AF_INET6 ? \
     sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in))
#endif

#define SOCK_ADDR_PORT(sa) \
    (SOCK_ADDR_PTR(sa)->sa_family == AF_INET6 ? \
	SOCK_ADDR_IN6_PORT(sa) : SOCK_ADDR_IN_PORT(sa))
#define SOCK_ADDR_PORTP(sa) \
    (SOCK_ADDR_PTR(sa)->sa_family == AF_INET6 ? \
	&SOCK_ADDR_IN6_PORT(sa) : &SOCK_ADDR_IN_PORT(sa))

#define SOCK_ADDR_IN6_PTR(sa)	((struct sockaddr_in6 *)(sa))
#define SOCK_ADDR_IN6_FAMILY(sa) SOCK_ADDR_IN6_PTR(sa)->sin6_family
#define SOCK_ADDR_IN6_PORT(sa)	SOCK_ADDR_IN6_PTR(sa)->sin6_port
#define SOCK_ADDR_IN6_ADDR(sa)	SOCK_ADDR_IN6_PTR(sa)->sin6_addr
#define IN6_ADDR(ia)		(*((struct in6_addr *) (ia)))

#define SOCK_ADDR_EQ_ADDR(sa, sb) \
    ((SOCK_ADDR_FAMILY(sa) == AF_INET && SOCK_ADDR_FAMILY(sb) == AF_INET \
      && SOCK_ADDR_IN_ADDR(sa).s_addr == SOCK_ADDR_IN_ADDR(sb).s_addr) \
     || (SOCK_ADDR_FAMILY(sa) == AF_INET6 && SOCK_ADDR_FAMILY(sb) == AF_INET6 \
         && memcmp((char *) &(SOCK_ADDR_IN6_ADDR(sa)), \
                   (char *) &(SOCK_ADDR_IN6_ADDR(sb)), \
                   sizeof(SOCK_ADDR_IN6_ADDR(sa))) == 0))

#define SOCK_ADDR_EQ_PORT(sa, sb) \
    ((SOCK_ADDR_FAMILY(sa) == AF_INET && SOCK_ADDR_FAMILY(sb) == AF_INET \
      && SOCK_ADDR_IN_PORT(sa) == SOCK_ADDR_IN_PORT(sb)) \
     || (SOCK_ADDR_FAMILY(sa) == AF_INET6 && SOCK_ADDR_FAMILY(sb) == AF_INET6 \
         && SOCK_ADDR_IN6_PORT(sa) == SOCK_ADDR_IN6_PORT(sb)))

#else

#ifndef HAS_SA_LEN
#define SOCK_ADDR_LEN(sa)	sizeof(struct sockaddr_in)
#endif

#define SOCK_ADDR_PORT(sa)	SOCK_ADDR_IN_PORT(sa))
#define SOCK_ADDR_PORTP(sa)	&SOCK_ADDR_IN_PORT(sa))

#define SOCK_ADDR_EQ_ADDR(sa, sb) \
    (SOCK_ADDR_FAMILY(sa) == AF_INET && SOCK_ADDR_FAMILY(sb) == AF_INET \
     && SOCK_ADDR_IN_ADDR(sa).s_addr == SOCK_ADDR_IN_ADDR(sb).s_addr)

#define SOCK_ADDR_EQ_PORT(sa, sb) \
    (SOCK_ADDR_FAMILY(sa) == AF_INET && SOCK_ADDR_FAMILY(sb) == AF_INET \
     && SOCK_ADDR_IN_PORT(sa) == SOCK_ADDR_IN_PORT(sb))

#endif

/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

#endif