forked from cfengine/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassic.c
168 lines (142 loc) · 4.89 KB
/
classic.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
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
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; version 3.
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <classic.h>
#include <logging.h>
#include <misc_lib.h>
static bool LastRecvTimedOut(void)
{
#ifndef __MINGW32__
if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
{
return true;
}
#else
int lasterror = GetLastError();
if (lasterror == EAGAIN || lasterror == WSAEWOULDBLOCK)
{
return true;
}
#endif
return false;
}
/**
* @brief Receive up to #toget bytes, plus a '\0', into buffer from sd.
* @param sd Socket descriptor
* @param buffer Buffer into which to read data
* @param toget Number of bytes to read; a '\0' shall be written after
* the data; buffer must have space for that.
*
* @note Writes up to buffer[toget] (toget + 1 bytes total)
* @return number of bytes actually received, must be equal to #toget
* -1 in case of timeout or error - socket is unusable
* 0 connection was closed (not an error, that's how clients
* normally terminate)
* <toget NEVER
*/
int RecvSocketStream(int sd, char *buffer, int toget)
{
int already, got;
// This function used to take char buffer[CF_BUFSIZE]
// Since it only writes up to (toget + 1) bytes I changed the function
// signature
if (toget > CF_BUFSIZE - 1 || toget <= 0)
{
Log(LOG_LEVEL_ERR, "Bad software request to receive %d bytes", toget);
return -1;
}
/* Repeat recv() until we get "toget" bytes. */
for (already = 0; already < toget; already += got)
{
got = recv(sd, buffer + already, toget - already, 0);
if (got == -1)
{
if (errno != EINTR) /* recv() again in case of signal */
{
if (LastRecvTimedOut())
{
Log(LOG_LEVEL_ERR, "Receive timeout"
" (received=%dB, expecting=%dB) (recv: %s)",
already, toget, GetErrorStr());
Log(LOG_LEVEL_VERBOSE,
"Consider increasing body agent control"
" \"default_timeout\" setting");
/* Shutdown() TCP connection despite of EAGAIN error, in
* order to avoid receiving this delayed response later on
* (Redmine #6027). */
shutdown(sd, SHUT_RDWR);
}
else
{
Log(LOG_LEVEL_ERR, "Couldn't receive (recv: %s)",
GetErrorStr());
}
return -1;
}
}
else if (got == 0)
{
Log(LOG_LEVEL_VERBOSE, "Peer has closed the connection");
buffer[already] = '\0';
return 0;
}
}
assert(already == toget);
buffer[already] = '\0';
return already;
}
/*************************************************************************/
/**
* @brief Send #tosend bytes from buffer to sd.
* @param sd Socket descriptor
* @param buffer Buffer from which to read data
* @param tosend Number of bytes to write.
*
* @return number of bytes actually sent, must be equal to #tosend
* -1 in case of timeout or error - socket is unusable
* -1 also in case of early proper connection close
* 0 NEVER
* <tosend NEVER
*/
int SendSocketStream(int sd, const char buffer[CF_BUFSIZE], int tosend)
{
int sent, already = 0;
if (tosend <= 0)
{
Log(LOG_LEVEL_ERR, "Bad software request to send %d bytes",
tosend);
return -1;
}
EnforceBwLimit(tosend);
do
{
sent = send(sd, buffer + already, tosend - already, 0);
if ((sent == -1) && (errno == EINTR))
{
continue;
}
if (sent == -1)
{
Log(LOG_LEVEL_ERR, "Couldn't send. (send: %s)", GetErrorStr());
return -1;
}
already += sent;
} while (already < tosend);
assert(already == tosend);
return already;
}