akaros/user/iplib/ipaux.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
  17/*
  18 *  well known IP addresses
  19 */
  20uint8_t IPv4bcast[IPaddrlen] = {
  21        0, 0, 0, 0,
  22        0, 0, 0, 0,
  23        0, 0, 0xff, 0xff,
  24        0xff, 0xff, 0xff, 0xff
  25};
  26uint8_t IPv4allsys[IPaddrlen] = {
  27        0, 0, 0, 0,
  28        0, 0, 0, 0,
  29        0, 0, 0xff, 0xff,
  30        0xe0, 0, 0, 0x01
  31};
  32uint8_t IPv4allrouter[IPaddrlen] = {
  33        0, 0, 0, 0,
  34        0, 0, 0, 0,
  35        0, 0, 0xff, 0xff,
  36        0xe0, 0, 0, 0x02
  37};
  38uint8_t IPallbits[IPaddrlen] = {
  39        0xff, 0xff, 0xff, 0xff,
  40        0xff, 0xff, 0xff, 0xff,
  41        0xff, 0xff, 0xff, 0xff,
  42        0xff, 0xff, 0xff, 0xff
  43};
  44uint8_t IPnoaddr[IPaddrlen];
  45
  46/*
  47 *  prefix of all v4 addresses
  48 */
  49uint8_t v4prefix[IPaddrlen] = {
  50        0, 0, 0, 0,
  51        0, 0, 0, 0,
  52        0, 0, 0xff, 0xff,
  53        0, 0, 0, 0
  54};
  55
  56int isv4(uint8_t *ip)
  57{
  58        return memcmp(ip, v4prefix, IPv4off) == 0;
  59}
  60
  61/*
  62 *  the following routines are unrolled with no memset's to speed
  63 *  up the usual case
  64 */
  65void v4tov6(uint8_t *v6, uint8_t *v4)
  66{
  67        v6[0] = 0;
  68        v6[1] = 0;
  69        v6[2] = 0;
  70        v6[3] = 0;
  71        v6[4] = 0;
  72        v6[5] = 0;
  73        v6[6] = 0;
  74        v6[7] = 0;
  75        v6[8] = 0;
  76        v6[9] = 0;
  77        v6[10] = 0xff;
  78        v6[11] = 0xff;
  79        v6[12] = v4[0];
  80        v6[13] = v4[1];
  81        v6[14] = v4[2];
  82        v6[15] = v4[3];
  83}
  84
  85int v6tov4(uint8_t *v4, uint8_t *v6)
  86{
  87        if(v6[0] == 0
  88        && v6[1] == 0
  89        && v6[2] == 0
  90        && v6[3] == 0
  91        && v6[4] == 0
  92        && v6[5] == 0
  93        && v6[6] == 0
  94        && v6[7] == 0
  95        && v6[8] == 0
  96        && v6[9] == 0
  97        && v6[10] == 0xff
  98        && v6[11] == 0xff)
  99        {
 100                v4[0] = v6[12];
 101                v4[1] = v6[13];
 102                v4[2] = v6[14];
 103                v4[3] = v6[15];
 104                return 0;
 105        } else {
 106                memset(v4, 0, 4);
 107                if (memcmp(v6, IPnoaddr, IPaddrlen) == 0)
 108                        return 0;
 109                return -1;
 110        }
 111}
 112