initial load of upstream version 1.06.32
[xmlrpc-c] / lib / abyss / src / socket.h
1 #ifndef SOCKET_H_INCLUDED
2 #define SOCKET_H_INCLUDED
3
4 #include <netinet/in.h>
5
6 #include "xmlrpc-c/abyss.h"
7
8 #include <netinet/in.h>
9
10 #define IPB1(x) (((unsigned char *)(&x))[0])
11 #define IPB2(x) (((unsigned char *)(&x))[1])
12 #define IPB3(x) (((unsigned char *)(&x))[2])
13 #define IPB4(x) (((unsigned char *)(&x))[3])
14
15 typedef struct in_addr TIPAddr;
16
17 typedef void SocketDestroyImpl(TSocket * const socketP);
18
19 typedef void SocketWriteImpl(TSocket *             const socketP,
20                              const unsigned char * const buffer,
21                              uint32_t              const len,
22                              abyss_bool *          const failedP);
23
24 typedef uint32_t SocketReadImpl(TSocket * const socketP,
25                                 char *    const buffer,
26                                 uint32_t  const len);
27
28 typedef abyss_bool SocketConnectImpl(TSocket * const socketP,
29                                      TIPAddr * const addrP,
30                                      uint16_t  const portNumber);
31
32 typedef abyss_bool SocketBindImpl(TSocket * const socketP,
33                                   TIPAddr * const addrP,
34                                   uint16_t  const portNumber);
35
36 typedef abyss_bool SocketListenImpl(TSocket * const socketP,
37                                     uint32_t  const backlog);
38
39 typedef void SocketAcceptImpl(TSocket *    const listenSocketP,
40                               abyss_bool * const connectedP,
41                               abyss_bool * const failedP,
42                               TSocket **   const acceptedSocketPP,
43                               TIPAddr *    const ipAddrP);
44
45 typedef uint32_t SocketErrorImpl(TSocket * const socketP);
46
47 typedef uint32_t SocketWaitImpl(TSocket *  const socketP,
48                                 abyss_bool const rd,
49                                 abyss_bool const wr,
50                                 uint32_t   const timems);
51
52 typedef uint32_t SocketAvailableReadBytesImpl(TSocket * const socketP);
53
54 typedef void SocketGetPeerNameImpl(TSocket *    const socketP,
55                                    TIPAddr *    const ipAddrP,
56                                    uint16_t *   const portNumberP,
57                                    abyss_bool * const successP);
58
59 struct TSocketVtbl {
60     SocketDestroyImpl            * destroy;
61     SocketWriteImpl              * write;
62     SocketReadImpl               * read;
63     SocketConnectImpl            * connect;
64     SocketBindImpl               * bind;
65     SocketListenImpl             * listen;
66     SocketAcceptImpl             * accept;
67     SocketErrorImpl              * error;
68     SocketWaitImpl               * wait;
69     SocketAvailableReadBytesImpl * availableReadBytes;
70     SocketGetPeerNameImpl        * getPeerName;
71 };
72
73 struct _TSocket {
74     uint               signature;
75         /* With both background and foreground use of sockets, and
76            background being both fork and pthread, it is very easy to
77            screw up socket lifetime and try to destroy twice.  We use
78            this signature to help catch such bugs.
79         */
80     void *             implP;
81     struct TSocketVtbl vtbl;
82 };
83
84 #define TIME_INFINITE   0xffffffff
85
86 extern abyss_bool SocketTraceIsActive;
87
88 abyss_bool
89 SocketInit(void);
90
91 void
92 SocketTerm(void);
93
94 void
95 SocketCreate(const struct TSocketVtbl * const vtblP,
96              void *                     const implP,
97              TSocket **                 const socketPP);
98
99 void
100 SocketWrite(TSocket *             const socketP,
101             const unsigned char * const buffer,
102             uint32_t              const len,
103             abyss_bool *          const failedP);
104
105 uint32_t
106 SocketRead(TSocket *       const socketP, 
107            unsigned char * const buffer, 
108            uint32_t        const len);
109
110 abyss_bool
111 SocketConnect(TSocket * const socketP,
112               TIPAddr * const addrP,
113               uint16_t  const portNumber);
114
115 abyss_bool
116 SocketBind(TSocket * const socketP,
117            TIPAddr * const addrP,
118            uint16_t  const portNumber);
119
120 abyss_bool
121 SocketListen(TSocket * const socketP,
122              uint32_t  const backlog);
123
124 void
125 SocketAccept(TSocket *    const listenSocketP,
126              abyss_bool * const connectedP,
127              abyss_bool * const failedP,
128              TSocket **   const acceptedSocketP,
129              TIPAddr *    const ipAddrP);
130
131 uint32_t
132 SocketWait(TSocket *  const socketP,
133            abyss_bool const rd,
134            abyss_bool const wr,
135            uint32_t   const timems);
136
137 uint32_t
138 SocketAvailableReadBytes(TSocket * const socketP);
139
140 void
141 SocketGetPeerName(TSocket *    const socketP,
142                   TIPAddr *    const ipAddrP,
143                   uint16_t *   const portNumberP,
144                   abyss_bool * const successP);
145
146 uint32_t
147 SocketError(TSocket * const socketP);
148
149 #endif