1 /*
2  * (c) Copyright 1992 by Panagiotis Tsirigotis
3  * (c) Sections Copyright 1998-2001 by Rob Braun
4  * All rights reserved.  The file named COPYRIGHT specifies the terms
5  * and conditions for redistribution.
6  */
7 
8 #ifndef CONNECTION_H
9 #define CONNECTION_H
10 
11 /*
12  * $Id: connection.h,v 1.4 2003-06-23 13:46:37 steveg Exp $
13  */
14 
15 #include "config.h"
16 #include <sys/types.h>
17 #include <netinet/in.h>
18 #if defined( HAVE_ARPA_INET_H )
19 #include <arpa/inet.h>
20 #endif
21 #include <string.h>
22 
23 #include "mask.h"
24 #include "service.h"
25 #include "defs.h"
26 #include "msg.h"
27 #include "sio.h"
28 
29 #ifndef IN6_IS_ADDR_V4MAPPED
30 #define IN6_IS_ADDR_V4MAPPED(a) \
31    ((((uint32_t *) (a))[0] == 0) && (((uint32_t *) (a))[1] == 0) && \
32    (((uint32_t *) (a))[2] == htonl (0xffff)))
33 #endif
34 #ifndef IN6_IS_ADDR_V4COMPAT
35 #define IN6_IS_ADDR_V4COMPAT(a) \
36    ((((uint32_t *) (a))[0] == 0) && (((uint32_t *) (a))[1] == 0) && \
37    (((uint32_t *) (a))[2] == 0) && (ntohl (((uint32_t *) (a))[3]) > 1))
38 #endif
39 
40 #define MAX_ALTERNATIVES            3
41 
42 /* Connection flags */
43 #define COF_HAVE_ADDRESS            1
44 #define COF_NEW_DESCRIPTOR          2
45 
46 struct connection
47 {
48    struct service        *co_sp ;
49    int                   co_descriptor ;
50    mask_t                co_flags ;
51    union xsockaddr       co_remote_address ;
52 } ;
53 
54 #define CONN_CLOSE( cp ) { Sclose( (cp)->co_descriptor ); (cp)->co_descriptor = -1; }
55 
56 #define COP( p )       ((connection_s *)(p))
57 #define CONN_NULL      COP( NULL )
58 
59 /*
60  * Field access macros
61  */
62 #define CONN_DESCRIPTOR( cp )       (cp)->co_descriptor
63 #define CONN_SERVICE( cp )          (cp)->co_sp
64 #define CONN_SET_FLAG( cp, flag )   M_SET( (cp)->co_flags, flag )
65 #define CONN_SET_DESCRIPTOR( cp, fd )   (cp)->co_descriptor = (fd)
66 
67 #define CONN_SETADDR( cp, sinp )               \
68    {                        \
69       CONN_SET_FLAG( cp, COF_HAVE_ADDRESS ) ;         \
70       memcpy(((cp)->co_remote_address.pad), sinp, sizeof(*sinp) ); \
71    }
72 
73 #define CONN_ADDRESS( cp )                     \
74    (                           \
75       M_IS_SET( (cp)->co_flags, COF_HAVE_ADDRESS )         \
76          ? &((cp)->co_remote_address.sa)    \
77          : SA(NULL)               \
78    )
79 #define CONN_XADDRESS( cp )                     \
80    (                           \
81       M_IS_SET( (cp)->co_flags, COF_HAVE_ADDRESS )         \
82          ? &((cp)->co_remote_address)    \
83          : NULL               \
84    )
85 
86 connection_s *conn_new(struct service *sp);
87 void conn_free(connection_s *cp, int);
88 void conn_dump(const connection_s *cp,int fd);
89 const char *conn_addrstr( const connection_s *cp );
90 
91 #endif   /* CONNECTION_H */
92 
93