iproute2/lib/ll_addr.c
<<
>>
Prefs
   1/*
   2 * ll_addr.c
   3 *
   4 *              This program is free software; you can redistribute it and/or
   5 *              modify it under the terms of the GNU General Public License
   6 *              as published by the Free Software Foundation; either version
   7 *              2 of the License, or (at your option) any later version.
   8 *
   9 * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10 */
  11
  12#include <stdio.h>
  13#include <stdlib.h>
  14#include <unistd.h>
  15#include <fcntl.h>
  16#include <sys/ioctl.h>
  17#include <sys/socket.h>
  18#include <netinet/in.h>
  19#include <arpa/inet.h>
  20#include <string.h>
  21
  22#include <linux/netdevice.h>
  23#include <linux/if_arp.h>
  24#include <linux/sockios.h>
  25
  26#include "rt_names.h"
  27#include "utils.h"
  28
  29const char *ll_addr_n2a(const unsigned char *addr, int alen, int type,
  30                        char *buf, int blen)
  31{
  32        int i;
  33        int l;
  34
  35        if (alen == 4 &&
  36            (type == ARPHRD_TUNNEL || type == ARPHRD_SIT
  37             || type == ARPHRD_IPGRE))
  38                return inet_ntop(AF_INET, addr, buf, blen);
  39
  40        if (alen == 16 && (type == ARPHRD_TUNNEL6 || type == ARPHRD_IP6GRE))
  41                return inet_ntop(AF_INET6, addr, buf, blen);
  42        if (alen == 7 && type == ARPHRD_AX25)
  43                return ax25_ntop(AF_AX25, addr, buf, blen);
  44        if (alen == 7 && type == ARPHRD_NETROM)
  45                return netrom_ntop(AF_NETROM, addr, buf, blen);
  46        if (alen == 5 && type == ARPHRD_ROSE)
  47                return rose_ntop(AF_ROSE, addr, buf, blen);
  48
  49        snprintf(buf, blen, "%02x", addr[0]);
  50        for (i = 1, l = 2; i < alen && l < blen; i++, l += 3)
  51                snprintf(buf + l, blen - l, ":%02x", addr[i]);
  52        return buf;
  53}
  54
  55/*NB: lladdr is char * (rather than u8 *) because sa_data is char * (1003.1g) */
  56int ll_addr_a2n(char *lladdr, int len, const char *arg)
  57{
  58        if (strchr(arg, '.')) {
  59                inet_prefix pfx;
  60                if (get_addr_1(&pfx, arg, AF_INET)) {
  61                        fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg);
  62                        return -1;
  63                }
  64                if (len < 4)
  65                        return -1;
  66                memcpy(lladdr, pfx.data, 4);
  67                return 4;
  68        } else {
  69                int i;
  70
  71                for (i = 0; i < len; i++) {
  72                        int temp;
  73                        char *cp = strchr(arg, ':');
  74                        if (cp) {
  75                                *cp = 0;
  76                                cp++;
  77                        }
  78                        if (sscanf(arg, "%x", &temp) != 1) {
  79                                fprintf(stderr, "\"%s\" is invalid lladdr.\n",
  80                                        arg);
  81                                return -1;
  82                        }
  83                        if (temp < 0 || temp > 255) {
  84                                fprintf(stderr, "\"%s\" is invalid lladdr.\n",
  85                                        arg);
  86                                return -1;
  87                        }
  88                        lladdr[i] = temp;
  89                        if (!cp)
  90                                break;
  91                        arg = cp;
  92                }
  93                return i + 1;
  94        }
  95}
  96