@@ -80,6 +80,54 @@ socks_proxy_close(struct socks_proxy_info *sp)
8080 free (sp );
8181}
8282
83+ static bool
84+ socks_proxy_recv_char (char * c , const char * name , socket_descriptor_t sd ,
85+ struct event_timeout * server_poll_timeout ,
86+ volatile int * signal_received )
87+ {
88+ fd_set reads ;
89+ FD_ZERO (& reads );
90+ openvpn_fd_set (sd , & reads );
91+
92+ struct timeval tv ;
93+ tv .tv_sec = get_server_poll_remaining_time (server_poll_timeout );
94+ tv .tv_usec = 0 ;
95+
96+ const int status = select (sd + 1 , & reads , NULL , NULL , & tv );
97+
98+ get_signal (signal_received );
99+ if (* signal_received )
100+ {
101+ return false;
102+ }
103+
104+ /* timeout? */
105+ if (status == 0 )
106+ {
107+ msg (D_LINK_ERRORS | M_ERRNO , "%s: TCP port read timeout expired" , name );
108+ return false;
109+ }
110+
111+ /* error */
112+ if (status < 0 )
113+ {
114+ msg (D_LINK_ERRORS | M_ERRNO , "%s: TCP port read failed on select()" , name );
115+ return false;
116+ }
117+
118+ /* read single char */
119+ const ssize_t size = recv (sd , c , 1 , MSG_NOSIGNAL );
120+
121+ /* error? */
122+ if (size != 1 )
123+ {
124+ msg (D_LINK_ERRORS | M_ERRNO , "%s: TCP port read failed on recv()" , name );
125+ return false;
126+ }
127+
128+ return true;
129+ }
130+
83131static bool
84132socks_username_password_auth (struct socks_proxy_info * p , socket_descriptor_t sd ,
85133 struct event_timeout * server_poll_timeout ,
@@ -121,52 +169,12 @@ socks_username_password_auth(struct socks_proxy_info *p, socket_descriptor_t sd,
121169
122170 while (len < 2 )
123171 {
124- int status ;
125- ssize_t size ;
126- fd_set reads ;
127- struct timeval tv ;
128172 char c ;
129173
130- FD_ZERO (& reads );
131- openvpn_fd_set (sd , & reads );
132- tv .tv_sec = get_server_poll_remaining_time (server_poll_timeout );
133- tv .tv_usec = 0 ;
134-
135- status = select (sd + 1 , & reads , NULL , NULL , & tv );
136-
137- get_signal (signal_received );
138- if (* signal_received )
139- {
140- goto cleanup ;
141- }
142-
143- /* timeout? */
144- if (status == 0 )
145- {
146- msg (D_LINK_ERRORS | M_ERRNO ,
147- "socks_username_password_auth: TCP port read timeout expired" );
148- goto cleanup ;
149- }
150-
151- /* error */
152- if (status < 0 )
153- {
154- msg (D_LINK_ERRORS | M_ERRNO ,
155- "socks_username_password_auth: TCP port read failed on select()" );
156- goto cleanup ;
157- }
158-
159- /* read single char */
160- size = recv (sd , & c , 1 , MSG_NOSIGNAL );
161-
162- /* error? */
163- if (size != 1 )
174+ if (!socks_proxy_recv_char (& c , __func__ , sd , server_poll_timeout , signal_received ))
164175 {
165- msg (D_LINK_ERRORS | M_ERRNO ,
166- "socks_username_password_auth: TCP port read failed on recv()" );
167176 goto cleanup ;
168177 }
169-
170178 /* store char in buffer */
171179 buf [len ++ ] = c ;
172180 }
@@ -208,49 +216,12 @@ socks_handshake(struct socks_proxy_info *p, socket_descriptor_t sd,
208216
209217 while (len < 2 )
210218 {
211- int status ;
212- ssize_t size ;
213- fd_set reads ;
214- struct timeval tv ;
215219 char c ;
216220
217- FD_ZERO (& reads );
218- openvpn_fd_set (sd , & reads );
219- tv .tv_sec = get_server_poll_remaining_time (server_poll_timeout );
220- tv .tv_usec = 0 ;
221-
222- status = select (sd + 1 , & reads , NULL , NULL , & tv );
223-
224- get_signal (signal_received );
225- if (* signal_received )
226- {
227- return false;
228- }
229-
230- /* timeout? */
231- if (status == 0 )
232- {
233- msg (D_LINK_ERRORS | M_ERRNO , "socks_handshake: TCP port read timeout expired" );
234- return false;
235- }
236-
237- /* error */
238- if (status < 0 )
239- {
240- msg (D_LINK_ERRORS | M_ERRNO , "socks_handshake: TCP port read failed on select()" );
241- return false;
242- }
243-
244- /* read single char */
245- size = recv (sd , & c , 1 , MSG_NOSIGNAL );
246-
247- /* error? */
248- if (size != 1 )
221+ if (!socks_proxy_recv_char (& c , __func__ , sd , server_poll_timeout , signal_received ))
249222 {
250- msg (D_LINK_ERRORS | M_ERRNO , "socks_handshake: TCP port read failed on recv()" );
251223 return false;
252224 }
253-
254225 /* store char in buffer */
255226 buf [len ++ ] = c ;
256227 }
@@ -317,51 +288,10 @@ recv_socks_reply(socket_descriptor_t sd, struct openvpn_sockaddr *addr,
317288
318289 while (len < 4 + alen + 2 )
319290 {
320- int status ;
321- ssize_t size ;
322- fd_set reads ;
323- struct timeval tv ;
324291 char c ;
325292
326- FD_ZERO (& reads );
327- openvpn_fd_set (sd , & reads );
328- tv .tv_sec = get_server_poll_remaining_time (server_poll_timeout );
329- tv .tv_usec = 0 ;
330-
331- status = select (sd + 1 , & reads , NULL , NULL , & tv );
332-
333- get_signal (signal_received );
334- if (* signal_received )
335- {
336- return false;
337- }
338-
339- /* timeout? */
340- if (status == 0 )
341- {
342- msg (D_LINK_ERRORS | M_ERRNO , "recv_socks_reply: TCP port read timeout expired" );
343- return false;
344- }
345-
346- /* error */
347- if (status < 0 )
348- {
349- msg (D_LINK_ERRORS | M_ERRNO , "recv_socks_reply: TCP port read failed on select()" );
350- return false;
351- }
352-
353- /* read single char */
354- size = recv (sd , & c , 1 , MSG_NOSIGNAL );
355-
356- /* error? */
357- if (size < 0 )
358- {
359- msg (D_LINK_ERRORS | M_ERRNO , "recv_socks_reply: TCP port read failed on recv()" );
360- return false;
361- }
362- else if (size == 0 )
293+ if (!socks_proxy_recv_char (& c , __func__ , sd , server_poll_timeout , signal_received ))
363294 {
364- msg (D_LINK_ERRORS , "ERROR: recv_socks_reply: empty response from socks server" );
365295 return false;
366296 }
367297
0 commit comments