Debian Codegraph @

Xpjproject/pjlib/src/pj/activesock.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program 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 * This program 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 #include <pj/activesock.h>
20 #include <pj/compat/socket.h>
21 #include <pj/assert.h>
22 #include <pj/errno.h>
23 #include <pj/log.h>
24 #include <pj/pool.h>
25 #include <pj/sock.h>
26 #include <pj/string.h>
27
28 #if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
29 PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0
30 # include <CFNetwork/CFNetwork.h>
31
32 static pj_bool_t ios_bg_support = PJ_TRUE;
33 #endif
34
35 #define PJ_ACTIVESOCK_MAX_LOOP 50
36
37
38 enum read_type
39 {
40 TYPE_NONE,
41 TYPE_RECV,
42 TYPE_RECV_FROM
43 };
44
45 enum shutdown_dir
46 {
47 SHUT_NONE = 0,
48 SHUT_RX = 1,
49 SHUT_TX = 2
50 };
51
52 struct read_op
53 {
54 pj_ioqueue_op_key_t op_key;
55 pj_uint8_t *pkt;
56 unsigned max_size;
57 pj_size_t size;
58 pj_sockaddr src_addr;
59 int src_addr_len;
60 };
61
62 struct accept_op
63 {
64 pj_ioqueue_op_key_t op_key;
65 pj_sock_t new_sock;
66 pj_sockaddr rem_addr;
67 int rem_addr_len;
68 };
69
70 struct send_data
71 {
72 pj_uint8_t *data;
73 pj_ssize_t len;
74 pj_ssize_t sent;
75 unsigned flags;
76 };
77
78 struct pj_activesock_t
79 {
80 pj_ioqueue_key_t *key;
81 pj_bool_t stream_oriented;
82 pj_bool_t whole_data;
83 pj_ioqueue_t *ioqueue;
84 void *user_data;
85 unsigned async_count;
86 unsigned shutdown;
87 unsigned max_loop;
88 pj_activesock_cb cb;
89 #if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
90 PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0
91 int bg_setting;
92 pj_sock_t sock;
93 CFReadStreamRef readStream;
94 #endif
95
96 unsigned err_counter;
97 pj_status_t last_err;
98
99 struct send_data send_data;
100
101 struct read_op *read_op;
102 pj_uint32_t read_flags;
103 enum read_type read_type;
104
105 struct accept_op *accept_op;
106 };
107
108
109 static void ioqueue_on_read_complete(pj_ioqueue_key_t *key,
110 pj_ioqueue_op_key_t *op_key,
111 pj_ssize_t bytes_read);
112 static void ioqueue_on_write_complete(pj_ioqueue_key_t *key,
113 pj_ioqueue_op_key_t *op_key,
114 pj_ssize_t bytes_sent);
115 #if PJ_HAS_TCP
116 static void ioqueue_on_accept_complete(pj_ioqueue_key_t *key,
117 pj_ioqueue_op_key_t *op_key,
118 pj_sock_t sock,
119 pj_status_t status);
120 static void ioqueue_on_connect_complete(pj_ioqueue_key_t *key,
121 pj_status_t status);
122 #endif
123
124 PJ_DEF(void) pj_activesock_cfg_default(pj_activesock_cfg *cfg)
125 {
126 pj_bzero(cfg, sizeof(*cfg));
127 cfg->async_cnt = 1;
128 cfg->concurrency = -1;
129 cfg->whole_data = PJ_TRUE;
130 cfg->sock_cloexec = PJ_TRUE;
131 }
132
133 #if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
134 PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0
135 static void activesock_destroy_iphone_os_stream(pj_activesock_t *asock)
136 {
137 if (asock->readStream) {
138 CFReadStreamClose(asock->readStream);
139 CFRelease(asock->readStream);
140 asock->readStream = NULL;
141 }
142 }
143
144 static void activesock_create_iphone_os_stream(pj_activesock_t *asock)
145 {
146 #if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \
147 __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0)
148
149 if (ios_bg_support && asock->bg_setting && asock->stream_oriented) {
150 activesock_destroy_iphone_os_stream(asock);
151
152 CFStreamCreatePairWithSocket(kCFAllocatorDefault, asock->sock,
153 &asock->readStream, NULL);
154
155 if (!asock->readStream ||
156 CFReadStreamSetProperty(asock->readStream,
157 kCFStreamNetworkServiceType,
158 kCFStreamNetworkServiceTypeVoIP)
159 != TRUE ||
160 CFReadStreamOpen(asock->readStream) != TRUE)
161 {
162 PJ_LOG(2,("", "Failed to configure TCP transport for VoIP "
163 "usage. Usage of THIS particular TCP transport in "
164 "background mode will not be supported."));
165
166
167 activesock_destroy_iphone_os_stream(asock);
168 }
169 }
170
171 #endif
172 }
173
174
175 PJ_DEF(void) pj_activesock_set_iphone_os_bg(pj_activesock_t *asock,
176 int val)
177 {
178 asock->bg_setting = val;
179 if (asock->bg_setting)
180 activesock_create_iphone_os_stream(asock);
181 else
182 activesock_destroy_iphone_os_stream(asock);
183 }
184
185 PJ_DEF(void) pj_activesock_enable_iphone_os_bg(pj_bool_t val)
186 {
187 ios_bg_support = val;
188 }
189 #endif
190
191 PJ_DEF(pj_status_t) pj_activesock_create( pj_pool_t *pool,
192 pj_sock_t sock,
193 int sock_type,
194 const pj_activesock_cfg *opt,
195 pj_ioqueue_t *ioqueue,
196 const pj_activesock_cb *cb,
197 void *user_data,
198 pj_activesock_t **p_asock)
199 {
200 pj_activesock_t *asock;
201 pj_ioqueue_callback ioq_cb;
202 pj_status_t status;
203
204 PJ_ASSERT_RETURN(pool && ioqueue && cb && p_asock, PJ_EINVAL);
205 PJ_ASSERT_RETURN(sock>=0 && sock!=PJ_INVALID_SOCKET, PJ_EINVAL);
206 PJ_ASSERT_RETURN((sock_type & 0xF)==pj_SOCK_STREAM() ||
207 (sock_type & 0xF)==pj_SOCK_DGRAM(), PJ_EINVAL);
208 PJ_ASSERT_RETURN(!opt || opt->async_cnt >= 1, PJ_EINVAL);
209
210 asock = PJ_POOL_ZALLOC_T(pool, pj_activesock_t);
211 asock->ioqueue = ioqueue;
212 asock->stream_oriented = ((sock_type & 0xF) == pj_SOCK_STREAM());
213 asock->async_count = (opt? opt->async_cnt : 1);
214 asock->whole_data = (opt? opt->whole_data : 1);
215 asock->max_loop = PJ_ACTIVESOCK_MAX_LOOP;
216 asock->user_data = user_data;
217 pj_memcpy(&asock->cb, cb, sizeof(*cb));
218
219 pj_bzero(&ioq_cb, sizeof(ioq_cb));
220 ioq_cb.on_read_complete = &ioqueue_on_read_complete;
221 ioq_cb.on_write_complete = &ioqueue_on_write_complete;
222 #if PJ_HAS_TCP
223 ioq_cb.on_connect_complete = &ioqueue_on_connect_complete;
224 ioq_cb.on_accept_complete = &ioqueue_on_accept_complete;
225 #endif
226
227 status = pj_ioqueue_register_sock2(pool, ioqueue, sock,
228 (opt? opt->grp_lock : NULL),
229 asock, &ioq_cb, &asock->key);
230 if (status != PJ_SUCCESS) {
231 pj_activesock_close(asock);
232 return status;
233 }
234
235 if (asock->whole_data) {
236 /* Must disable concurrency otherwise there is a race condition */
237 pj_ioqueue_set_concurrency(asock->key, 0);
238 } else if (opt && opt->concurrency >= 0) {
239 pj_ioqueue_set_concurrency(asock->key, opt->concurrency);
240 }
241
242 #if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
243 PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0
244 asock->sock = sock;
245 asock->bg_setting = PJ_ACTIVESOCK_TCP_IPHONE_OS_BG;
246 #endif
247
248 *p_asock = asock;
249 return PJ_SUCCESS;
250 }
251
252
253 PJ_DEF(pj_status_t) pj_activesock_create_udp( pj_pool_t *pool,
254 const pj_sockaddr *addr,
255 const pj_activesock_cfg *opt,
256 pj_ioqueue_t *ioqueue,
257 const pj_activesock_cb *cb,
258 void *user_data,
259 pj_activesock_t **p_asock,
260 pj_sockaddr *bound_addr)
261 {
262 pj_sock_t sock_fd;
263 pj_sockaddr default_addr;
264 pj_status_t status;
265 int sock_type = pj_SOCK_DGRAM();
266
267 if (opt && opt->sock_cloexec)
268 sock_type |= pj_SOCK_CLOEXEC();
269
270 if (addr == NULL) {
271 pj_sockaddr_init(pj_AF_INET(), &default_addr, NULL, 0);
272 addr = &default_addr;
273 }
274
275 status = pj_sock_socket(addr->addr.sa_family, sock_type, 0,
276 &sock_fd);
277 if (status != PJ_SUCCESS) {
278 return status;
279 }
280
281 status = pj_sock_bind(sock_fd, addr, pj_sockaddr_get_len(addr));
282 if (status != PJ_SUCCESS) {
283 pj_sock_close(sock_fd);
284 return status;
285 }
286
287 status = pj_activesock_create(pool, sock_fd, sock_type, opt,
288 ioqueue, cb, user_data, p_asock);
289 if (status != PJ_SUCCESS) {
290 pj_sock_close(sock_fd);
291 return status;
292 }
293
294 if (bound_addr) {
295 int addr_len = sizeof(*bound_addr);
296 status = pj_sock_getsockname(sock_fd, bound_addr, &addr_len);
297 if (status != PJ_SUCCESS) {
298 pj_activesock_close(*p_asock);
299 return status;
300 }
301 }
302
303 return PJ_SUCCESS;
304 }
305
306 PJ_DEF(pj_status_t) pj_activesock_close(pj_activesock_t *asock)
307 {
308 pj_ioqueue_key_t *key;
309 pj_bool_t unregister = PJ_FALSE;
310
311 PJ_ASSERT_RETURN(asock, PJ_EINVAL);
312 asock->shutdown = SHUT_RX | SHUT_TX;
313
314 /* Avoid double unregistration on the key */
315 key = asock->key;
316 if (key) {
317 pj_ioqueue_lock_key(key);
318 unregister = (asock->key != NULL);
319 asock->key = NULL;
320 pj_ioqueue_unlock_key(key);
321 }
322
323 if (unregister) {
324 pj_ioqueue_unregister(key);
325
326 #if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
327 PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0
328 activesock_destroy_iphone_os_stream(asock);
329 #endif
330 }
331 return PJ_SUCCESS;
332 }
333
334
335 PJ_DEF(pj_status_t) pj_activesock_set_user_data( pj_activesock_t *asock,
336 void *user_data)
337 {
338 PJ_ASSERT_RETURN(asock, PJ_EINVAL);
339 asock->user_data = user_data;
340 return PJ_SUCCESS;
341 }
342
343
344 PJ_DEF(void*) pj_activesock_get_user_data(pj_activesock_t *asock)
345 {
346 PJ_ASSERT_RETURN(asock, NULL);
347 return asock->user_data;
348 }
349
350
351 PJ_DEF(pj_status_t) pj_activesock_start_read(pj_activesock_t *asock,
352 pj_pool_t *pool,
353 unsigned buff_size,
354 pj_uint32_t flags)
355 {
356 void **readbuf;
357 unsigned i;
358
359 PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL);
360
361 readbuf = (void**) pj_pool_calloc(pool, asock->async_count,
362 sizeof(void*));
363
364 for (i=0; i<asock->async_count; ++i) {
365 readbuf[i] = pj_pool_alloc(pool, buff_size);
366 }
367
368 return pj_activesock_start_read2(asock, pool, buff_size, readbuf, flags);
369 }
370
371
372 PJ_DEF(pj_status_t) pj_activesock_start_read2( pj_activesock_t *asock,
373 pj_pool_t *pool,
374 unsigned buff_size,
375 void *readbuf[],
376 pj_uint32_t flags)
377 {
378 unsigned i;
379 pj_status_t status;
380
381 PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL);
382 PJ_ASSERT_RETURN(asock->read_type == TYPE_NONE, PJ_EINVALIDOP);
383 PJ_ASSERT_RETURN(asock->read_op == NULL, PJ_EINVALIDOP);
384
385 asock->read_op = (struct read_op*)
386 pj_pool_calloc(pool, asock->async_count,
387 sizeof(struct read_op));
388 asock->read_type = TYPE_RECV;
389 asock->read_flags = flags;
390
391 for (i=0; i<asock->async_count; ++i) {
392 struct read_op *r = &asock->read_op[i];
393 pj_ssize_t size_to_read;
394
395 r->pkt = (pj_uint8_t*)readbuf[i];
396 size_to_read = r->max_size = buff_size;
397
398 status = pj_ioqueue_recv(asock->key, &r->op_key, r->pkt, &size_to_read,
399 PJ_IOQUEUE_ALWAYS_ASYNC | flags);
400 PJ_ASSERT_RETURN(status != PJ_SUCCESS, PJ_EBUG);
401
402 if (status != PJ_EPENDING)
403 return status;
404 }
405
406 return PJ_SUCCESS;
407 }
408
409
410 PJ_DEF(pj_status_t) pj_activesock_start_recvfrom(pj_activesock_t *asock,
411 pj_pool_t *pool,
412 unsigned buff_size,
413 pj_uint32_t flags)
414 {
415 void **readbuf;
416 unsigned i;
417
418 PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL);
419
420 readbuf = (void**) pj_pool_calloc(pool, asock->async_count,
421 sizeof(void*));
422
423 for (i=0; i<asock->async_count; ++i) {
424 readbuf[i] = pj_pool_alloc(pool, buff_size);
425 }
426
427 return pj_activesock_start_recvfrom2(asock, pool, buff_size,
428 readbuf, flags);
429 }
430
431
432 PJ_DEF(pj_status_t) pj_activesock_start_recvfrom2( pj_activesock_t *asock,
433 pj_pool_t *pool,
434 unsigned buff_size,
435 void *readbuf[],
436 pj_uint32_t flags)
437 {
438 unsigned i;
439 pj_status_t status;
440
441 PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL);
442 PJ_ASSERT_RETURN(asock->read_type == TYPE_NONE, PJ_EINVALIDOP);
443
444 asock->read_op = (struct read_op*)
445 pj_pool_calloc(pool, asock->async_count,
446 sizeof(struct read_op));
447 asock->read_type = TYPE_RECV_FROM;
448 asock->read_flags = flags;
449
450 for (i=0; i<asock->async_count; ++i) {
451 struct read_op *r = &asock->read_op[i];
452 pj_ssize_t size_to_read;
453
454 r->pkt = (pj_uint8_t*) readbuf[i];
455 size_to_read = r->max_size = buff_size;
456 r->src_addr_len = sizeof(r->src_addr);
457
458 status = pj_ioqueue_recvfrom(asock->key, &r->op_key, r->pkt,
459 &size_to_read,
460 PJ_IOQUEUE_ALWAYS_ASYNC | flags,
461 &r->src_addr, &r->src_addr_len);
462 PJ_ASSERT_RETURN(status != PJ_SUCCESS, PJ_EBUG);
463
464 if (status != PJ_EPENDING)
465 return status;
466 }
467
468 return PJ_SUCCESS;
469 }
470
471
472 static void ioqueue_on_read_complete(pj_ioqueue_key_t *key,
473 pj_ioqueue_op_key_t *op_key,
474 pj_ssize_t bytes_read)
475 {
476 pj_activesock_t *asock;
477 struct read_op *r = (struct read_op*)op_key;
478 unsigned loop = 0;
479 pj_status_t status;
480
481 asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key);
482
483 /* Ignore if we've been shutdown */
484 if (asock->shutdown & SHUT_RX)
485 return;
486
487 do {
488 unsigned flags;
489
490 if (bytes_read > 0) {
491 /*
492 * We've got new data.
493 */
494 pj_size_t remainder;
495 pj_bool_t ret;
496
497 /* Append this new data to existing data. If socket is stream
498 * oriented, user might have left some data in the buffer.
499 * Otherwise if socket is datagram there will be nothing in
500 * existing packet hence the packet will contain only the new
501 * packet.
502 */
503 r->size += bytes_read;
504
505 /* Set default remainder to zero */
506 remainder = 0;
507
508 /* And return value to TRUE */
509 ret = PJ_TRUE;
510
511 /* Notify callback */
512 if (asock->read_type == TYPE_RECV && asock->cb.on_data_read) {
513 ret = (*asock->cb.on_data_read)(asock, r->pkt, r->size,
514 PJ_SUCCESS, &remainder);
515 PJ_ASSERT_ON_FAIL(
516 !ret || !asock->stream_oriented || remainder <= r->size,
517 {
518 PJ_LOG(2, ("",
519 "App bug! Invalid remainder length from "
520 "activesock on_data_read()."));
521 remainder = 0;
522 });
523 } else if (asock->read_type == TYPE_RECV_FROM &&
524 asock->cb.on_data_recvfrom)
525 {
526 ret = (*asock->cb.on_data_recvfrom)(asock, r->pkt, r->size,
527 &r->src_addr,
528 r->src_addr_len,
529 PJ_SUCCESS);
530 }
531
532 /* If callback returns false, we have been destroyed! */
533 if (!ret)
534 return;
535
536 /* Only stream oriented socket may leave data in the packet */
537 if (asock->stream_oriented) {
538 r->size = remainder;
539 } else {
540 r->size = 0;
541 }
542
543 } else if (bytes_read <= 0 &&
544 -bytes_read != PJ_STATUS_FROM_OS(OSERR_EWOULDBLOCK) &&
545 -bytes_read != PJ_STATUS_FROM_OS(OSERR_EINPROGRESS) &&
546 (asock->stream_oriented ||
547 -bytes_read != PJ_STATUS_FROM_OS(OSERR_ECONNRESET)))
548 {
549 pj_size_t remainder;
550 pj_bool_t ret;
551
552 if (bytes_read == 0) {
553 /* For stream/connection oriented socket, this means the
554 * connection has been closed. For datagram sockets, it means
555 * we've received datagram with zero length.
556 */
557 if (asock->stream_oriented)
558 status = PJ_EEOF;
559 else
560 status = PJ_SUCCESS;
561 } else {
562 /* This means we've got an error. If this is stream/connection
563 * oriented, it means connection has been closed. For datagram
564 * sockets, it means we've got some error (e.g. EWOULDBLOCK).
565 */
566 status = (pj_status_t)-bytes_read;
567 }
568
569 /* Set default remainder to zero */
570 remainder = 0;
571
572 /* And return value to TRUE */
573 ret = PJ_TRUE;
574
575 /* Notify callback */
576 if (asock->read_type == TYPE_RECV && asock->cb.on_data_read) {
577 /* For connection oriented socket, we still need to report
578 * the remainder data (if any) to the user to let user do
579 * processing with the remainder data before it closes the
580 * connection.
581 * If there is no remainder data, set the packet to NULL.
582 */
583
584 /* Shouldn't set the packet to NULL, as there may be active
585 * socket user, such as SSL socket, that needs to have access
586 * to the read buffer packet.
587 */
588 //ret = (*asock->cb.on_data_read)(asock, (r->size? r->pkt:NULL),
589 // r->size, status, &remainder);
590 ret = (*asock->cb.on_data_read)(asock, r->pkt, r->size,
591 status, &remainder);
592 PJ_ASSERT_ON_FAIL(
593 !ret || !asock->stream_oriented || remainder <= r->size,
594 {
595 PJ_LOG(2, ("",
596 "App bug! Invalid remainder length from "
597 "activesock on_data_read()."));
598 remainder = 0;
599 });
600
601 } else if (asock->read_type == TYPE_RECV_FROM &&
602 asock->cb.on_data_recvfrom)
603 {
604 /* This would always be datagram oriented hence there's
605 * nothing in the packet. We can't be sure if there will be
606 * anything useful in the source_addr, so just put NULL
607 * there too.
608 */
609 /* In some scenarios, status may be PJ_SUCCESS. The upper
610 * layer application may not expect the callback to be called
611 * with successful status and NULL data, so lets not call the
612 * callback if the status is PJ_SUCCESS.
613 */
614 if (status != PJ_SUCCESS ) {
615 ret = (*asock->cb.on_data_recvfrom)(asock, NULL, 0,
616 NULL, 0, status);
617 }
618 }
619
620 /* If callback returns false, we have been destroyed! */
621 if (!ret)
622 return;
623
624 /* Also stop further read if we've been shutdown */
625 if (asock->shutdown & SHUT_RX)
626 return;
627
628 /* Only stream oriented socket may leave data in the packet */
629 if (asock->stream_oriented) {
630 r->size = remainder;
631 } else {
632 r->size = 0;
633 }
634 }
635
636 /* Read next data. We limit ourselves to processing max_loop immediate
637 * data, so when the loop counter has exceeded this value, force the
638 * read()/recvfrom() to return pending operation to allow the program
639 * to do other jobs.
640 */
641 bytes_read = r->max_size - r->size;
642 flags = asock->read_flags;
643 if (++loop >= asock->max_loop)
644 flags |= PJ_IOQUEUE_ALWAYS_ASYNC;
645
646 if (asock->read_type == TYPE_RECV) {
647 status = pj_ioqueue_recv(key, op_key, r->pkt + r->size,
648 &bytes_read, flags);
649 } else {
650 r->src_addr_len = sizeof(r->src_addr);
651 status = pj_ioqueue_recvfrom(key, op_key, r->pkt + r->size,
652 &bytes_read, flags,
653 &r->src_addr, &r->src_addr_len);
654 }
655
656 if (status == PJ_SUCCESS) {
657 /* Immediate data */
658 ;
659 } else if (status != PJ_EPENDING && status != PJ_ECANCELLED) {
660 /* Error */
661 bytes_read = -status;
662 } else {
663 break;
664 }
665 } while (1);
666
667 }
668
669
670 static pj_status_t send_remaining(pj_activesock_t *asock,
671 pj_ioqueue_op_key_t *send_key)
672 {
673 struct send_data *sd = (struct send_data*)send_key->activesock_data;
674 pj_status_t status;
675
676 do {
677 pj_ssize_t size;
678
679 size = sd->len - sd->sent;
680 status = pj_ioqueue_send(asock->key, send_key,
681 sd->data+sd->sent, &size, sd->flags);
682 if (status != PJ_SUCCESS) {
683 /* Pending or error */
684 break;
685 }
686
687 sd->sent += size;
688 if (sd->sent == sd->len) {
689 /* The whole data has been sent. */
690 return PJ_SUCCESS;
691 }
692
693 } while (sd->sent < sd->len);
694
695 return status;
696 }
697
698
699 PJ_DEF(pj_status_t) pj_activesock_send( pj_activesock_t *asock,
700 pj_ioqueue_op_key_t *send_key,
701 const void *data,
702 pj_ssize_t *size,
703 unsigned flags)
704 {
705 PJ_ASSERT_RETURN(asock && send_key && data && size, PJ_EINVAL);
706
707 if (asock->shutdown & SHUT_TX)
708 return PJ_EINVALIDOP;
709
710 send_key->activesock_data = NULL;
711
712 if (asock->whole_data) {
713 pj_ssize_t whole;
714 pj_status_t status;
715
716 whole = *size;
717
718 status = pj_ioqueue_send(asock->key, send_key, data, size, flags);
719 if (status != PJ_SUCCESS) {
720 /* Pending or error */
721 return status;
722 }
723
724 if (*size == whole) {
725 /* The whole data has been sent. */
726 return PJ_SUCCESS;
727 }
728
729 /* Data was partially sent */
730 asock->send_data.data = (pj_uint8_t*)data;
731 asock->send_data.len = whole;
732 asock->send_data.sent = *size;
733 asock->send_data.flags = flags;
734 send_key->activesock_data = &asock->send_data;
735
736 /* Try again */
737 status = send_remaining(asock, send_key);
738 if (status == PJ_SUCCESS) {
739 *size = whole;
740 }
741 return status;
742
743 } else {
744 return pj_ioqueue_send(asock->key, send_key, data, size, flags);
745 }
746 }
747
748
749 PJ_DEF(pj_status_t) pj_activesock_sendto( pj_activesock_t *asock,
750 pj_ioqueue_op_key_t *send_key,
751 const void *data,
752 pj_ssize_t *size,
753 unsigned flags,
754 const pj_sockaddr_t *addr,
755 int addr_len)
756 {
757 PJ_ASSERT_RETURN(asock && send_key && data && size && addr && addr_len,
758 PJ_EINVAL);
759
760 if (asock->shutdown & SHUT_TX)
761 return PJ_EINVALIDOP;
762
763 return pj_ioqueue_sendto(asock->key, send_key, data, size, flags,
764 addr, addr_len);
765 }
766
767
768 static void ioqueue_on_write_complete(pj_ioqueue_key_t *key,
769 pj_ioqueue_op_key_t *op_key,
770 pj_ssize_t bytes_sent)
771 {
772 pj_activesock_t *asock;
773
774 asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key);
775
776 /* If we've been shutdown, still invoke the callback so upper layers
777 * can release resources (e.g., pjsip_tx_data_dec_ref). See #4878.
778 *
779 * Note: asock->key may be NULL at this point (cleared by
780 * pj_activesock_close before pj_ioqueue_unregister). The callback
781 * must not attempt further I/O on this active socket.
782 */
783 if (asock->shutdown & SHUT_TX) {
784 if (asock->cb.on_data_sent)
785 (*asock->cb.on_data_sent)(asock, op_key, bytes_sent);
786 return;
787 }
788
789 if (bytes_sent > 0 && op_key->activesock_data) {
790 /* whole_data is requested. Make sure we send all the data */
791 struct send_data *sd = (struct send_data*)op_key->activesock_data;
792
793 sd->sent += bytes_sent;
794 if (sd->sent == sd->len) {
795 /* all has been sent */
796 bytes_sent = sd->sent;
797 op_key->activesock_data = NULL;
798 } else {
799 /* send remaining data */
800 pj_status_t status;
801
802 status = send_remaining(asock, op_key);
803 if (status == PJ_EPENDING)
804 return;
805 else if (status == PJ_SUCCESS)
806 bytes_sent = sd->sent;
807 else
808 bytes_sent = -status;
809
810 op_key->activesock_data = NULL;
811 }
812 }
813
814 if (asock->cb.on_data_sent) {
815 pj_bool_t ret;
816
817 ret = (*asock->cb.on_data_sent)(asock, op_key, bytes_sent);
818
819 /* If callback returns false, we have been destroyed! */
820 if (!ret)
821 return;
822 }
823 }
824
825 #if PJ_HAS_TCP
826 PJ_DEF(pj_status_t) pj_activesock_start_accept(pj_activesock_t *asock,
827 pj_pool_t *pool)
828 {
829 unsigned i;
830
831 PJ_ASSERT_RETURN(asock, PJ_EINVAL);
832 PJ_ASSERT_RETURN(asock->accept_op==NULL, PJ_EINVALIDOP);
833
834 /* Ignore if we've been shutdown */
835 if (asock->shutdown)
836 return PJ_EINVALIDOP;
837
838 asock->accept_op = (struct accept_op*)
839 pj_pool_calloc(pool, asock->async_count,
840 sizeof(struct accept_op));
841 for (i=0; i<asock->async_count; ++i) {
842 struct accept_op *a = &asock->accept_op[i];
843 pj_status_t status;
844
845 do {
846 a->new_sock = PJ_INVALID_SOCKET;
847 a->rem_addr_len = sizeof(a->rem_addr);
848
849 status = pj_ioqueue_accept(asock->key, &a->op_key, &a->new_sock,
850 NULL, &a->rem_addr, &a->rem_addr_len);
851 if (status == PJ_SUCCESS) {
852 /* We've got immediate connection. Not sure if it's a good
853 * idea to call the callback now (probably application will
854 * not be prepared to process it), so lets just silently
855 * close the socket.
856 */
857 pj_sock_close(a->new_sock);
858 }
859 } while (status == PJ_SUCCESS);
860
861 if (status != PJ_EPENDING) {
862 return status;
863 }
864 }
865
866 return PJ_SUCCESS;
867 }
868
869
870 static void ioqueue_on_accept_complete(pj_ioqueue_key_t *key,
871 pj_ioqueue_op_key_t *op_key,
872 pj_sock_t new_sock,
873 pj_status_t status)
874 {
875 pj_activesock_t *asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key);
876 struct accept_op *accept_op = (struct accept_op*) op_key;
877
878 PJ_UNUSED_ARG(new_sock);
879
880 /* Ignore if we've been shutdown */
881 if (asock->shutdown)
882 return;
883
884 do {
885 if (status == asock->last_err && status != PJ_SUCCESS) {
886 asock->err_counter++;
887 if (asock->err_counter >= PJ_ACTIVESOCK_MAX_CONSECUTIVE_ACCEPT_ERROR) {
888 PJ_LOG(3, ("", "Received %d consecutive errors: %d for the accept()"
889 " operation, stopping further ioqueue accepts.",
890 asock->err_counter, asock->last_err));
891
892 if ((status == PJ_STATUS_FROM_OS(OSERR_EWOULDBLOCK)) &&
893 (asock->cb.on_accept_complete2))
894 {
895 (*asock->cb.on_accept_complete2)(asock,
896 accept_op->new_sock,
897 &accept_op->rem_addr,
898 accept_op->rem_addr_len,
899 PJ_ESOCKETSTOP);
900 }
901 return;
902 }
903 } else {
904 asock->err_counter = 0;
905 asock->last_err = status;
906 }
907
908 if (status==PJ_SUCCESS && (asock->cb.on_accept_complete2 ||
909 asock->cb.on_accept_complete)) {
910 pj_bool_t ret;
911
912 /* Notify callback */
913 if (asock->cb.on_accept_complete2) {
914 ret = (*asock->cb.on_accept_complete2)(asock,
915 accept_op->new_sock,
916 &accept_op->rem_addr,
917 accept_op->rem_addr_len,
918 status);
919 } else {
920 ret = (*asock->cb.on_accept_complete)(asock,
921 accept_op->new_sock,
922 &accept_op->rem_addr,
923 accept_op->rem_addr_len);
924 }
925
926 /* If callback returns false, we have been destroyed! */
927 if (!ret)
928 return;
929
930 #if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
931 PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0
932 activesock_create_iphone_os_stream(asock);
933 #endif
934 } else if (status==PJ_SUCCESS) {
935 /* Application doesn't handle the new socket, we need to
936 * close it to avoid resource leak.
937 */
938 pj_sock_close(accept_op->new_sock);
939 }
940
941 /* Don't start another accept() if we've been shutdown */
942 if (asock->shutdown)
943 return;
944
945 /* Prepare next accept() */
946 accept_op->new_sock = PJ_INVALID_SOCKET;
947 accept_op->rem_addr_len = sizeof(accept_op->rem_addr);
948
949 status = pj_ioqueue_accept(asock->key, op_key, &accept_op->new_sock,
950 NULL, &accept_op->rem_addr,
951 &accept_op->rem_addr_len);
952
953 } while (status != PJ_EPENDING && status != PJ_ECANCELLED);
954 }
955
956
957 PJ_DEF(pj_status_t) pj_activesock_start_connect( pj_activesock_t *asock,
958 pj_pool_t *pool,
959 const pj_sockaddr_t *remaddr,
960 int addr_len)
961 {
962 PJ_UNUSED_ARG(pool);
963
964 if (asock->shutdown)
965 return PJ_EINVALIDOP;
966
967 return pj_ioqueue_connect(asock->key, remaddr, addr_len);
968 }
969
970 static void ioqueue_on_connect_complete(pj_ioqueue_key_t *key,
971 pj_status_t status)
972 {
973 pj_activesock_t *asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key);
974
975 /* Ignore if we've been shutdown */
976 if (asock->shutdown)
977 return;
978
979 if (asock->cb.on_connect_complete) {
980 pj_bool_t ret;
981
982 ret = (*asock->cb.on_connect_complete)(asock, status);
983
984 if (!ret) {
985 /* We've been destroyed */
986 return;
987 }
988
989 #if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
990 PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0
991 activesock_create_iphone_os_stream(asock);
992 #endif
993
994 }
995 }
996 #endif /* PJ_HAS_TCP */
997