Move virMacAddrXXX functions to src/util/virmacaddr.[ch]

Move the virMacAddrXXX functions out of util.[ch] and into a
new dedicate file virmacaddr.[ch]
This commit is contained in:
Daniel P. Berrange 2012-01-27 17:23:05 +00:00
parent 4ce98dadcc
commit 510fa47c2a
10 changed files with 181 additions and 119 deletions

View File

@ -93,6 +93,7 @@ UTIL_SOURCES = \
util/virhashcode.c util/virhashcode.h \
util/virkeycode.c util/virkeycode.h \
util/virkeymaps.h \
util/virmacaddr.h util/virmacaddr.c \
util/virnetdev.h util/virnetdev.c \
util/virnetdevbandwidth.h util/virnetdevbandwidth.c \
util/virnetdevbridge.h util/virnetdevbridge.c \

View File

@ -25,9 +25,9 @@
# define __VIR_CAPABILITIES_H
# include "internal.h"
# include "util.h"
# include "buf.h"
# include "cpu_conf.h"
# include "virmacaddr.h"
# include <libxml/xpath.h>

View File

@ -35,7 +35,7 @@
# include "virsocketaddr.h"
# include "virnetdevbandwidth.h"
# include "virnetdevvportprofile.h"
# include "util.h"
# include "virmacaddr.h"
enum virNetworkForwardType {
VIR_NETWORK_FORWARD_NONE = 0,

View File

@ -1120,10 +1120,6 @@ virHexToBin;
virIndexToDiskName;
virIsDevMapperDevice;
virKillProcess;
virMacAddrCompare;
virMacAddrFormat;
virMacAddrGenerate;
virMacAddrParse;
virParseNumber;
virParseVersionString;
virPipeReadUntilEOF;
@ -1183,6 +1179,13 @@ virKeycodeValueFromString;
virKeycodeValueTranslate;
# virmacaddr.h
virMacAddrCompare;
virMacAddrFormat;
virMacAddrGenerate;
virMacAddrParse;
# virnetclient.h
virNetClientHasPassFD;

View File

@ -77,7 +77,6 @@
#include "command.h"
#include "nonblocking.h"
#include "passfd.h"
#include "virrandom.h"
#ifndef NSIG
# define NSIG 32
@ -1762,104 +1761,6 @@ virStrcpy(char *dest, const char *src, size_t destbytes)
return virStrncpy(dest, src, strlen(src), destbytes);
}
/* Compare two MAC addresses, ignoring differences in case,
* as well as leading zeros.
*/
int
virMacAddrCompare (const char *p, const char *q)
{
unsigned char c, d;
do {
while (*p == '0' && c_isxdigit (p[1]))
++p;
while (*q == '0' && c_isxdigit (q[1]))
++q;
c = c_tolower (*p);
d = c_tolower (*q);
if (c == 0 || d == 0)
break;
++p;
++q;
} while (c == d);
if (UCHAR_MAX <= INT_MAX)
return c - d;
/* On machines where 'char' and 'int' are types of the same size, the
difference of two 'unsigned char' values - including the sign bit -
doesn't fit in an 'int'. */
return (c > d ? 1 : c < d ? -1 : 0);
}
/**
* virMacAddrParse:
* @str: string representation of MAC address, e.g., "0:1E:FC:E:3a:CB"
* @addr: 6-byte MAC address
*
* Parse a MAC address
*
* Return 0 upon success, or -1 in case of error.
*/
int
virMacAddrParse(const char* str, unsigned char *addr)
{
int i;
errno = 0;
for (i = 0; i < VIR_MAC_BUFLEN; i++) {
char *end_ptr;
unsigned long result;
/* This is solely to avoid accepting the leading
* space or "+" that strtoul would otherwise accept.
*/
if (!c_isxdigit(*str))
break;
result = strtoul(str, &end_ptr, 16);
if ((end_ptr - str) < 1 || 2 < (end_ptr - str) ||
(errno != 0) ||
(0xFF < result))
break;
addr[i] = (unsigned char) result;
if ((i == 5) && (*end_ptr == '\0'))
return 0;
if (*end_ptr != ':')
break;
str = end_ptr + 1;
}
return -1;
}
void virMacAddrFormat(const unsigned char *addr,
char *str)
{
snprintf(str, VIR_MAC_STRING_BUFLEN,
"%02X:%02X:%02X:%02X:%02X:%02X",
addr[0], addr[1], addr[2],
addr[3], addr[4], addr[5]);
str[VIR_MAC_STRING_BUFLEN-1] = '\0';
}
void virMacAddrGenerate(const unsigned char *prefix,
unsigned char *addr)
{
addr[0] = prefix[0];
addr[1] = prefix[1];
addr[2] = prefix[2];
addr[3] = virRandomBits(8);
addr[4] = virRandomBits(8);
addr[5] = virRandomBits(8);
}
int virEnumFromString(const char *const*types,
unsigned int ntypes,
const char *type)

View File

@ -157,8 +157,6 @@ int virStrToDouble(char const *s,
int virHexToBin(unsigned char c);
int virMacAddrCompare (const char *mac1, const char *mac2);
void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1);
void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1);
void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1);
@ -178,17 +176,6 @@ char *virStrcpy(char *dest, const char *src, size_t destbytes)
ATTRIBUTE_RETURN_CHECK;
# define virStrcpyStatic(dest, src) virStrcpy((dest), (src), sizeof(dest))
# define VIR_MAC_BUFLEN 6
# define VIR_MAC_PREFIX_BUFLEN 3
# define VIR_MAC_STRING_BUFLEN VIR_MAC_BUFLEN * 3
int virMacAddrParse(const char* str,
unsigned char *addr) ATTRIBUTE_RETURN_CHECK;
void virMacAddrFormat(const unsigned char *addr,
char *str);
void virMacAddrGenerate(const unsigned char *prefix,
unsigned char *addr);
int virDiskNameToIndex(const char* str);
char *virIndexToDiskName(int idx, const char *prefix);

128
src/util/virmacaddr.c Normal file
View File

@ -0,0 +1,128 @@
/*
* virmacaddr.c: MAC address handling
*
* Copyright (C) 2006-2012 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* Daniel P. Berrange <berrange@redhat.com>
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include "c-ctype.h"
#include "virmacaddr.h"
#include "virrandom.h"
/* Compare two MAC addresses, ignoring differences in case,
* as well as leading zeros.
*/
int
virMacAddrCompare(const char *p, const char *q)
{
unsigned char c, d;
do {
while (*p == '0' && c_isxdigit(p[1]))
++p;
while (*q == '0' && c_isxdigit(q[1]))
++q;
c = c_tolower(*p);
d = c_tolower(*q);
if (c == 0 || d == 0)
break;
++p;
++q;
} while (c == d);
if (UCHAR_MAX <= INT_MAX)
return c - d;
/* On machines where 'char' and 'int' are types of the same size, the
difference of two 'unsigned char' values - including the sign bit -
doesn't fit in an 'int'. */
return (c > d ? 1 : c < d ? -1 : 0);
}
/**
* virMacAddrParse:
* @str: string representation of MAC address, e.g., "0:1E:FC:E:3a:CB"
* @addr: 6-byte MAC address
*
* Parse a MAC address
*
* Return 0 upon success, or -1 in case of error.
*/
int
virMacAddrParse(const char* str, unsigned char *addr)
{
int i;
errno = 0;
for (i = 0; i < VIR_MAC_BUFLEN; i++) {
char *end_ptr;
unsigned long result;
/* This is solely to avoid accepting the leading
* space or "+" that strtoul would otherwise accept.
*/
if (!c_isxdigit(*str))
break;
result = strtoul(str, &end_ptr, 16);
if ((end_ptr - str) < 1 || 2 < (end_ptr - str) ||
(errno != 0) ||
(0xFF < result))
break;
addr[i] = (unsigned char) result;
if ((i == 5) && (*end_ptr == '\0'))
return 0;
if (*end_ptr != ':')
break;
str = end_ptr + 1;
}
return -1;
}
void virMacAddrFormat(const unsigned char *addr,
char *str)
{
snprintf(str, VIR_MAC_STRING_BUFLEN,
"%02X:%02X:%02X:%02X:%02X:%02X",
addr[0], addr[1], addr[2],
addr[3], addr[4], addr[5]);
str[VIR_MAC_STRING_BUFLEN-1] = '\0';
}
void virMacAddrGenerate(const unsigned char *prefix,
unsigned char *addr)
{
addr[0] = prefix[0];
addr[1] = prefix[1];
addr[2] = prefix[2];
addr[3] = virRandomBits(8);
addr[4] = virRandomBits(8);
addr[5] = virRandomBits(8);
}

41
src/util/virmacaddr.h Normal file
View File

@ -0,0 +1,41 @@
/*
* virmacaddr.h: MAC address handling
*
* Copyright (C) 2006-2012 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* Daniel P. Berrange <berrange@redhat.com>
*/
#ifndef __VIR_MACADDR_H__
# define __VIR_MACADDR_H__
# include "internal.h"
# define VIR_MAC_BUFLEN 6
# define VIR_MAC_PREFIX_BUFLEN 3
# define VIR_MAC_STRING_BUFLEN VIR_MAC_BUFLEN * 3
int virMacAddrCompare(const char *mac1, const char *mac2);
void virMacAddrFormat(const unsigned char *addr,
char *str);
void virMacAddrGenerate(const unsigned char *prefix,
unsigned char *addr);
int virMacAddrParse(const char* str,
unsigned char *addr) ATTRIBUTE_RETURN_CHECK;
#endif /* __VIR_MACADDR_H__ */

View File

@ -23,6 +23,7 @@
#include <config.h>
#include "virnetdev.h"
#include "virmacaddr.h"
#include "virfile.h"
#include "virterror_internal.h"
#include "command.h"

View File

@ -27,8 +27,8 @@
#include <config.h>
#include "virnetdevmacvlan.h"
#include "virmacaddr.h"
#include "util.h"
#include "virterror_internal.h"