Coverage Report

Created: 2025-08-28 07:07

/src/pjsip/pjlib/src/pj/sock_select.c
Line
Count
Source (jump to first uncovered line)
1
/* 
2
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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/sock_select.h>
20
#include <pj/compat/socket.h>
21
#include <pj/os.h>
22
#include <pj/assert.h>
23
#include <pj/errno.h>
24
25
#if defined(PJ_HAS_STRING_H) && PJ_HAS_STRING_H!=0
26
#   include <string.h>
27
#endif
28
29
#if defined(PJ_HAS_SYS_TIME_H) && PJ_HAS_SYS_TIME_H!=0
30
#   include <sys/time.h>
31
#endif
32
33
#ifdef _MSC_VER
34
#   pragma warning(disable: 4018)    // Signed/unsigned mismatch in FD_*
35
#   pragma warning(disable: 4389)    // Signed/unsigned mismatch in FD_*
36
#endif
37
38
0
#define PART_FDSET(ps)          ((fd_set*)&ps->data[1])
39
0
#define PART_FDSET_OR_NULL(ps)  (ps ? PART_FDSET(ps) : NULL)
40
0
#define PART_COUNT(ps)          (ps->data[0])
41
42
PJ_DEF(void) PJ_FD_ZERO(pj_fd_set_t *fdsetp)
43
0
{
44
0
    PJ_CHECK_STACK();
45
0
    pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set));
46
47
0
    FD_ZERO(PART_FDSET(fdsetp));
48
0
    PART_COUNT(fdsetp) = 0;
49
0
}
50
51
52
PJ_DEF(void) PJ_FD_SET(pj_sock_t fd, pj_fd_set_t *fdsetp)
53
0
{
54
0
    PJ_CHECK_STACK();
55
0
    pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set));
56
57
0
    if (!PJ_FD_ISSET(fd, fdsetp))
58
0
        ++PART_COUNT(fdsetp);
59
0
    FD_SET(fd, PART_FDSET(fdsetp));
60
0
}
61
62
63
PJ_DEF(void) PJ_FD_CLR(pj_sock_t fd, pj_fd_set_t *fdsetp)
64
0
{
65
0
    PJ_CHECK_STACK();
66
0
    pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set));
67
68
0
    if (PJ_FD_ISSET(fd, fdsetp))
69
0
        --PART_COUNT(fdsetp);
70
0
    FD_CLR(fd, PART_FDSET(fdsetp));
71
0
}
72
73
74
PJ_DEF(pj_bool_t) PJ_FD_ISSET(pj_sock_t fd, const pj_fd_set_t *fdsetp)
75
0
{
76
0
    PJ_CHECK_STACK();
77
0
    PJ_ASSERT_RETURN(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set),
78
0
                     0);
79
80
0
    return FD_ISSET(fd, PART_FDSET(fdsetp));
81
0
}
82
83
PJ_DEF(pj_size_t) PJ_FD_COUNT(const pj_fd_set_t *fdsetp)
84
0
{
85
0
    return PART_COUNT(fdsetp);
86
0
}
87
88
PJ_DEF(int) pj_sock_select( int n, 
89
                            pj_fd_set_t *readfds, 
90
                            pj_fd_set_t *writefds,
91
                            pj_fd_set_t *exceptfds, 
92
                            const pj_time_val *timeout)
93
0
{
94
0
    struct timeval os_timeout, *p_os_timeout;
95
96
0
    PJ_CHECK_STACK();
97
98
0
    PJ_ASSERT_RETURN(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set),
99
0
                     PJ_EBUG);
100
101
0
    if (timeout) {
102
0
        os_timeout.tv_sec = timeout->sec;
103
0
        os_timeout.tv_usec = timeout->msec * 1000;
104
0
        p_os_timeout = &os_timeout;
105
0
    } else {
106
0
        p_os_timeout = NULL;
107
0
    }
108
109
0
    return select(n, PART_FDSET_OR_NULL(readfds), PART_FDSET_OR_NULL(writefds),
110
0
                  PART_FDSET_OR_NULL(exceptfds), p_os_timeout);
111
0
}
112