1
/* $Id: sock_qos_bsd.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
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.
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.
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
19
#include <pj/sock_qos.h>
20
#include <pj/assert.h>
22
#include <pj/string.h>
24
/* This is the implementation of QoS with BSD socket's setsockopt(),
25
* using IP_TOS and SO_PRIORITY
27
#if !defined(PJ_QOS_IMPLEMENTATION) || PJ_QOS_IMPLEMENTATION==PJ_QOS_BSD
29
PJ_DEF(pj_status_t) pj_sock_set_qos_params(pj_sock_t sock,
32
pj_status_t last_err = PJ_ENOTSUP;
39
/* Clear WMM field since we don't support it */
40
param->flags &= ~(PJ_QOS_PARAM_HAS_WMM);
43
if (param->flags & PJ_QOS_PARAM_HAS_DSCP) {
44
/* Value is dscp_val << 2 */
45
int val = (param->dscp_val << 2);
46
status = pj_sock_setsockopt(sock, pj_SOL_IP(), pj_IP_TOS(),
48
if (status != PJ_SUCCESS) {
49
param->flags &= ~(PJ_QOS_PARAM_HAS_DSCP);
55
if (param->flags & PJ_QOS_PARAM_HAS_SO_PRIO) {
56
int val = param->so_prio;
57
status = pj_sock_setsockopt(sock, pj_SOL_SOCKET(), pj_SO_PRIORITY(),
59
if (status != PJ_SUCCESS) {
60
param->flags &= ~(PJ_QOS_PARAM_HAS_SO_PRIO);
65
return param->flags ? PJ_SUCCESS : last_err;
68
PJ_DEF(pj_status_t) pj_sock_set_qos_type(pj_sock_t sock,
74
status = pj_qos_get_params(type, ¶m);
75
if (status != PJ_SUCCESS)
78
return pj_sock_set_qos_params(sock, ¶m);
82
PJ_DEF(pj_status_t) pj_sock_get_qos_params(pj_sock_t sock,
83
pj_qos_params *p_param)
85
pj_status_t last_err = PJ_ENOTSUP;
89
pj_bzero(p_param, sizeof(*p_param));
91
/* Get DSCP/TOS value */
93
status = pj_sock_getsockopt(sock, pj_SOL_IP(), pj_IP_TOS(),
95
if (status == PJ_SUCCESS) {
96
p_param->flags |= PJ_QOS_PARAM_HAS_DSCP;
97
p_param->dscp_val = (pj_uint8_t)(val >> 2);
102
/* Get SO_PRIORITY */
103
optlen = sizeof(val);
104
status = pj_sock_getsockopt(sock, pj_SOL_SOCKET(), pj_SO_PRIORITY(),
106
if (status == PJ_SUCCESS) {
107
p_param->flags |= PJ_QOS_PARAM_HAS_SO_PRIO;
108
p_param->so_prio = (pj_uint8_t)val;
113
/* WMM is not supported */
115
return p_param->flags ? PJ_SUCCESS : last_err;
118
PJ_DEF(pj_status_t) pj_sock_get_qos_type(pj_sock_t sock,
124
status = pj_sock_get_qos_params(sock, ¶m);
125
if (status != PJ_SUCCESS)
128
return pj_qos_get_type(¶m, p_type);
131
#endif /* PJ_QOS_IMPLEMENTATION */