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 <stdlib.h> 31 #include <inttypes.h> 32 33 #ifdef WIN32 34 #define strcasecmp(a,b) _stricmp(a,b) 35 #define snprintf _snprintf 36 #endif 37 38 int parse_qos(const char *cp); 39 const char * iptos2str(int iptos); 40 41 /* 42 * Definitions for IP type of service (ip_tos) 43 */ 44 45 #if HAVE_NETINET_IN_SYSTM_H 46 #include <netinet/in_systm.h> 47 #endif 48 #if HAVE_NETINET_IP_H 49 #include <netinet/ip.h> 50 #endif 51 52 #ifndef IPTOS_LOWDELAY 53 # define IPTOS_LOWDELAY 0x10 54 # define IPTOS_THROUGHPUT 0x08 55 # define IPTOS_RELIABILITY 0x04 56 # define IPTOS_LOWCOST 0x02 57 # define IPTOS_MINCOST IPTOS_LOWCOST 58 #endif /* IPTOS_LOWDELAY */ 59 60 /* 61 * Definitions for DiffServ Codepoints as per RFC2474 62 */ 63 #ifndef IPTOS_DSCP_AF11 64 # define IPTOS_DSCP_AF11 0x28 65 # define IPTOS_DSCP_AF12 0x30 66 # define IPTOS_DSCP_AF13 0x38 67 # define IPTOS_DSCP_AF21 0x48 68 # define IPTOS_DSCP_AF22 0x50 69 # define IPTOS_DSCP_AF23 0x58 70 # define IPTOS_DSCP_AF31 0x68 71 # define IPTOS_DSCP_AF32 0x70 72 # define IPTOS_DSCP_AF33 0x78 73 # define IPTOS_DSCP_AF41 0x88 74 # define IPTOS_DSCP_AF42 0x90 75 # define IPTOS_DSCP_AF43 0x98 76 # define IPTOS_DSCP_EF 0xb8 77 #endif /* IPTOS_DSCP_AF11 */ 78 79 #ifndef IPTOS_DSCP_CS0 80 # define IPTOS_DSCP_CS0 0x00 81 # define IPTOS_DSCP_CS1 0x20 82 # define IPTOS_DSCP_CS2 0x40 83 # define IPTOS_DSCP_CS3 0x60 84 # define IPTOS_DSCP_CS4 0x80 85 # define IPTOS_DSCP_CS5 0xa0 86 # define IPTOS_DSCP_CS6 0xc0 87 # define IPTOS_DSCP_CS7 0xe0 88 #endif /* IPTOS_DSCP_CS0 */ 89 #ifndef IPTOS_DSCP_EF 90 # define IPTOS_DSCP_EF 0xb8 91 #endif /* IPTOS_DSCP_EF */ 92 93 static const struct { 94 const char *name; 95 int value; 96 } ipqos[] = { 97 { "af11", IPTOS_DSCP_AF11 }, 98 { "af12", IPTOS_DSCP_AF12 }, 99 { "af13", IPTOS_DSCP_AF13 }, 100 { "af21", IPTOS_DSCP_AF21 }, 101 { "af22", IPTOS_DSCP_AF22 }, 102 { "af23", IPTOS_DSCP_AF23 }, 103 { "af31", IPTOS_DSCP_AF31 }, 104 { "af32", IPTOS_DSCP_AF32 }, 105 { "af33", IPTOS_DSCP_AF33 }, 106 { "af41", IPTOS_DSCP_AF41 }, 107 { "af42", IPTOS_DSCP_AF42 }, 108 { "af43", IPTOS_DSCP_AF43 }, 109 { "cs0", IPTOS_DSCP_CS0 }, 110 { "cs1", IPTOS_DSCP_CS1 }, 111 { "cs2", IPTOS_DSCP_CS2 }, 112 { "cs3", IPTOS_DSCP_CS3 }, 113 { "cs4", IPTOS_DSCP_CS4 }, 114 { "cs5", IPTOS_DSCP_CS5 }, 115 { "cs6", IPTOS_DSCP_CS6 }, 116 { "cs7", IPTOS_DSCP_CS7 }, 117 { "ef", IPTOS_DSCP_EF }, 118 { "lowdelay", IPTOS_LOWDELAY }, 119 { "throughput", IPTOS_THROUGHPUT }, 120 { "reliability", IPTOS_RELIABILITY }, 121 { NULL, -1 } 122 }; 123 124 int 125 parse_qos(const char *cp) 126 { 127 unsigned int i; 128 char *ep = NULL; 129 long val; 130 131 if (cp == NULL) 132 return -1; 133 for (i = 0; ipqos[i].name != NULL; i++) { 134 if (strcasecmp(cp, ipqos[i].name) == 0) 135 return ipqos[i].value; 136 } 137 /* Try parsing as an integer */ 138 val = strtol(cp, &ep, 0); 139 if (*cp == '\0' || *ep != '\0' || val < 0 || val > 255) 140 return -1; 141 return val; 142 } 143 144 const char * 145 iptos2str(int iptos) 146 { 147 int i; 148 static char iptos_str[sizeof "0xff"]; 149 if (iptos < 0 || iptos > 64) iptos = 0; 150 for (i = 0; ipqos[i].name != NULL; i++) { 151 if (ipqos[i].value == iptos) 152 return ipqos[i].name; 153 } 154 snprintf(iptos_str, sizeof iptos_str, "0x%02x", iptos); 155 return iptos_str; 156 } 157