- case 'S': // IP type-of-service
- // TODO use a function that understands base-2
- // the zero base here allows the user to specify
- // "0x#" hex, "0#" octal, and "#" decimal numbers
- mExtSettings->mTOS = strtol( optarg, NULL, 0 );
+ case 'S':
+ // Convert into IPv4/v6 6-bit DiffServ codepoint. If string
+ // - consists of six 0/1 digits, it is seen as binary number;
+ // - is preceded by `0x', it is interpreted as hex number;
+ // - is preceded by `0', it is interpreted as octal number;
+ // - otherwise it is interpreted as decimal number.
+ // The maximum possible 6-bit value is 252 (0xfc).
+ if ( strspn(optarg, "01") == 6 )
+ mExtSettings->mTOS = strtol(optarg, NULL, 2);
+ else
+ mExtSettings->mTOS = strtol(optarg, NULL, 0);
+ if ( mExtSettings->mTOS < 0 || mExtSettings->mTOS > 0xfc )
+ die("Invalid DiffServ codepoint %s", optarg);
+ mExtSettings->mTOS <<= 2;