tsocket: add tdgram_sendto_queue_send/recv()
[ira/wip.git] / lib / tsocket / tsocket_helpers.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Stefan Metzmacher 2009
5
6      ** NOTE! The following LGPL license applies to the tevent
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "replace.h"
25 #include "system/network.h"
26 #include "system/filesys.h"
27 #include "tsocket.h"
28 #include "tsocket_internal.h"
29
30 int tsocket_error_from_errno(int ret,
31                              int sys_errno,
32                              bool *retry)
33 {
34         *retry = false;
35
36         if (ret >= 0) {
37                 return 0;
38         }
39
40         if (ret != -1) {
41                 return EIO;
42         }
43
44         if (sys_errno == 0) {
45                 return EIO;
46         }
47
48         if (sys_errno == EINTR) {
49                 *retry = true;
50                 return sys_errno;
51         }
52
53         if (sys_errno == EINPROGRESS) {
54                 *retry = true;
55                 return sys_errno;
56         }
57
58         if (sys_errno == EAGAIN) {
59                 *retry = true;
60                 return sys_errno;
61         }
62
63 #ifdef EWOULDBLOCK
64         if (sys_errno == EWOULDBLOCK) {
65                 *retry = true;
66                 return sys_errno;
67         }
68 #endif
69
70         return sys_errno;
71 }
72
73 int tsocket_simple_int_recv(struct tevent_req *req, int *perrno)
74 {
75         enum tevent_req_state state;
76         uint64_t error;
77
78         if (!tevent_req_is_error(req, &state, &error)) {
79                 return 0;
80         }
81
82         switch (state) {
83         case TEVENT_REQ_NO_MEMORY:
84                 *perrno = ENOMEM;
85                 return -1;
86         case TEVENT_REQ_TIMED_OUT:
87                 *perrno = ETIMEDOUT;
88                 return -1;
89         case TEVENT_REQ_USER_ERROR:
90                 *perrno = (int)error;
91                 return -1;
92         default:
93                 *perrno = EIO;
94                 return -1;
95         }
96
97         *perrno = EIO;
98         return -1;
99 }
100
101 int tsocket_common_prepare_fd(int fd, bool high_fd)
102 {
103         int i;
104         int sys_errno = 0;
105         int fds[3];
106         int num_fds = 0;
107
108         int result, flags;
109
110         if (fd == -1) {
111                 return -1;
112         }
113
114         /* first make a fd >= 3 */
115         if (high_fd) {
116                 while (fd < 3) {
117                         fds[num_fds++] = fd;
118                         fd = dup(fd);
119                         if (fd == -1) {
120                                 sys_errno = errno;
121                                 break;
122                         }
123                 }
124                 for (i=0; i<num_fds; i++) {
125                         close(fds[i]);
126                 }
127                 if (fd == -1) {
128                         errno = sys_errno;
129                         return fd;
130                 }
131         }
132
133         /* fd should be nonblocking. */
134
135 #ifdef O_NONBLOCK
136 #define FLAG_TO_SET O_NONBLOCK
137 #else
138 #ifdef SYSV
139 #define FLAG_TO_SET O_NDELAY
140 #else /* BSD */
141 #define FLAG_TO_SET FNDELAY
142 #endif
143 #endif
144
145         if ((flags = fcntl(fd, F_GETFL)) == -1) {
146                 goto fail;
147         }
148
149         flags |= FLAG_TO_SET;
150         if (fcntl(fd, F_SETFL, flags) == -1) {
151                 goto fail;
152         }
153
154 #undef FLAG_TO_SET
155
156         /* fd should be closed on exec() */
157 #ifdef FD_CLOEXEC
158         result = flags = fcntl(fd, F_GETFD, 0);
159         if (flags >= 0) {
160                 flags |= FD_CLOEXEC;
161                 result = fcntl(fd, F_SETFD, flags);
162         }
163         if (result < 0) {
164                 goto fail;
165         }
166 #endif
167         return fd;
168
169  fail:
170         if (fd != -1) {
171                 sys_errno = errno;
172                 close(fd);
173                 errno = sys_errno;
174         }
175         return -1;
176 }
177