socket.h
4.47 KB
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
/**
* \file
* Abstraction layer above BSD sockets. Capsules and simplifies connect
* accept and listen for TCP and UDP sockets.
* It also provides a mostly unified interface to both types of sockets.
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TR_SOCKET_H__
#define __TR_SOCKET_H__
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
//#include <arpa/inet.h> // for in_port_t
#include "trbase.h"
//#include "tr/interface/socket.h"
#include "tr/logger.h"
typedef enum TR_e_socket_fin {
TR_FIN_NO = 0,
TR_FIN_RD = 1,
TR_FIN_WR = 2,
TR_FIN_RDWR = 3
} TR_SocketFin;
#define TR_MAX_HOST 256
#define TR_MAX_CNAME 512
TR_CLASS(TR_Socket) {
TR_Logger log;
int flags;
int type;
char host[TR_MAX_HOST];
char cname[TR_MAX_CNAME];
int port;
time_t ttl;
union {
struct sockaddr info;
struct sockaddr_in in;
struct sockaddr_in6 in6;
} addr;
socklen_t addrlen;
int handle;
TR_SocketFin fin_state;
};
TR_INSTANCE_INIT(TR_Socket);
TR_CLASSVARS_DECL(TR_Socket) {};
#define TR_socketLog(socket) ((socket)->log)
#define TR_socketFlags(socket) ((socket)->flags)
#define TR_socketType(socket) ((socket)->type)
#define TR_socketHost(socket) ((socket)->host)
#define TR_socketPort(socket) ((socket)->port)
#define TR_socketCname(socket) ((socket)->cname)
#define TR_socketTtl(socket) ((socket)->ttl)
#define TR_socketAddr(socket) ((void *)&(socket)->addr)
#define TR_socketAddrlen(socket) ((socket)->addrlen)
#define TR_socketHandle(socket) ((socket)->handle)
#define TR_socketFinState(socket) ((socket)->fin_state)
#define TR_socketFinRd(socket) \
((TR_socketFinState(socket) & TR_FIN_RD) == TR_FIN_RD)
#define TR_socketFinWr(socket) \
((TR_socketFinState(socket) & TR_FIN_WR) == TR_FIN_WR)
#define TR_socketFinRdWr(socket) \
((TR_socketFinState(socket) & TR_FIN_RDWR) == TR_FIN_RDWR)
#define TR_socketAddrPort(socket) \
((socket)->addr.info.sa_family == AF_INET \
? (socket)->addr.in.sin_port \
: (socket)->addr.info.sa_family == AF_INET6 \
? (socket)->addr.in6.sin6_port \
: 0)
#define TR_socketAddrIp(socket) \
((socket)->addr.info.sa_family == AF_INET \
? (void *)&(socket)->addr.in.sin_addr.s_addr \
: (socket)->addr.info.sa_family == AF_INET6 \
? (void *)&(socket)->addr.in6.sin6_addr.s6_addr \
: NULL)
#define TR_socketAddrIplen(socket) \
((socket)->addr.info.sa_family == AF_INET \
? sizeof((socket)->addr.in.sin_addr) \
: (socket)->addr.info.sa_family == AF_INET6 \
? sizeof((socket)->addr.in6.sin6_addr) \
: 0)
#define TR_socketAddrIpStr(socket, buffer, nbuffer) \
if (TR_socketAddrIp((socket))) \
inet_ntop( \
(socket)->addr.info.sa_family, \
TR_socketAddrIp((socket)), \
buffer, nbuffer)
TR_CLASS(TR_TcpSocket) {
TR_EXTENDS(TR_Socket);
int listen;
int connected;
};
TR_INSTANCE_INIT(TR_TcpSocket);
TR_CLASSVARS_DECL(TR_TcpSocket) {};
TR_CLASS(TR_UdpSocket) {
TR_EXTENDS(TR_Socket);
};
TR_INSTANCE_INIT(TR_UdpSocket);
TR_CLASSVARS_DECL(TR_UdpSocket) {};
typedef int (* TR_socketAction_fptr)(void *);
int TR_socketInit(TR_Socket, TR_socketAction_fptr);
TR_SocketFin TR_socketShutdownRead(TR_Socket);
TR_SocketFin TR_socketShutdownWrite(TR_Socket);
TR_SocketFin TR_socketShutdown(TR_Socket);
void TR_socketClose(TR_Socket);
void TR_socketNonblock(TR_Socket);
#define TR_socketBind(socket) \
(TR_socketInit((socket), TR_socketBindAction))
TR_TcpSocket TR_socketAccept(TR_TcpSocket);
#define TR_socketConnect(socket) \
(TR_socketInit((socket), TR_socketConnectAction))
#endif // __TR_SOCKET_H__
// vim: set ts=4 sw=4: