-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.cc
146 lines (117 loc) · 2.93 KB
/
socket.cc
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
#include "socket.hh"
#include <iostream>
Socket::Socket() {
init();
}
Socket::Socket(int af, string connectto, int destport) {
init();
setsin(af, connectto, destport);
}
/* Setter methods */
void Socket::init() {
name = hostname();
setip();
setfd();
}
void Socket::setsin(int af, string connectto, int destport) {
sin.sin_family = af;
sin.sin_port = htons(destport);
inet_pton(af,connectto.c_str(),&sin.sin_addr.s_addr);
}
void Socket::setip() {
ip = ipofhost(name);
}
int Socket::setfd(int fdesc) {
fd = fdesc;
return fd;
}
int Socket::setfd() {
return setfd(socket(sin.sin_family, SOCK_STREAM, 0));
}
/* Accessor methods */
string Socket::getsrcname() const {
return string(name);
}
int Socket::getsinport() const {
return sin.sin_port;
}
string Socket::getsrcip() const {
return string(ip);
}
string Socket::getsinip() const {
int family = sin.sin_family;
char ip[family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN];
return string(inet_ntop(family,
&sin.sin_addr.s_addr,
ip, sizeof(ip)));
}
int Socket::getfd() const {
return fd;
}
struct sockaddr* Socket::getsin() const {
return (struct sockaddr *) &sin;
}
pair<Socket, Socket> SockSet::make_pair(Socket sock, bool r, bool w) {
pair<Socket, Socket> sockpair;
if(r)
sockpair.first = sock;
if(w)
sockpair.second = sock;
return sockpair;
}
void SockSet::sockset_zero() {
sset.clear();
FD_ZERO(&rfdset);
FD_ZERO(&wfdset);
}
void SockSet::sockset_addreadable(Socket sock) {
FD_SET(sock.getfd(), &rfdset);
sset[sock.getfd()] = make_pair(sock, true, false);
maxfd = (maxfd < sock.getfd() ?
sock.getfd() :
maxfd);
}
void SockSet::sockset_addwriteable(Socket sock) {
FD_SET(sock.getfd(), &wfdset);
sset[sock.getfd()] = make_pair(sock, false, true);
maxfd = (maxfd < sock.getfd() ?
sock.getfd() :
maxfd);
}
void SockSet::sockset_add(Socket sock) {
sockset_addreadable(sock);
sockset_addwriteable(sock);
}
bool SockSet::sockset_isreadable(Socket sock) {
pair<Socket, Socket> sockpair;
if(sset.find(sock.getfd()) != sset.end()) {
sockpair = sset[sock.getfd()];
}
if(sockpair.first.getfd() > 0) {
return FD_ISSET(sockpair.first.getfd(), &rfdset);
}
return false;
}
bool SockSet::sockset_iswriteable(Socket sock) {
pair<Socket, Socket> sockpair;
if(sset.find(sock.getfd()) != sset.end()) {
sockpair = sset[sock.getfd()];
}
if(sockpair.second.getfd() > 0) {
return FD_ISSET(sockpair.second.getfd(), &wfdset);
}
return false;
}
bool SockSet::sockset_isset(Socket sock) {
return (sockset_iswriteable(sock) ||
sockset_isreadable(sock));
}
pair<Socket, Socket> SockSet::sockset_select() {
return sset[select(maxfd+1, &rfdset, &wfdset, NULL, NULL)];
}
fd_set SockSet::getrfdset() const {
return rfdset;
}
fd_set SockSet::getwfdset() const {
return wfdset;
}