-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet.c
237 lines (198 loc) · 7.25 KB
/
net.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
224
225
226
227
228
229
230
231
232
233
234
235
236
/******************************************************************************/
/* ____ _ _ _ _ _ _____ _____ */
/* / ___| / \ | \ | | | | | | |_ _| | ____| */
/* | | / _ \ | \| | | | | | | | | _| */
/* | |___ / ___ \ | |\ | | |_| | | | | |___ */
/* \____| /_/ \_\ |_| \_| \___/ |_| |_____| */
/* */
/* NETWORK MANAGEMENT AND TRANSMISSION FUNCTIONS */
/* */
/******************************************************************************/
#include "canute.h"
/*
* open_connection_server
*
* Set up a connection in server mode. Opens the specified port for listening
* and wait for someone to connect. Return the connected socket ready for
* transmission.
*/
SOCKET open_connection_server (unsigned short port)
{
SOCKET bsk, sk; /* bsk --> Binding SocKet */
struct sockaddr_in saddr;
int e;
socklen_t alen;
bsk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (bsk == INVALID_SOCKET)
fatal("Could not create socket");
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr.s_addr = INADDR_ANY;
/* Ignore errors from setsockopt(), bind() will fail in that case */
e = 1;
setsockopt(bsk, SOL_SOCKET, SO_REUSEADDR, CCP_CAST &e, sizeof(e));
e = bind(bsk, (SOCKADDR *) &saddr, sizeof(saddr));
if (e == SOCKET_ERROR)
fatal("Could not open port %d", port);
e = listen(bsk, 1);
if (e == SOCKET_ERROR)
fatal("Could not listen on port %d", port);
alen = sizeof(saddr);
sk = accept(bsk, (SOCKADDR *) &saddr, &alen);
if (sk == INVALID_SOCKET)
fatal("Could not accept client connection");
/* Binding socket not needed anymore */
closesocket(bsk);
return sk;
}
/*
* open_connection_client
*
* Set up a connection in client mode. Try to connect to the specified host
* (either a hostname or an IP address) and return the connected socket ready
* for transmission.
*/
SOCKET open_connection_client (char *host, unsigned short port)
{
SOCKET sk;
struct sockaddr_in saddr;
struct hostent *he;
int e;
sk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sk == INVALID_SOCKET)
fatal("Creating socket");
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr.s_addr = inet_addr(host);
if (saddr.sin_addr.s_addr == INADDR_NONE)
{
/* Invalid IP, maybe we have to do a DNS query */
he = gethostbyname(host);
if (he == NULL)
{
#if defined(HASEFROCH) || defined(OMIT_HERROR)
fatal("Invalid IP or hostname");
#else
herror("FATAL ERROR: Invalid IP or hostname");
exit(EXIT_FAILURE);
#endif
}
saddr.sin_addr.s_addr = ((struct in_addr *) he->h_addr)->s_addr;
}
/* Now we have a destination host */
e = connect(sk, (SOCKADDR *) &saddr, sizeof(saddr));
if (e == SOCKET_ERROR)
fatal("Connecting to host '%s'", host);
return sk;
}
/*
* send_data
*
* Send count bytes over the connection. Doesn't finish until all of them are
* sent. On error aborts.
*/
void send_data (SOCKET sk, char *buf, size_t count)
{
int s; /* Sent bytes in one send() call */
do {
s = send(sk, buf, count, 0);
if (s == SOCKET_ERROR)
fatal("Sending data");
count -= s;
buf += s;
} while (count > 0);
}
/*
* receive_data
*
* Receive count bytes from the connection. Doesn't finish until all bytes are
* received. On error aborts.
*/
void receive_data (SOCKET sk, char *buf, size_t count)
{
int r; /* Received bytes in one recv() call */
do {
r = recv(sk, buf, count, 0);
if (r == SOCKET_ERROR)
fatal("Receiving data");
count -= r;
buf += r;
} while (count > 0);
}
/*
* send_message
*
* Build a header packet and send it through the connection. All the fields are
* converted to network byte order if required.
*/
void send_message (SOCKET sk,
int type,
int is_executable,
int mtime,
long long size,
char *name)
{
int blocks, extra;
struct header packet;
blocks = (int) (size >> CANUTE_BLOCK_BITS);
extra = (int) (size & CANUTE_BLOCK_MASK);
/* Encode executable bit on the mtime filed; never mind compatibility
* because the remote side will ignore the mtime field */
if (is_executable)
mtime = -mtime;
packet.type = htonl(type);
packet.mtime = htonl(mtime);
packet.blocks = htonl(blocks); /* Read protocol.c for an explanation */
packet.extra = htonl(extra);
if (name != NULL)
strncpy(packet.name, name, CANUTE_NAME_LENGTH);
else
packet.name[0] = '\0';
/* Mark the packet as enhanced version and send it */
packet.name[CANUTE_NAME_LENGTH] = CANUTE_ENHANCED;
send_data(sk, (char *) &packet, sizeof(struct header));
}
/*
* receive_message
*
* Read from the connection expecting a header packet. Fix byte ordering if
* necessary, fill the fields (if address was provided by the caller) and return
* the message type.
*/
int receive_message (SOCKET sk,
int *is_executable,
int *mtime,
long long *size,
char *name)
{
int blocks, extra, pmtime = 0, is_x = 0;
struct header packet;
receive_data(sk, (char *) &packet, sizeof(struct header));
if (packet.name[CANUTE_NAME_LENGTH] == CANUTE_ENHANCED)
{
/* Decode executable bit from the mtime field */
pmtime = ntohl(packet.mtime);
if (pmtime < 0)
{
is_x = !is_x;
pmtime = -pmtime;
}
}
if (is_executable != NULL)
*is_executable = is_x;
if (mtime != NULL)
*mtime = pmtime;
if (size != NULL)
{
/* Read protocol.c for an explanation */
blocks = ntohl(packet.blocks);
extra = ntohl(packet.extra);
*size = ((long long) blocks << 16) + extra;
}
if (name != NULL)
{
strncpy(name, packet.name, CANUTE_NAME_LENGTH);
name[CANUTE_NAME_LENGTH] = '\0';
}
return ntohl(packet.type);
}