Skip to content

Commit

Permalink
util: Relocate utility functions from pmc.c.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
richardcochran committed Mar 8, 2018
1 parent d89d26d commit 0483bf4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
48 changes: 0 additions & 48 deletions pmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,60 +127,12 @@ static char *text2str(struct PTPText *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 buf[BIN_BUF_SIZE];
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)
{
int action;
Expand Down
39 changes: 39 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
Expand Down Expand Up @@ -68,6 +69,25 @@ const char *ev_str[] = {
"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)
{
static char buf[64];
Expand Down Expand Up @@ -100,6 +120,25 @@ char *pid2str(struct PortIdentity *id)
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])
{
unsigned char buf[MAC_LEN];
Expand Down
15 changes: 15 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
#ifndef HAVE_UTIL_H
#define HAVE_UTIL_H

#include <string.h>
#include <time.h>

#include "ddt.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.
*/
Expand All @@ -35,6 +39,15 @@ extern const char *ps_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.
*
Expand Down Expand Up @@ -65,6 +78,8 @@ int count_char(const char *str, char c);
*/
char *pid2str(struct PortIdentity *id);

char *portaddr2str(struct PortAddress *addr);

/**
* Scan a string containing a MAC address and convert it into binary form.
*
Expand Down

0 comments on commit 0483bf4

Please sign in to comment.