socket.h
3.39 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
/**
* \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 © 2012 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;
TR_CLASS(TR_Socket) {
TR_Logger log;
int flags;
int type;
char * host;
char * cname;
int port;
time_t ttl;
struct sockaddr_in addr; // for now... should be more generic.
socklen_t addrlen;
int handle;
TR_SocketFin fin_state;
};
#define TR_socketLog(socket) (((TR_Socket)(socket))->log)
#define TR_socketFlags(socket) (((TR_Socket)(socket))->flags)
#define TR_socketType(socket) (((TR_Socket)(socket))->type)
#define TR_socketHost(socket) (((TR_Socket)(socket))->host)
#define TR_socketPort(socket) (((TR_Socket)(socket))->port)
#define TR_socketCname(socket) (((TR_Socket)(socket))->cname)
#define TR_socketTtl(socket) (((TR_Socket)(socket))->ttl)
#define TR_socketAddr(socket) (((TR_Socket)(socket))->addr)
#define TR_socketAddrlen(socket) (((TR_Socket)(socket))->addrlen)
#define TR_socketHandle(socket) (((TR_Socket)(socket))->handle)
#define TR_socketFinState(socket) (((TR_Socket)(socket))->fin_state)
#define TR_socketFinRd(socket) ((TR_socketFinState((socket)) & 1) == 1)
#define TR_socketFinWr(socket) ((TR_socketFinState((socket)) & 2) == 2)
#define TR_socketFinRdWr(socket) ((TR_socketFinState((socket)) & 3) == 3)
#define TR_socketGetIp(socket) (TR_socketAddr(socket).sin_addr.s_addr)
#define TR_socketGetIpStr(socket) (inet_ntoa(TR_socketGetIp(socket)))
TR_CLASS(TR_TcpSocket) {
TR_EXTENDS(TR_Socket);
int listen;
int connected;
};
TR_CLASS(TR_UdpSocket) {
TR_EXTENDS(TR_Socket);
};
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: