File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / iperf / src / dscp.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:14:54 2023 UTC (9 months ago) by misho
Branches: iperf, MAIN
CVS tags: v3_15, HEAD
Version 3.15

    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: #ifndef IPTOS_DSCP_VA
   94: # define	IPTOS_DSCP_VA		0xb0
   95: #endif /* IPTOS_DSCP_VA */
   96: 
   97: static const struct {
   98: 	const char *name;
   99: 	int value;
  100: } ipqos[] = {
  101: 	{ "af11", IPTOS_DSCP_AF11 },
  102: 	{ "af12", IPTOS_DSCP_AF12 },
  103: 	{ "af13", IPTOS_DSCP_AF13 },
  104: 	{ "af21", IPTOS_DSCP_AF21 },
  105: 	{ "af22", IPTOS_DSCP_AF22 },
  106: 	{ "af23", IPTOS_DSCP_AF23 },
  107: 	{ "af31", IPTOS_DSCP_AF31 },
  108: 	{ "af32", IPTOS_DSCP_AF32 },
  109: 	{ "af33", IPTOS_DSCP_AF33 },
  110: 	{ "af41", IPTOS_DSCP_AF41 },
  111: 	{ "af42", IPTOS_DSCP_AF42 },
  112: 	{ "af43", IPTOS_DSCP_AF43 },
  113: 	{ "cs0", IPTOS_DSCP_CS0 },
  114: 	{ "cs1", IPTOS_DSCP_CS1 },
  115: 	{ "cs2", IPTOS_DSCP_CS2 },
  116: 	{ "cs3", IPTOS_DSCP_CS3 },
  117: 	{ "cs4", IPTOS_DSCP_CS4 },
  118: 	{ "cs5", IPTOS_DSCP_CS5 },
  119: 	{ "cs6", IPTOS_DSCP_CS6 },
  120: 	{ "cs7", IPTOS_DSCP_CS7 },
  121: 	{ "ef", IPTOS_DSCP_EF },
  122: 	{ "va", IPTOS_DSCP_VA },
  123: 	{ "lowdelay", IPTOS_LOWDELAY },
  124: 	{ "throughput", IPTOS_THROUGHPUT },
  125: 	{ "reliability", IPTOS_RELIABILITY },
  126: 	{ NULL, -1 }
  127: };
  128: 
  129: int
  130: parse_qos(const char *cp)
  131: {
  132: 	unsigned int i;
  133: 	char *ep = NULL;
  134: 	long val;
  135: 
  136: 	if (cp == NULL)
  137: 		return -1;
  138: 	for (i = 0; ipqos[i].name != NULL; i++) {
  139: 		if (strcasecmp(cp, ipqos[i].name) == 0)
  140: 			return ipqos[i].value;
  141: 	}
  142: 	/* Try parsing as an integer */
  143:     /* Max DSCP value is 2**6 - 1 */
  144: 	val = strtol(cp, &ep, 0);
  145: 	if (*cp == '\0' || *ep != '\0' || val < 0 || val > 63)
  146: 		return -1;
  147: 	return val << 2;
  148: }
  149: 
  150: const char *
  151: iptos2str(int iptos)
  152: {
  153: 	int i;
  154: 	static char iptos_str[sizeof "0xff"];
  155: 	if (iptos < 0 || iptos > 64) iptos = 0;
  156: 	for (i = 0; ipqos[i].name != NULL; i++) {
  157: 		if (ipqos[i].value == iptos)
  158: 			return ipqos[i].name;
  159: 	}
  160: 	snprintf(iptos_str, sizeof iptos_str, "0x%02x", iptos);
  161: 	return iptos_str;
  162: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>