1 /*
2 Copyright (C) 2015-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5 
6 /*
7 	WARNING: This file was generated by the dkct program (see
8 	http://dktools.sourceforge.net/ for details).
9 	Changes you make here will be lost if dkct is run again!
10 	You should modify the original source and run dkct on it.
11 	Original source: dk4sock20.ctr
12 */
13 
14 /**	@file dk4sock20.c The dk4sock20 module.
15 */
16 
17 
18 #include "dk4conf.h"
19 #include <libdk4sock/dk4sock.h>
20 
21 #if DK4_HAVE_LIMITS_H
22 #ifndef LIMITS_H_INCLUDED
23 #include <limits.h>
24 #define	LIMITS_H_INCLUDED 1
25 #endif
26 #endif
27 
28 #if DK4_HAVE_STDINT_H
29 #ifndef STDINT_H_INCLUDED
30 #if defined(__cplusplus) && (!defined(__STDC_LIMIT_MACROS))
31 #define __STDC_LIMIT_MACROS 1
32 #endif
33 #if defined(__cplusplus) && (!defined(__STDC_CONSTANT_MACROS))
34 #define __STDC_CONSTANT_MACROS 1
35 #endif
36 #include <stdint.h>
37 #define	STDINT_H_INCLUDED 1
38 #endif
39 #endif
40 
41 #if DK4_HAVE_STRING_H
42 #ifndef	STRING_H_INCLUDED
43 #include <string.h>
44 #define	STRING_H_INCLUDED 1
45 #endif
46 #endif
47 
48 #include <libdk4base/dk4numco.h>
49 
50 #if DK4_HAVE_ASSERT_H
51 #ifndef	ASSERT_H_INCLUDED
52 #include <assert.h>
53 #define	ASSERT_H_INCLUDED 1
54 #endif
55 #endif
56 
57 
58 
59 
60 
61 
62 
63 int
dk4socket_send(dk4_socket_t sock,const void * buf,size_t * pszbuf,int flags,long secs,long usecs,dk4_er_t * erp)64 dk4socket_send(
65   dk4_socket_t	 sock,
66   const void	*buf,
67   size_t	*pszbuf,
68   int		 flags,
69   long		 secs,
70   long		 usecs,
71   dk4_er_t	*erp
72 )
73 {
74   fd_set	 	wfds;
75   struct timeval	to;
76 #if DK4_HAVE_SSIZE_T
77   ssize_t		bwr;
78 #else
79   int			bwr;
80 #endif
81   int			res;
82   int		 	back = DK4_SOCKET_RESULT_FAILED;
83 
84 #if	DK4_USE_ASSERT
85   assert(INVALID_SOCKET != sock);
86   assert(NULL != buf);
87   assert(NULL != pszbuf);
88   assert(0 < *pszbuf);
89 #endif
90   if ((INVALID_SOCKET != sock) && (NULL != buf) && (NULL != pszbuf)) {
91     if (0 < *pszbuf) {
92 #if DK4_HAVE_SSIZE_T
93       if ((dk4_um_t)(*pszbuf) <= (dk4_um_t)(SIZE_MAX / 2))
94 #else
95       if ((dk4_um_t)(*pszbuf) <= (dk4_um_t)(INT_MAX))
96 #endif
97       {
98         if ((0L != secs) || (0L != usecs)) {
99 	  /*
100 		  Timeout, check writability.
101 	  */
102 	  FD_ZERO(&wfds);
103 	  FD_SET(sock, &wfds);
104 	  to.tv_sec = secs;
105 	  to.tv_usec = usecs;
106 	  dk4socket_error_reset();
107 	  res = select(((int)sock + 1), NULL, &wfds, NULL, &to);
108 	  if (0 < res) {
109 	    if (FD_ISSET(sock, &wfds)) {
110 	      back = DK4_SOCKET_RESULT_SUCCESS;
111 	    } else {
112 	      /* Timeout */
113 	      dk4error_set_simple_error_code(erp, DK4_E_SOCKET_TIMEOUT);
114 	    }
115 	  } else {
116 #if DK4_ON_WINDOWS
117 	    if (SOCKET_ERROR == res)
118 #else
119 	    if (-1 == res)
120 #endif
121 	    {
122 	      /* select error */
123 	      dk4socket_error_report(erp, DK4_E_SOCKET_SELECT);
124 	    }
125 	    else
126 	    {
127 	      /* Timeout */
128 	      dk4error_set_simple_error_code(erp, DK4_E_SOCKET_TIMEOUT);
129 	    }
130 	  }
131         } else {
132           /*
133 		  No timeout, can continue sending immediately.
134 	  */
135           back = DK4_SOCKET_RESULT_SUCCESS;
136         }
137         if (DK4_SOCKET_RESULT_SUCCESS == back) {
138           back = DK4_SOCKET_RESULT_FAILED;
139 	  /*
140 		  Really send.
141 	  */
142 	  dk4socket_error_reset();
143 #if DK4_ON_WINDOWS
144 	  bwr = send(sock, buf, (int)(*pszbuf), flags);
145 #else
146 	  bwr = send(sock, buf, *pszbuf, flags);
147 #endif
148 	  if (0 <= bwr) {
149 	    if ((size_t)bwr == *pszbuf) {
150 	      back = DK4_SOCKET_RESULT_SUCCESS;
151 	    } else {
152 	      back = DK4_SOCKET_RESULT_IN_PROGRESS;
153 	    }
154 	    *pszbuf = (size_t)bwr;
155 	  } else {
156 	    dk4socket_error_report(erp, DK4_E_SOCKET_SEND);
157 	    *pszbuf = 0;
158 	  }
159         } else {
160 	  *pszbuf = 0;
161 	}
162       } else {
163         dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
164 	*pszbuf = 0;
165       }
166     } else {
167       dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
168       *pszbuf = 0;
169     }
170   } else {
171     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
172     if (NULL != pszbuf) { *pszbuf = 0; }
173   }
174   return back;
175 }
176 
177 
178 
179 int
dk4socket_recv(dk4_socket_t sock,void * buf,size_t * pszbuf,int flags,long secs,long usecs,dk4_er_t * erp)180 dk4socket_recv(
181   dk4_socket_t	 sock,
182   void		*buf,
183   size_t	*pszbuf,
184   int		 flags,
185   long		 secs,
186   long		 usecs,
187   dk4_er_t	*erp
188 )
189 {
190   fd_set		 rfds;
191   struct timeval	 to;
192 #if DK4_HAVE_SSIZE_T
193   ssize_t		 bwr;
194 #else
195   int			 bwr;
196 #endif
197   int			 res;
198   int			 back = DK4_SOCKET_RESULT_FAILED;
199 #if	DK4_USE_ASSERT
200   assert(INVALID_SOCKET != sock);
201   assert(NULL != buf);
202   assert(NULL != pszbuf);
203   assert(0 < *pszbuf);
204 #endif
205   if ((INVALID_SOCKET != sock) && (NULL != buf) && (NULL != pszbuf)) {
206     if (0 < *pszbuf) {
207 #if DK4_HAVE_SSIZE_T
208       if ((dk4_um_t)(*pszbuf) <= (dk4_um_t)(SIZE_MAX / 2))
209 #else
210       if ((dk4_um_t)(*pszbuf) <= (dk4_um_t)(INT_MAX))
211 #endif
212       {
213         if ((0L != secs) || (0L != usecs)) {
214 	  /*
215 		Timeout, check readability.
216 	  */
217 	  FD_ZERO(&rfds);
218 	  FD_SET(sock, &rfds);
219 	  to.tv_sec = secs;
220 	  to.tv_usec = usecs;
221 	  dk4socket_error_reset();
222 	  res = select(((int)sock + 1), &rfds, NULL, NULL, &to);
223 	  if (0 < res) {
224 	    if (FD_ISSET(sock, &rfds)) {
225 	      back = DK4_SOCKET_RESULT_SUCCESS;
226 	    } else {
227 	      dk4error_set_simple_error_code(erp, DK4_E_SOCKET_TIMEOUT);
228 	    }
229 	  } else {
230 #if DK4_ON_WINDOWS
231 	    if (SOCKET_ERROR == res)
232 #else
233 	    if (-1 == res)
234 #endif
235 	    {
236 	      dk4socket_error_report(erp, DK4_E_SOCKET_SELECT);
237 	    }
238 	    else
239 	    {
240 	      dk4error_set_simple_error_code(erp, DK4_E_SOCKET_TIMEOUT);
241 	    }
242 	  }
243 	} else {
244 	  /*
245 		No timeout.
246 	  */
247 	  back = DK4_SOCKET_RESULT_SUCCESS;
248 	}
249 	if (DK4_SOCKET_RESULT_SUCCESS == back) {
250 	  back = DK4_SOCKET_RESULT_FAILED;
251 	  dk4socket_error_reset();
252 #if DK4_ON_WINDOWS
253 	  bwr = recv(sock, buf, (int)(*pszbuf), flags);
254 #else
255 	  bwr = recv(sock, buf, *pszbuf, flags);
256 #endif
257 	  if (0 <= bwr) {
258 	    back = DK4_SOCKET_RESULT_SUCCESS;
259 	    *pszbuf = (size_t)bwr;
260 	  } else {
261 	    dk4socket_error_report(erp, DK4_E_SOCKET_RECV);
262 	    *pszbuf = 0;
263 	  }
264 	}
265       }
266       else
267       {
268         dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
269       }
270     } else {
271       dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
272     }
273   } else {
274     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
275   }
276   return back;
277 }
278 
279 
280