source: verona/libeXosip2/src/jpipe.c @ 268:a190752a5256

Last change on this file since 268:a190752a5256 was 268:a190752a5256, checked in by Vadim Lebedev <vadim@…>, 22 months ago

exosip COVERITY fixes

File size: 6.1 KB
Line 
1/*
2  eXosip - This is the eXtended osip library.
3  Copyright (C) 2002,2003,2004,2005,2006,2007  Aymeric MOIZARD  - jack@atosc.org
4 
5  eXosip is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  eXosip is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18*/
19
20
21#ifdef ENABLE_MPATROL
22#include <mpatrol.h>
23#endif
24
25#ifdef OSIP_MT
26
27#include "jpipe.h"
28
29#if !defined(WIN32) && !defined(__arc__)
30
31#include <fcntl.h>
32
33jpipe_t *jpipe()
34{
35        jpipe_t *my_pipe = (jpipe_t *) osip_malloc(sizeof(jpipe_t));
36
37        if (my_pipe == NULL)
38                return NULL;
39
40        if (0 != pipe(my_pipe->pipes)) {
41                osip_free(my_pipe);
42                return NULL;
43        }
44
45        if (fcntl(my_pipe->pipes[1], F_SETFL, O_NONBLOCK) == -1) {
46                /* failed for some reason... */
47                OSIP_TRACE(osip_trace
48                                   (__FILE__, __LINE__, OSIP_ERROR, NULL,
49                                        "cannot set O_NONBLOCK to the pipe[1]!\n"));
50        }
51
52        return my_pipe;
53}
54
55int jpipe_close(jpipe_t * apipe)
56{
57        if (apipe == NULL)
58                return OSIP_BADPARAMETER;
59        close(apipe->pipes[0]);
60        close(apipe->pipes[1]);
61        osip_free(apipe);
62        return OSIP_SUCCESS;
63}
64
65
66/**
67 * Write in a pipe.
68 */
69int jpipe_write(jpipe_t * apipe, const void *buf, int count)
70{
71        if (apipe == NULL)
72                return OSIP_BADPARAMETER;
73        return write(apipe->pipes[1], buf, count);
74}
75
76/**
77 * Read in a pipe.
78 */
79int jpipe_read(jpipe_t * apipe, void *buf, int count)
80{
81        if (apipe == NULL)
82                return OSIP_BADPARAMETER;
83        return read(apipe->pipes[0], buf, count);
84}
85
86/**
87 * Get descriptor of reading pipe.
88 */
89int jpipe_get_read_descr(jpipe_t * apipe)
90{
91        if (apipe == NULL)
92                return OSIP_BADPARAMETER;
93        return apipe->pipes[0];
94}
95
96#else
97
98jpipe_t *jpipe()
99{
100        int s = 0;
101        int timeout = 0;
102        static int aport = 10500;
103        struct sockaddr_in raddr;
104        int j;
105
106        jpipe_t *my_pipe = (jpipe_t *) osip_malloc(sizeof(jpipe_t));
107
108        if (my_pipe == NULL)
109                return NULL;
110
111        s = (int) socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
112        if (0 > s) {
113                osip_free(my_pipe);
114                return NULL;
115        }
116        my_pipe->pipes[1] = (int) socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
117        if (0 > my_pipe->pipes[1]) {
118#if defined(__arc__)
119                close(s);
120#else
121                closesocket(s);
122#endif
123                osip_free(my_pipe);
124                return NULL;
125        }
126
127        raddr.sin_addr.s_addr = inet_addr("127.0.0.1");
128        raddr.sin_family = AF_INET;
129
130        j = 50;
131        while (aport++ && j-- > 0) {
132                raddr.sin_port = htons((short) aport);
133                if (bind(s, (struct sockaddr *) &raddr, sizeof(raddr)) < 0) {
134                        OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_WARNING, NULL,
135                                                                  "Failed to bind one local socket %i!\n", aport));
136                } else
137                        break;
138        }
139
140        if (j == 0) {
141                OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_ERROR, NULL,
142                                                          "Failed to bind a local socket, aborting!\n"));
143#if defined(__arc__)
144                close(s);
145                close(my_pipe->pipes[1]);
146#else
147                closesocket(s);
148                closesocket(my_pipe->pipes[1]);
149#endif
150                osip_free(my_pipe);
151                return NULL;
152        }
153
154        j = listen(s, 1);
155        if (j != 0) {
156                OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_ERROR, NULL,
157                                                          "Failed to listen on a local socket, aborting!\n"));
158#if defined(__arc__)
159                close(s);
160                close(my_pipe->pipes[1]);
161#else
162                closesocket(s);
163                closesocket(my_pipe->pipes[1]);
164#endif
165                osip_free(my_pipe);
166                return NULL;
167        }
168
169        j = setsockopt(my_pipe->pipes[1],
170                                   SOL_SOCKET,
171                                   SO_RCVTIMEO, (const char *) &timeout, sizeof(timeout));
172#if defined(__arc__)
173        if (j != 0) {
174                /* failed for some reason... */
175                OSIP_TRACE(osip_trace
176                                   (__FILE__, __LINE__, OSIP_ERROR, NULL,
177                                        "udp plugin; cannot set O_NONBLOCK to the file desciptor!\n"));
178                close(s);
179                close(my_pipe->pipes[1]);
180                osip_free(my_pipe);
181                return NULL;
182        }
183#elif !defined(_WIN32_WCE)
184        if (j != NO_ERROR) {
185                /* failed for some reason... */
186                OSIP_TRACE(osip_trace
187                                   (__FILE__, __LINE__, OSIP_ERROR, NULL,
188                                        "udp plugin; cannot set O_NONBLOCK to the file desciptor!\n"));
189                closesocket(s);
190                closesocket(my_pipe->pipes[1]);
191                osip_free(my_pipe);
192                return NULL;
193        }
194#endif
195
196        j = connect(my_pipe->pipes[1], (struct sockaddr *) &raddr, sizeof(raddr));
197#if defined(__arc__)
198        if (j != 0) {
199                /* failed for some reason... */
200                OSIP_TRACE(osip_trace
201                                   (__FILE__, __LINE__, OSIP_ERROR, NULL,
202                                        "udp plugin; cannot coonect local pipe\n"));
203                close(s);
204                close(my_pipe->pipes[1]);
205                osip_free(my_pipe);
206                return NULL;
207        }
208#elif !defined(_WIN32_WCE)
209        if (j != NO_ERROR) {
210                /* failed for some reason... */
211                OSIP_TRACE(osip_trace
212                                   (__FILE__, __LINE__, OSIP_ERROR, NULL,
213                                        "udp plugin; cannot connect local pipe\n"));
214                closesocket(s);
215                closesocket(my_pipe->pipes[1]);
216                osip_free(my_pipe);
217                return NULL;
218        }
219#endif
220
221
222        my_pipe->pipes[0] = accept(s, NULL, NULL);
223
224        if (my_pipe->pipes[0] <= 0) {
225                OSIP_TRACE(osip_trace
226                                   (__FILE__, __LINE__, OSIP_ERROR, NULL,
227                                        "udp plugin; Failed to call accept!\n"));
228#if defined(__arc__)
229                close(s);
230                close(my_pipe->pipes[1]);
231#else
232                closesocket(s);
233                closesocket(my_pipe->pipes[1]);
234#endif
235                osip_free(my_pipe);
236                return NULL;
237        }
238
239        return my_pipe;
240}
241
242int jpipe_close(jpipe_t * apipe)
243{
244        if (apipe == NULL)
245                return OSIP_BADPARAMETER;
246#if defined(__arc__)
247        close(apipe->pipes[0]);
248        close(apipe->pipes[1]);
249#else
250        closesocket(apipe->pipes[0]);
251        closesocket(apipe->pipes[1]);
252#endif
253        osip_free(apipe);
254        return OSIP_SUCCESS;
255}
256
257
258/**
259 * Write in a pipe.
260 */
261int jpipe_write(jpipe_t * apipe, const void *buf, int count)
262{
263        if (apipe == NULL)
264                return OSIP_BADPARAMETER;
265        return send(apipe->pipes[1], buf, count, 0);
266}
267
268/**
269 * Read in a pipe.
270 */
271int jpipe_read(jpipe_t * apipe, void *buf, int count)
272{
273        if (apipe == NULL)
274                return OSIP_BADPARAMETER;
275        return recv(apipe->pipes[0], buf, count, 0 /* MSG_DONTWAIT */ );        /* BUG?? */
276}
277
278/**
279 * Get descriptor of reading pipe.
280 */
281int jpipe_get_read_descr(jpipe_t * apipe)
282{
283        if (apipe == NULL)
284                return OSIP_BADPARAMETER;
285        return apipe->pipes[0];
286}
287
288#endif
289
290#endif
Note: See TracBrowser for help on using the repository browser.