From e9199719056f83f3df298aad0e6e449d74b1726d Mon Sep 17 00:00:00 2001 From: Geoff Salmon Date: Sun, 24 Feb 2013 18:56:36 -0500 Subject: [PATCH] adds CLOCK_DESCRIPTION struct Modifies existing structs changing Octet *foo -> Octet foo[0] and marks them PACKED so the message buffer can be cast to the structs. Signed-off-by: Geoff Salmon --- ddt.h | 13 +++++++++---- tlv.h | 26 ++++++++++++++++++++++++-- util.c | 17 +++++++++-------- util.h | 14 +++++++------- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/ddt.h b/ddt.h index 760b443..4acaa4f 100644 --- a/ddt.h +++ b/ddt.h @@ -51,8 +51,13 @@ struct PortIdentity { struct PortAddress { Enumeration16 networkProtocol; UInteger16 addressLength; - Octet *address; -}; + Octet address[0]; +} PACKED; + +struct PhysicalAddress { + UInteger16 length; + Octet address[0]; +} PACKED; struct ClockQuality { UInteger8 clockClass; @@ -68,8 +73,8 @@ struct TLV { struct PTPText { UInteger8 length; - Octet *text; -}; + Octet text[0]; +} PACKED; /* A static_ptp_text is like a PTPText but includes space to store the * text inside the struct. The text array must always be diff --git a/tlv.h b/tlv.h index cfcab68..b437286 100644 --- a/tlv.h +++ b/tlv.h @@ -21,6 +21,7 @@ #define HAVE_TLV_H #include "ddt.h" +#include "ds.h" /* TLV types */ #define TLV_MANAGEMENT 0x0001 @@ -176,10 +177,31 @@ struct time_status_np { struct ClockIdentity gmIdentity; } PACKED; +enum clock_type { + CLOCK_TYPE_ORDINARY = 0x80, + CLOCK_TYPE_BOUNDARY = 0x40, + CLOCK_TYPE_P2P = 0x20, + CLOCK_TYPE_E2E = 0x10, + CLOCK_TYPE_MANAGEMENT = 0x08, +}; + +#define PROFILE_ID_LEN 6 + +struct mgmt_clock_description { + UInteger16 *clockType; + struct PTPText *physicalLayerProtocol; + struct PhysicalAddress *physicalAddress; + struct PortAddress *protocolAddress; + Octet *manufacturerIdentity; + struct PTPText *productDescription; + struct PTPText *revisionData; + struct PTPText *userDescription; + Octet *profileIdentity; +}; + struct tlv_extra { union { - /* Empty for now, but will contain structs for the - * TLVs that use the tlv_extra support. */ + struct mgmt_clock_description cd; }; }; diff --git a/util.c b/util.c index 57e9fbe..7699fac 100644 --- a/util.c +++ b/util.c @@ -119,7 +119,7 @@ int static_ptp_text_copy(struct static_ptp_text *dst, const struct PTPText *src) void ptp_text_copy(struct PTPText *dst, const struct static_ptp_text *src) { dst->length = src->length; - dst->text = (Octet *)src->text; + memcpy(dst->text, src->text, src->length); } int ptp_text_set(struct PTPText *dst, const char *src) @@ -130,21 +130,22 @@ int ptp_text_set(struct PTPText *dst, const char *src) if (len > MAX_PTP_OCTETS) return -1; dst->length = len; - dst->text = (Octet *)src; + memcpy(dst->text, src, len); } else { dst->length = 0; - dst->text = NULL; } return 0; } int static_ptp_text_set(struct static_ptp_text *dst, const char *src) { - struct PTPText t; - size_t len = strlen(src); + int len = strlen(src); if (len > MAX_PTP_OCTETS) return -1; - t.length = len; - t.text = (Octet *)src; - return static_ptp_text_copy(dst, &t); + if (dst->max_symbols > 0 && strlen_utf8((Octet *) src) > dst->max_symbols) + return -1; + dst->length = len; + memcpy(dst->text, src, len); + dst->text[len] = '\0'; + return 0; } diff --git a/util.h b/util.h index 5fe9dda..a2d7089 100644 --- a/util.h +++ b/util.h @@ -58,7 +58,7 @@ int generate_clock_identity(struct ClockIdentity *ci, char *name); /** * Copies a PTPText to a static_ptp_text. This copies the text into - * the static_ptp_text.o + * the static_ptp_text. * @param dst The static_ptp_text to copy to * @param src The PTPText to copy from * @return Zero on success, -1 if text in src is too long or not valid @@ -67,18 +67,18 @@ int generate_clock_identity(struct ClockIdentity *ci, char *name); int static_ptp_text_copy(struct static_ptp_text *dst, const struct PTPText *src); /** - * Copies a static_ptp_text to a PTPText. This makes the PTPText point - * to the memory inside the static_ptp_text. + * Copies a static_ptp_text to a PTPText. Caller must ensure it's + * valid to write to the memory after the PTPText struct. The trailing + * \0 is not copied. * @param dst The PTPText to copy to * @param src The static_ptp_text to copy from */ void ptp_text_copy(struct PTPText *dst, const struct static_ptp_text *src); /** - * Sets a PTPText from a null-terminated char*. After returning, the - * PTPText field points to the same memory as str, so str should not - * be modified and should remain valid as long as the PTPText can be - * used. + * Sets a PTPText from a null-terminated char*. Caller must ensure it's + * valid to write to the memory after the PTPText struct. The trailing + * \0 is not copied. * @param dst The PTPText to copy to * @param src The text to copy from * @return Zero on success, -1 if src is too long