Debian Codegraph @

Xpjproject/pjlib/src/pj/sock_qos_bsd.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pj/sock_qos.h>
19 #include <pj/assert.h>
20 #include <pj/errno.h>
21 #include <pj/string.h>
22
23 /* This is the implementation of QoS with BSD socket's setsockopt(),
24 * using IP_TOS/IPV6_TCLASS and SO_PRIORITY
25 */
26 #if !defined(PJ_QOS_IMPLEMENTATION) || PJ_QOS_IMPLEMENTATION==PJ_QOS_BSD
27
28 PJ_DEF(pj_status_t) pj_sock_set_qos_params(pj_sock_t sock,
29 pj_qos_params *param)
30 {
31 pj_status_t last_err = PJ_ENOTSUP;
32 pj_status_t status;
33
34 /* No op? */
35 if (!param->flags)
36 return PJ_SUCCESS;
37
38 /* Clear WMM field since we don't support it */
39 param->flags &= ~(PJ_QOS_PARAM_HAS_WMM);
40
41 /* Set TOS/DSCP */
42 if (param->flags & PJ_QOS_PARAM_HAS_DSCP) {
43 /* We need to know if the socket is IPv4 or IPv6 */
44 pj_sockaddr sa;
45 int salen = sizeof(sa);
46
47 /* Value is dscp_val << 2 */
48 int val = (param->dscp_val << 2);
49
50 status = pj_sock_getsockname(sock, &sa, &salen);
51 if (status != PJ_SUCCESS)
52 return status;
53
54 if (sa.addr.sa_family == pj_AF_INET()) {
55 /* In IPv4, the DS field goes in the TOS field */
56 status = pj_sock_setsockopt(sock, pj_SOL_IP(), pj_IP_TOS(),
57 &val, sizeof(val));
58 } else if (sa.addr.sa_family == pj_AF_INET6()) {
59 /* In IPv6, the DS field goes in the Traffic Class field */
60 status = pj_sock_setsockopt(sock, pj_SOL_IPV6(),
61 pj_IPV6_TCLASS(),
62 &val, sizeof(val));
63 } else {
64 status = PJ_EINVAL;
65 }
66
67 if (status != PJ_SUCCESS) {
68 param->flags &= ~(PJ_QOS_PARAM_HAS_DSCP);
69 last_err = status;
70 }
71 }
72
73 /* Set SO_PRIORITY */
74 if (param->flags & PJ_QOS_PARAM_HAS_SO_PRIO) {
75 int val = param->so_prio;
76 status = pj_sock_setsockopt(sock, pj_SOL_SOCKET(), pj_SO_PRIORITY(),
77 &val, sizeof(val));
78 if (status != PJ_SUCCESS) {
79 param->flags &= ~(PJ_QOS_PARAM_HAS_SO_PRIO);
80 last_err = status;
81 }
82 }
83
84 return param->flags ? PJ_SUCCESS : last_err;
85 }
86
87 PJ_DEF(pj_status_t) pj_sock_set_qos_type(pj_sock_t sock,
88 pj_qos_type type)
89 {
90 pj_qos_params param;
91 pj_status_t status;
92
93 status = pj_qos_get_params(type, &param);
94 if (status != PJ_SUCCESS)
95 return status;
96
97 return pj_sock_set_qos_params(sock, &param);
98 }
99
100
101 PJ_DEF(pj_status_t) pj_sock_get_qos_params(pj_sock_t sock,
102 pj_qos_params *p_param)
103 {
104 pj_status_t last_err = PJ_ENOTSUP;
105 int val = 0, optlen;
106 pj_sockaddr sa;
107 int salen = sizeof(sa);
108 pj_status_t status;
109
110 pj_bzero(p_param, sizeof(*p_param));
111
112 /* Get DSCP/TOS value */
113 status = pj_sock_getsockname(sock, &sa, &salen);
114 if (status == PJ_SUCCESS) {
115 optlen = sizeof(val);
116 if (sa.addr.sa_family == pj_AF_INET()) {
117 status = pj_sock_getsockopt(sock, pj_SOL_IP(), pj_IP_TOS(),
118 &val, &optlen);
119 } else if (sa.addr.sa_family == pj_AF_INET6()) {
120 status = pj_sock_getsockopt(sock, pj_SOL_IPV6(),
121 pj_IPV6_TCLASS(),
122 &val, &optlen);
123 } else {
124 status = PJ_EINVAL;
125 }
126
127 if (status == PJ_SUCCESS) {
128 p_param->flags |= PJ_QOS_PARAM_HAS_DSCP;
129 p_param->dscp_val = (pj_uint8_t)(val >> 2);
130 } else {
131 last_err = status;
132 }
133 } else {
134 last_err = status;
135 }
136
137 /* Get SO_PRIORITY */
138 optlen = sizeof(val);
139 status = pj_sock_getsockopt(sock, pj_SOL_SOCKET(), pj_SO_PRIORITY(),
140 &val, &optlen);
141 if (status == PJ_SUCCESS) {
142 p_param->flags |= PJ_QOS_PARAM_HAS_SO_PRIO;
143 p_param->so_prio = (pj_uint8_t)val;
144 } else {
145 last_err = status;
146 }
147
148 /* WMM is not supported */
149
150 return p_param->flags ? PJ_SUCCESS : last_err;
151 }
152
153 PJ_DEF(pj_status_t) pj_sock_get_qos_type(pj_sock_t sock,
154 pj_qos_type *p_type)
155 {
156 pj_qos_params param;
157 pj_status_t status;
158
159 status = pj_sock_get_qos_params(sock, &param);
160 if (status != PJ_SUCCESS)
161 return status;
162
163 return pj_qos_get_type(&param, p_type);
164 }
165
166 #endif /* PJ_QOS_IMPLEMENTATION */
167