forked from commoncriteria/tls-cc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_sock.c
223 lines (192 loc) · 5.8 KB
/
tcp_sock.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Code based off of Beej's Guide to Network Programming
*/
#include "tcp_sock.h"
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include "printer.h"
#define MAXPORTLEN (6)
#ifdef SO_NOSIGPIPE
#define OSX_SO_NOSIGPIPE
#else
#ifndef MSG_NOSIGNAL
#error "Cannot block SIGPIPE!"
#endif
#endif
int
create_server_sock(const char *interface, const char *port, int backlog,
int family, int reuse)
{
struct addrinfo hints;
struct addrinfo *res = 0;
struct addrinfo *walker;
int sockfd;
int error;
int yes = 1;
char str_ip[INET6_ADDRSTRLEN]; /* should fit ipv4 too */
char str_port[MAXPORTLEN];
memset(&hints, 0, sizeof (hints));
memset(str_ip, 0, sizeof (str_ip));
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((error = getaddrinfo(interface, port, &hints, &res)) != 0) {
write_out(PRINT_ERROR, "Error attempting to call getaddrinfo:"
" %s", gai_strerror(error));
return (-1);
}
write_out(PRINT_INFO, "Searching for interface to listen on...");
write_raise_level();
for (walker = res; walker; walker = walker->ai_next) {
if ((sockfd = socket(walker->ai_family, walker->ai_socktype,
walker->ai_protocol)) == -1) {
write_out(PRINT_WARNING, "Invalid interface: %s",
strerror(errno));
write_raise_level();
write_out(PRINT_WARNING, "Domain: %d, Type: %d,"
" Protocol: %d", walker->ai_family,
walker->ai_socktype, walker->ai_protocol);
write_lower_level();
continue;
}
if (reuse)
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof (int)) == -1)
write_out(PRINT_WARNING, "Unable to set "
"SO_REUSEADDR on socket: %s",
strerror(errno));
#ifdef OSX_SO_NOSIGPIPE
if (setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &yes,
sizeof (int)) == -1)
write_out(PRINT_WARNING, "Unable to set SO_NOSIGPIPE"
" on socket: %s", strerror(errno));
#endif
if (bind(sockfd, walker->ai_addr, walker->ai_addrlen) == -1) {
getnameinfo((struct sockaddr *)walker->ai_addr,
walker->ai_addrlen, str_ip, sizeof (str_ip),
str_port, sizeof (str_port),
NI_NUMERICHOST | NI_NUMERICSERV);
write_out(PRINT_WARNING, "Unable to bind to interface:"
" %s", strerror(errno));
write_raise_level();
write_out(PRINT_WARNING, "Domain: %d, Type: %d,"
" Protocol: %d, Address: %s, Port: %s",
walker->ai_family, walker->ai_socktype,
walker->ai_protocol, str_ip, str_port);
write_lower_level();
close(sockfd);
continue;
}
break;
}
if (!walker) {
sockfd = 0;
write_out(PRINT_ERROR, "Unable to find valid interface.");
freeaddrinfo(res);
write_lower_level();
return (-1);
}
write_lower_level();
getnameinfo((struct sockaddr *)walker->ai_addr, walker->ai_addrlen,
str_ip, sizeof (str_ip), str_port, sizeof (str_port),
NI_NUMERICHOST | NI_NUMERICSERV);
freeaddrinfo(res);
if (listen(sockfd, backlog) == -1) {
write_out(PRINT_ERROR, "Unable to listen on interface %s on"
" port %s.", str_ip, str_port);
close(sockfd);
return (-1);
}
write_out(PRINT_INFO, "Listening on on interface %s on port %s.",
str_ip, str_port);
return (sockfd);
}
int
accept_connection(int sockfd, struct sockaddr_storage *out_opt_client_addr)
{
struct sockaddr_storage client_addr;
struct sockaddr_storage *ptr_to_client_addr;
socklen_t addr_size;
int newfd;
char str_ip[INET6_ADDRSTRLEN]; /* should fit ipv4 too */
char str_port[MAXPORTLEN];
addr_size = sizeof (struct sockaddr_storage);
if (out_opt_client_addr)
ptr_to_client_addr = out_opt_client_addr;
else
ptr_to_client_addr = &client_addr;
newfd = accept(sockfd, (struct sockaddr *)ptr_to_client_addr,
&addr_size);
if (newfd == -1)
write_out(PRINT_WARNING, "Unable to accept client connection:"
" %s", strerror(errno));
else {
getnameinfo((struct sockaddr *)ptr_to_client_addr,
sizeof (struct sockaddr_storage), str_ip, sizeof (str_ip),
str_port, sizeof (str_port),
NI_NUMERICHOST | NI_NUMERICSERV);
write_out(PRINT_INFO, "Accepted connection from remote client"
" %s on port %s", str_ip, str_port);
}
return (newfd);
}
int
do_connect(const char *target, const char *port)
{
int sockfd;
struct addrinfo hints;
struct addrinfo *res;
struct addrinfo *walker;
int error;
char str_ip[INET6_ADDRSTRLEN]; /* should fit ipv4 too */
char str_port[MAXPORTLEN];
memset(&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((error = getaddrinfo(target, port, &hints, &res)) != 0) {
write_out(PRINT_ERROR, "Error attempting to call getaddrinfo:"
" %s", gai_strerror(error));
return (-1);
}
write_out(PRINT_INFO, "Attempting to connect...");
write_raise_level();
for (walker = res; walker; walker = walker->ai_next) {
if ((sockfd = socket(walker->ai_family, walker->ai_socktype,
walker->ai_protocol)) == -1) {
write_out(PRINT_WARNING, "Invalid socket: %s",
gai_strerror(error));
continue;
}
if (connect(sockfd, walker->ai_addr, walker->ai_addrlen)
== -1) {
write_out(PRINT_WARNING, "Unable to establish "
"connection with socket: %s", gai_strerror(error));
close(sockfd);
continue;
}
break;
}
if (!walker) {
write_out(PRINT_ERROR, "Failed to establish connection with"
" server at %s on port %s.", target, port);
return (-1);
}
write_lower_level();
getnameinfo((struct sockaddr *)walker->ai_addr, walker->ai_addrlen,
str_ip, sizeof (str_ip), str_port, sizeof (str_port),
NI_NUMERICHOST | NI_NUMERICSERV);
write_out(PRINT_INFO, "Established connection to %s on port %s.",
str_ip, str_port);
freeaddrinfo(res);
return (sockfd);
}