util: Make a useful helper function more public.

The function, str2addr(), will be needed by the upcoming unicast
client code.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2018-03-08 22:02:29 -08:00
parent ffbafa557b
commit 40480f3b28
3 changed files with 50 additions and 30 deletions

31
nsm.c
View File

@ -359,43 +359,14 @@ static int nsm_request(struct nsm *nsm, char *target)
{ {
enum transport_type type = transport_type(nsm->trp); enum transport_type type = transport_type(nsm->trp);
UInteger8 transportSpecific; UInteger8 transportSpecific;
unsigned char mac[MAC_LEN];
struct in_addr ipv4_addr;
struct ptp_message *msg; struct ptp_message *msg;
struct tlv_extra *extra; struct tlv_extra *extra;
Integer64 asymmetry; Integer64 asymmetry;
struct address dst; struct address dst;
int cnt, err; int cnt, err;
memset(&dst, 0, sizeof(dst)); if (str2addr(type, target, &dst)) {
switch (type) {
case TRANS_UDS:
case TRANS_UDP_IPV6:
case TRANS_DEVICENET:
case TRANS_CONTROLNET:
case TRANS_PROFINET:
pr_err("sorry, NSM not support with this transport");
return -1; return -1;
case TRANS_UDP_IPV4:
if (!inet_aton(target, &ipv4_addr)) {
pr_err("bad IPv4 address");
return -1;
}
dst.sin.sin_family = AF_INET;
dst.sin.sin_addr = ipv4_addr;
dst.len = sizeof(dst.sin);
break;
case TRANS_IEEE_802_3:
if (str2mac(target, mac)) {
pr_err("bad Layer-2 address");
return -1;
}
dst.sll.sll_family = AF_PACKET;
dst.sll.sll_halen = MAC_LEN;
memcpy(&dst.sll.sll_addr, mac, MAC_LEN);
dst.len = sizeof(dst.sll);
break;
} }
msg = msg_allocate(); msg = msg_allocate();

38
util.c
View File

@ -139,6 +139,44 @@ char *portaddr2str(struct PortAddress *addr)
return buf; return buf;
} }
int str2addr(enum transport_type type, const char *s, struct address *addr)
{
unsigned char mac[MAC_LEN];
struct in_addr ipv4_addr;
memset(addr, 0, sizeof(*addr));
switch (type) {
case TRANS_UDS:
case TRANS_UDP_IPV6:
case TRANS_DEVICENET:
case TRANS_CONTROLNET:
case TRANS_PROFINET:
pr_err("sorry, cannot convert addresses for this transport");
return -1;
case TRANS_UDP_IPV4:
if (!inet_aton(s, &ipv4_addr)) {
pr_err("bad IPv4 address");
return -1;
}
addr->sin.sin_family = AF_INET;
addr->sin.sin_addr = ipv4_addr;
addr->len = sizeof(addr->sin);
break;
case TRANS_IEEE_802_3:
if (str2mac(s, mac)) {
pr_err("bad Layer-2 address");
return -1;
}
addr->sll.sll_family = AF_PACKET;
addr->sll.sll_halen = MAC_LEN;
memcpy(&addr->sll.sll_addr, mac, MAC_LEN);
addr->len = sizeof(addr->sll);
break;
}
return 0;
}
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];

11
util.h
View File

@ -23,8 +23,10 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "address.h"
#include "ddt.h" #include "ddt.h"
#include "ether.h" #include "ether.h"
#include "transport.h"
#define MAX_PRINT_BYTES 16 #define MAX_PRINT_BYTES 16
#define BIN_BUF_SIZE (MAX_PRINT_BYTES * 3 + 1) #define BIN_BUF_SIZE (MAX_PRINT_BYTES * 3 + 1)
@ -104,6 +106,15 @@ static inline int pid_eq(struct PortIdentity *a, struct PortIdentity *b)
return memcmp(a, b, sizeof(*a)) == 0; return memcmp(a, b, sizeof(*a)) == 0;
} }
/**
* Convert a string containing a network address into binary form.
* @param type The network transport type of the address.
* @param s String in human readable form.
* @param addr Pointer to a buffer to hold the result.
* @return Zero on success, or -1 if the string is incorrectly formatted.
*/
int str2addr(enum transport_type type, const char *s, struct address *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.
* *