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: dk4sock21.ctr
12 */
13 
14 /**	@file dk4sock21.c The dk4sock21 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_sendto(dk4_socket_t sock,const void * buf,size_t * pszbuf,int flags,const struct sockaddr * saddr,size_t addrlen,long secs,long usecs,dk4_er_t * erp)64 dk4socket_sendto(
65   dk4_socket_t		 sock,
66   const void		*buf,
67   size_t		*pszbuf,
68   int			 flags,
69   const struct sockaddr	*saddr,
70   size_t		 addrlen,
71   long			 secs,
72   long			 usecs,
73   dk4_er_t		*erp
74 )
75 {
76   fd_set	 	wfds;
77   struct timeval	to;
78 #if DK4_HAVE_SSIZE_T
79   ssize_t		bwr;
80 #else
81   int			bwr;
82 #endif
83   int			res;
84   int		 	back = DK4_SOCKET_RESULT_FAILED;
85 #if	DK4_USE_ASSERT
86   assert(INVALID_SOCKET != sock);
87   assert(NULL != buf);
88   assert(NULL != pszbuf);
89   assert(0 < *pszbuf);
90   assert(NULL != saddr);
91   assert(0 < addrlen);
92 #endif
93   if (
94     (INVALID_SOCKET != sock) && (NULL != buf) && (NULL != pszbuf)
95     && (NULL != saddr) && (0 < addrlen)
96   )
97   {
98     if ((0 < *pszbuf) && (0 != dk4socket_correct_addr_lgt(saddr, &addrlen))) {
99 #if DK4_HAVE_SSIZE_T
100       if ((dk4_um_t)(*pszbuf) <= (dk4_um_t)(SIZE_MAX / 2))
101 #else
102       if ((dk4_um_t)(*pszbuf) <= (dk4_um_t)(INT_MAX))
103 #endif
104       {
105         if ((0L != secs) || (0L != usecs)) {
106 	  /*
107 		  Timeout, check writability.
108 	  */
109 	  FD_ZERO(&wfds);
110 	  FD_SET(sock, &wfds);
111 	  to.tv_sec = secs;
112 	  to.tv_usec = usecs;
113 	  dk4socket_error_reset();
114 	  res = select(((int)sock + 1), NULL, &wfds, NULL, &to);
115 	  if (0 < res) {
116 	    if (FD_ISSET(sock, &wfds)) {
117 	      back = DK4_SOCKET_RESULT_SUCCESS;
118 	    } else {
119 	      /* Timeout */
120 	      dk4error_set_simple_error_code(erp, DK4_E_SOCKET_TIMEOUT);
121 	    }
122 	  } else {
123 #if DK4_ON_WINDOWS
124 	    if (SOCKET_ERROR == res)
125 #else
126 	    if (-1 == res)
127 #endif
128 	    {
129 	      /* select error */
130 	      dk4socket_error_report(erp, DK4_E_SOCKET_SELECT);
131 	    }
132 	    else
133 	    {
134 	      /* Timeout */
135 	      dk4error_set_simple_error_code(erp, DK4_E_SOCKET_TIMEOUT);
136 	    }
137 	  }
138         } else {
139           /*
140 		  No timeout, can continue sending immediately.
141 	  */
142           back = DK4_SOCKET_RESULT_SUCCESS;
143         }
144         if (DK4_SOCKET_RESULT_SUCCESS == back) {
145 	  (void)dk4socket_correct_addr_lgt(saddr, &addrlen);
146           back = DK4_SOCKET_RESULT_FAILED;
147 	  /*
148 		  Really send.
149 	  */
150 	  dk4socket_error_reset();
151 #if DK4_ON_WINDOWS
152 #if DK4_HAVE_SOCKLEN_T
153 	  bwr = sendto(
154 	    sock, buf, (int)(*pszbuf), flags, saddr, (socklen_t)addrlen
155 	  );
156 #else
157 	  bwr = sendto(
158 	    sock, buf, (int)(*pszbuf), flags, saddr, (int)addrlen
159 	  );
160 #endif
161 #else
162 #if DK4_HAVE_SOCKLEN_T
163 	  bwr = sendto(
164 	    sock, buf, *pszbuf, flags, saddr, (socklen_t)addrlen
165 	  );
166 #else
167 	  bwr = sendto(
168 	    sock, buf, *pszbuf, flags, saddr, (int)addrlen
169 	  );
170 #endif
171 #endif
172 	  if (0 <= bwr) {
173 	    if ((size_t)bwr == *pszbuf) {
174 	      back = DK4_SOCKET_RESULT_SUCCESS;
175 	    } else {
176 	      back = DK4_SOCKET_RESULT_IN_PROGRESS;
177 	    }
178 	    *pszbuf = (size_t)bwr;
179 	  } else {
180 	    dk4socket_error_report(erp, DK4_E_SOCKET_SEND);
181 	    *pszbuf = 0;
182 	  }
183         }
184       } else {
185         dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
186       }
187     } else {
188       dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
189     }
190   } else {
191     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
192   }
193   return back;
194 }
195 
196 
197 
198 int
dk4socket_recvfrom(dk4_socket_t sock,void * buf,size_t * pszbuf,int flags,struct sockaddr * saddr,size_t * paddrlen,long secs,long usecs,dk4_er_t * erp)199 dk4socket_recvfrom(
200   dk4_socket_t		 sock,
201   void			*buf,
202   size_t		*pszbuf,
203   int			 flags,
204   struct sockaddr	*saddr,
205   size_t		*paddrlen,
206   long			 secs,
207   long			 usecs,
208   dk4_er_t		*erp
209 )
210 {
211   fd_set		 rfds;
212   struct timeval	 to;
213 #if DK4_HAVE_SSIZE_T
214   ssize_t		 bwr;
215 #else
216   int			 bwr;
217 #endif
218 #if DK4_HAVE_SOCKLEN_T
219   socklen_t		 peersize;
220 #else
221   int			 peersize;
222 #endif
223   int			 res;
224   int			 back = DK4_SOCKET_RESULT_FAILED;
225 #if	DK4_USE_ASSERT
226   assert(INVALID_SOCKET != sock);
227   assert(NULL != buf);
228   assert(NULL != pszbuf);
229   assert(0 < *pszbuf);
230   assert(NULL != saddr);
231   assert(NULL != paddrlen);
232 #endif
233   if (
234     (INVALID_SOCKET != sock) && (NULL != buf) && (NULL != pszbuf)
235     && (NULL != saddr) && (NULL != paddrlen)
236   )
237   {
238     if ((0 < *pszbuf) && (0 < *paddrlen)) {
239 #if DK4_HAVE_SSIZE_T
240       if ((dk4_um_t)(*pszbuf) <= (dk4_um_t)(SIZE_MAX / 2))
241 #else
242       if ((dk4_um_t)(*pszbuf) <= (dk4_um_t)(INT_MAX))
243 #endif
244       {
245         if ((0L != secs) || (0L != usecs)) {
246 	  /*
247 		Timeout, check readability.
248 	  */
249 	  FD_ZERO(&rfds);
250 	  FD_SET(sock, &rfds);
251 	  to.tv_sec = secs;
252 	  to.tv_usec = usecs;
253 	  dk4socket_error_reset();
254 	  res = select(((int)sock + 1), &rfds, NULL, NULL, &to);
255 	  if (0 < res) {
256 	    if (FD_ISSET(sock, &rfds)) {
257 	      back = DK4_SOCKET_RESULT_SUCCESS;
258 	    } else {
259 	      dk4error_set_simple_error_code(erp, DK4_E_SOCKET_TIMEOUT);
260 	    }
261 	  } else {
262 #if DK4_ON_WINDOWS
263 	    if (SOCKET_ERROR == res)
264 #else
265 	    if (-1 == res)
266 #endif
267 	    {
268 	      dk4socket_error_report(erp, DK4_E_SOCKET_SELECT);
269 	    }
270 	    else
271 	    {
272 	      dk4error_set_simple_error_code(erp, DK4_E_SOCKET_TIMEOUT);
273 	    }
274 	  }
275 	} else {
276 	  /*
277 		No timeout.
278 	  */
279 	  back = DK4_SOCKET_RESULT_SUCCESS;
280 	}
281 	if (DK4_SOCKET_RESULT_SUCCESS == back) {
282 	  back = DK4_SOCKET_RESULT_FAILED;
283 #if DK4_HAVE_SOCKLEN_T
284 	  peersize = (socklen_t)(*paddrlen);
285 #else
286 	  peersize = (int)(*paddrlen);
287 #endif
288 	  dk4socket_error_reset();
289 #if DK4_ON_WINDOWS
290 	  bwr = recvfrom(
291 	    sock, buf, (int)(*pszbuf), flags, saddr, &peersize
292 	  );
293 #else
294 	  bwr = recvfrom(
295 	    sock, buf, *pszbuf, flags, saddr, &peersize
296 	  );
297 #endif
298 	  if (0 <= bwr) {
299 	    back = DK4_SOCKET_RESULT_SUCCESS;
300 	    *pszbuf = (size_t)bwr;
301 	  } else {
302 	    dk4socket_error_report(erp, DK4_E_SOCKET_RECV);
303 	    *pszbuf = 0;
304 	  }
305 	  *paddrlen = (size_t)peersize;
306 	}
307       }
308       else
309       {
310         dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
311       }
312     } else {
313       dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
314     }
315   } else {
316     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
317   }
318   return back;
319 }
320 
321 
322