busybox/networking/libiproute/ll_addr.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * This program is free software; you can redistribute it and/or
   4 * modify it under the terms of the GNU General Public License
   5 * as published by the Free Software Foundation; either version
   6 * 2 of the License, or (at your option) any later version.
   7 *
   8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
   9 */
  10
  11#include <net/if_arp.h>
  12
  13#include "libbb.h"
  14#include "rt_names.h"
  15#include "utils.h"
  16
  17
  18const char* FAST_FUNC ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen)
  19{
  20        int i;
  21        int l;
  22
  23        if (alen == 4
  24         && (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)
  25        ) {
  26                return inet_ntop(AF_INET, addr, buf, blen);
  27        }
  28        l = 0;
  29        for (i = 0; i < alen; i++) {
  30                if (i == 0) {
  31                        snprintf(buf + l, blen, ":%02x"+1, addr[i]);
  32                        blen -= 2;
  33                        l += 2;
  34                } else {
  35                        snprintf(buf + l, blen, ":%02x", addr[i]);
  36                        blen -= 3;
  37                        l += 3;
  38                }
  39        }
  40        return buf;
  41}
  42
  43int FAST_FUNC ll_addr_a2n(unsigned char *lladdr, int len, char *arg)
  44{
  45        int i;
  46
  47        if (strchr(arg, '.')) {
  48                inet_prefix pfx;
  49                if (get_addr_1(&pfx, arg, AF_INET)) {
  50                        bb_error_msg("\"%s\" is invalid lladdr", arg);
  51                        return -1;
  52                }
  53                if (len < 4) {
  54                        return -1;
  55                }
  56                memcpy(lladdr, pfx.data, 4);
  57                return 4;
  58        }
  59
  60        for (i = 0; i < len; i++) {
  61                int temp;
  62                char *cp = strchr(arg, ':');
  63                if (cp) {
  64                        *cp = 0;
  65                        cp++;
  66                }
  67                if (sscanf(arg, "%x", &temp) != 1 || (temp < 0 || temp > 255)) {
  68                        bb_error_msg("\"%s\" is invalid lladdr", arg);
  69                        return -1;
  70                }
  71                lladdr[i] = temp;
  72                if (!cp) {
  73                        break;
  74                }
  75                arg = cp;
  76        }
  77        return i+1;
  78}
  79