akaros/user/iplib/classmask.c
<<
>>
Prefs
   1/*
   2 * This file is part of the UCB release of Plan 9. It is subject to the license
   3 * terms in the LICENSE file found in the top-level directory of this
   4 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
   5 * part of the UCB release of Plan 9, including this file, may be copied,
   6 * modified, propagated, or distributed except according to the terms contained
   7 * in the LICENSE file.
   8 */
   9#include <stdlib.h>
  10
  11#include <iplib/iplib.h>
  12#include <parlib/parlib.h>
  13#include <signal.h>
  14#include <stdio.h>
  15#include <unistd.h>
  16
  17static uint8_t classmask[4][16] = {
  18        0xff,0xff,0xff,0xff,  0xff,0xff,0xff,0xff,  0xff,0xff,0xff,0xff,  0xff,0x00,0x00,0x00,
  19        0xff,0xff,0xff,0xff,  0xff,0xff,0xff,0xff,  0xff,0xff,0xff,0xff,  0xff,0x00,0x00,0x00,
  20        0xff,0xff,0xff,0xff,  0xff,0xff,0xff,0xff,  0xff,0xff,0xff,0xff,  0xff,0xff,0x00,0x00,
  21        0xff,0xff,0xff,0xff,  0xff,0xff,0xff,0xff,  0xff,0xff,0xff,0xff,  0xff,0xff,0xff,0x00,
  22};
  23
  24static uint8_t v6loopback[IPaddrlen] = {
  25        0, 0, 0, 0,
  26        0, 0, 0, 0,
  27        0, 0, 0, 0,
  28        0, 0, 0, 0x01
  29};
  30
  31static uint8_t v6linklocal[IPaddrlen] = {
  32        0xfe, 0x80, 0, 0,
  33        0, 0, 0, 0,
  34        0, 0, 0, 0,
  35        0, 0, 0, 0
  36};
  37static uint8_t v6linklocalmask[IPaddrlen] = {
  38        0xff, 0xff, 0xff, 0xff,
  39        0xff, 0xff, 0xff, 0xff,
  40        0, 0, 0, 0,
  41        0, 0, 0, 0
  42};
  43static int v6llpreflen = 8;     /* link-local prefix length in bytes */
  44
  45static uint8_t v6multicast[IPaddrlen] = {
  46        0xff, 0, 0, 0,
  47        0, 0, 0, 0,
  48        0, 0, 0, 0,
  49        0, 0, 0, 0
  50};
  51static uint8_t v6multicastmask[IPaddrlen] = {
  52        0xff, 0, 0, 0,
  53        0, 0, 0, 0,
  54        0, 0, 0, 0,
  55        0, 0, 0, 0
  56};
  57static int v6mcpreflen = 1;     /* multicast prefix length */
  58
  59static uint8_t v6solicitednode[IPaddrlen] = {
  60        0xff, 0x02, 0, 0,
  61        0, 0, 0, 0,
  62        0, 0, 0, 0x01,
  63        0xff, 0, 0, 0
  64};
  65static uint8_t v6solicitednodemask[IPaddrlen] = {
  66        0xff, 0xff, 0xff, 0xff,
  67        0xff, 0xff, 0xff, 0xff,
  68        0xff, 0xff, 0xff, 0xff,
  69        0xff, 0x0, 0x0, 0x0
  70};
  71static int v6snpreflen = 13;
  72
  73uint8_t *defmask(uint8_t *ip)
  74{
  75        if (isv4(ip))
  76                return classmask[ip[IPv4off] >> 6];
  77        else {
  78                if (ipcmp(ip, v6loopback) == 0)
  79                        return IPallbits;
  80                else if (memcmp(ip, v6linklocal, v6llpreflen) == 0)
  81                        return v6linklocalmask;
  82                else if (memcmp(ip, v6solicitednode, v6snpreflen) == 0)
  83                        return v6solicitednodemask;
  84                else if (memcmp(ip, v6multicast, v6mcpreflen) == 0)
  85                        return v6multicastmask;
  86                return IPallbits;
  87        }
  88}
  89
  90void maskip(uint8_t *from, uint8_t *mask, uint8_t *to)
  91{
  92        int i;
  93
  94        for (i = 0; i < IPaddrlen; i++)
  95                to[i] = from[i] & mask[i];
  96}
  97