1 /* dscp lookup routines lifted wholesale from openssh */
2
3 /*
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Copyright (c) 2005,2006 Damien Miller. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <stdlib.h>
32 #include <inttypes.h>
33
34 #ifdef WIN32
35 #define strcasecmp(a,b) _stricmp(a,b)
36 #define snprintf _snprintf
37 #endif
38
39 int parse_qos(const char *cp);
40 const char * iptos2str(int iptos);
41
42 /*
43 * Definitions for IP type of service (ip_tos)
44 */
45
46 #ifdef HAVE_NETINET_IN_SYSTM_H
47 #include <netinet/in_systm.h>
48 #endif
49 #ifdef HAVE_NETINET_IP_H
50 #include <netinet/ip.h>
51 #endif
52
53 #ifndef IPTOS_LOWDELAY
54 # define IPTOS_LOWDELAY 0x10
55 # define IPTOS_THROUGHPUT 0x08
56 # define IPTOS_RELIABILITY 0x04
57 # define IPTOS_LOWCOST 0x02
58 # define IPTOS_MINCOST IPTOS_LOWCOST
59 #endif /* IPTOS_LOWDELAY */
60
61 /*
62 * Definitions for DiffServ Codepoints as per RFC2474
63 */
64 #ifndef IPTOS_DSCP_AF11
65 # define IPTOS_DSCP_AF11 0x28
66 # define IPTOS_DSCP_AF12 0x30
67 # define IPTOS_DSCP_AF13 0x38
68 # define IPTOS_DSCP_AF21 0x48
69 # define IPTOS_DSCP_AF22 0x50
70 # define IPTOS_DSCP_AF23 0x58
71 # define IPTOS_DSCP_AF31 0x68
72 # define IPTOS_DSCP_AF32 0x70
73 # define IPTOS_DSCP_AF33 0x78
74 # define IPTOS_DSCP_AF41 0x88
75 # define IPTOS_DSCP_AF42 0x90
76 # define IPTOS_DSCP_AF43 0x98
77 # define IPTOS_DSCP_EF 0xb8
78 #endif /* IPTOS_DSCP_AF11 */
79
80 #ifndef IPTOS_DSCP_CS0
81 # define IPTOS_DSCP_CS0 0x00
82 # define IPTOS_DSCP_CS1 0x20
83 # define IPTOS_DSCP_CS2 0x40
84 # define IPTOS_DSCP_CS3 0x60
85 # define IPTOS_DSCP_CS4 0x80
86 # define IPTOS_DSCP_CS5 0xa0
87 # define IPTOS_DSCP_CS6 0xc0
88 # define IPTOS_DSCP_CS7 0xe0
89 #endif /* IPTOS_DSCP_CS0 */
90 #ifndef IPTOS_DSCP_EF
91 # define IPTOS_DSCP_EF 0xb8
92 #endif /* IPTOS_DSCP_EF */
93
94 static const struct {
95 const char *name;
96 int value;
97 } ipqos[] = {
98 { "af11", IPTOS_DSCP_AF11 },
99 { "af12", IPTOS_DSCP_AF12 },
100 { "af13", IPTOS_DSCP_AF13 },
101 { "af21", IPTOS_DSCP_AF21 },
102 { "af22", IPTOS_DSCP_AF22 },
103 { "af23", IPTOS_DSCP_AF23 },
104 { "af31", IPTOS_DSCP_AF31 },
105 { "af32", IPTOS_DSCP_AF32 },
106 { "af33", IPTOS_DSCP_AF33 },
107 { "af41", IPTOS_DSCP_AF41 },
108 { "af42", IPTOS_DSCP_AF42 },
109 { "af43", IPTOS_DSCP_AF43 },
110 { "cs0", IPTOS_DSCP_CS0 },
111 { "cs1", IPTOS_DSCP_CS1 },
112 { "cs2", IPTOS_DSCP_CS2 },
113 { "cs3", IPTOS_DSCP_CS3 },
114 { "cs4", IPTOS_DSCP_CS4 },
115 { "cs5", IPTOS_DSCP_CS5 },
116 { "cs6", IPTOS_DSCP_CS6 },
117 { "cs7", IPTOS_DSCP_CS7 },
118 { "ef", IPTOS_DSCP_EF },
119 { "lowdelay", IPTOS_LOWDELAY },
120 { "throughput", IPTOS_THROUGHPUT },
121 { "reliability", IPTOS_RELIABILITY },
122 { NULL, -1 }
123 };
124
125 int
parse_qos(const char * cp)126 parse_qos(const char *cp)
127 {
128 unsigned int i;
129 char *ep = NULL;
130 long val;
131
132 if (cp == NULL)
133 return -1;
134 for (i = 0; ipqos[i].name != NULL; i++) {
135 if (strcasecmp(cp, ipqos[i].name) == 0)
136 return ipqos[i].value;
137 }
138 /* Try parsing as an integer */
139 val = strtol(cp, &ep, 0);
140 if (*cp == '\0' || *ep != '\0' || val < 0 || val > 255)
141 return -1;
142 return val;
143 }
144
145 const char *
iptos2str(int iptos)146 iptos2str(int iptos)
147 {
148 int i;
149 static char iptos_str[sizeof "0xff"];
150 if (iptos < 0 || iptos > 64) iptos = 0;
151 for (i = 0; ipqos[i].name != NULL; i++) {
152 if (ipqos[i].value == iptos)
153 return ipqos[i].name;
154 }
155 snprintf(iptos_str, sizeof iptos_str, "0x%02x", iptos);
156 return iptos_str;
157 }
158