util: Relocate utility functions from pmc.c.
The file, pmc.c, contains utility functions for printing out a port address structure. We will want to call these functions from pmc_common.c, and so this patch moves the utility functions where they belong. Signed-off-by: Richard Cochran <richardcochran@gmail.com>master
parent
d89d26d618
commit
0483bf4972
48
pmc.c
48
pmc.c
|
@ -127,60 +127,12 @@ static char *text2str(struct PTPText *text)
|
||||||
return (char*)(s.text);
|
return (char*)(s.text);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAX_PRINT_BYTES 16
|
|
||||||
#define BIN_BUF_SIZE (MAX_PRINT_BYTES * 3 + 1)
|
|
||||||
|
|
||||||
static char *bin2str_impl(Octet *data, int len, char *buf, int buf_len)
|
|
||||||
{
|
|
||||||
int i, offset = 0;
|
|
||||||
if (len > MAX_PRINT_BYTES)
|
|
||||||
len = MAX_PRINT_BYTES;
|
|
||||||
buf[0] = '\0';
|
|
||||||
if (!data)
|
|
||||||
return buf;
|
|
||||||
if (len)
|
|
||||||
offset += snprintf(buf, buf_len, "%02hhx", data[0]);
|
|
||||||
for (i = 1; i < len; i++) {
|
|
||||||
if (offset >= buf_len)
|
|
||||||
/* truncated output */
|
|
||||||
break;
|
|
||||||
offset += snprintf(buf + offset, buf_len - offset, ":%02hhx", data[i]);
|
|
||||||
}
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *bin2str(Octet *data, int len)
|
static char *bin2str(Octet *data, int len)
|
||||||
{
|
{
|
||||||
static char buf[BIN_BUF_SIZE];
|
static char buf[BIN_BUF_SIZE];
|
||||||
return bin2str_impl(data, len, buf, sizeof(buf));
|
return bin2str_impl(data, len, buf, sizeof(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t align16(uint16_t *p)
|
|
||||||
{
|
|
||||||
uint16_t v;
|
|
||||||
memcpy(&v, p, sizeof(v));
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *portaddr2str(struct PortAddress *addr)
|
|
||||||
{
|
|
||||||
static char buf[BIN_BUF_SIZE];
|
|
||||||
switch(align16(&addr->networkProtocol)) {
|
|
||||||
case TRANS_UDP_IPV4:
|
|
||||||
if (align16(&addr->addressLength) == 4
|
|
||||||
&& inet_ntop(AF_INET, addr->address, buf, sizeof(buf)))
|
|
||||||
return buf;
|
|
||||||
break;
|
|
||||||
case TRANS_UDP_IPV6:
|
|
||||||
if (align16(&addr->addressLength) == 16
|
|
||||||
&& inet_ntop(AF_INET6, addr->address, buf, sizeof(buf)))
|
|
||||||
return buf;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
bin2str_impl(addr->address, align16(&addr->addressLength), buf, sizeof(buf));
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void pmc_show(struct ptp_message *msg, FILE *fp)
|
static void pmc_show(struct ptp_message *msg, FILE *fp)
|
||||||
{
|
{
|
||||||
int action;
|
int action;
|
||||||
|
|
39
util.c
39
util.c
|
@ -16,6 +16,7 @@
|
||||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*/
|
*/
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
@ -68,6 +69,25 @@ const char *ev_str[] = {
|
||||||
"RS_PASSIVE",
|
"RS_PASSIVE",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
char *bin2str_impl(Octet *data, int len, char *buf, int buf_len)
|
||||||
|
{
|
||||||
|
int i, offset = 0;
|
||||||
|
if (len > MAX_PRINT_BYTES)
|
||||||
|
len = MAX_PRINT_BYTES;
|
||||||
|
buf[0] = '\0';
|
||||||
|
if (!data)
|
||||||
|
return buf;
|
||||||
|
if (len)
|
||||||
|
offset += snprintf(buf, buf_len, "%02hhx", data[0]);
|
||||||
|
for (i = 1; i < len; i++) {
|
||||||
|
if (offset >= buf_len)
|
||||||
|
/* truncated output */
|
||||||
|
break;
|
||||||
|
offset += snprintf(buf + offset, buf_len - offset, ":%02hhx", data[i]);
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
char *cid2str(struct ClockIdentity *id)
|
char *cid2str(struct ClockIdentity *id)
|
||||||
{
|
{
|
||||||
static char buf[64];
|
static char buf[64];
|
||||||
|
@ -100,6 +120,25 @@ char *pid2str(struct PortIdentity *id)
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *portaddr2str(struct PortAddress *addr)
|
||||||
|
{
|
||||||
|
static char buf[BIN_BUF_SIZE];
|
||||||
|
switch (align16(&addr->networkProtocol)) {
|
||||||
|
case TRANS_UDP_IPV4:
|
||||||
|
if (align16(&addr->addressLength) == 4
|
||||||
|
&& inet_ntop(AF_INET, addr->address, buf, sizeof(buf)))
|
||||||
|
return buf;
|
||||||
|
break;
|
||||||
|
case TRANS_UDP_IPV6:
|
||||||
|
if (align16(&addr->addressLength) == 16
|
||||||
|
&& inet_ntop(AF_INET6, addr->address, buf, sizeof(buf)))
|
||||||
|
return buf;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bin2str_impl(addr->address, align16(&addr->addressLength), buf, sizeof(buf));
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
int str2mac(const char *s, unsigned char mac[MAC_LEN])
|
int str2mac(const char *s, unsigned char mac[MAC_LEN])
|
||||||
{
|
{
|
||||||
unsigned char buf[MAC_LEN];
|
unsigned char buf[MAC_LEN];
|
||||||
|
|
15
util.h
15
util.h
|
@ -20,11 +20,15 @@
|
||||||
#ifndef HAVE_UTIL_H
|
#ifndef HAVE_UTIL_H
|
||||||
#define HAVE_UTIL_H
|
#define HAVE_UTIL_H
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "ddt.h"
|
#include "ddt.h"
|
||||||
#include "ether.h"
|
#include "ether.h"
|
||||||
|
|
||||||
|
#define MAX_PRINT_BYTES 16
|
||||||
|
#define BIN_BUF_SIZE (MAX_PRINT_BYTES * 3 + 1)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Table of human readable strings, one for each port state.
|
* Table of human readable strings, one for each port state.
|
||||||
*/
|
*/
|
||||||
|
@ -35,6 +39,15 @@ extern const char *ps_str[];
|
||||||
*/
|
*/
|
||||||
extern const char *ev_str[];
|
extern const char *ev_str[];
|
||||||
|
|
||||||
|
static inline uint16_t align16(uint16_t *p)
|
||||||
|
{
|
||||||
|
uint16_t v;
|
||||||
|
memcpy(&v, p, sizeof(v));
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *bin2str_impl(Octet *data, int len, char *buf, int buf_len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a clock identity into a human readable string.
|
* Convert a clock identity into a human readable string.
|
||||||
*
|
*
|
||||||
|
@ -65,6 +78,8 @@ int count_char(const char *str, char c);
|
||||||
*/
|
*/
|
||||||
char *pid2str(struct PortIdentity *id);
|
char *pid2str(struct PortIdentity *id);
|
||||||
|
|
||||||
|
char *portaddr2str(struct PortAddress *addr);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scan a string containing a MAC address and convert it into binary form.
|
* Scan a string containing a MAC address and convert it into binary form.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue