diff --git a/commandline.c b/commandline.c index bafce311..7ab41741 100644 --- a/commandline.c +++ b/commandline.c @@ -761,7 +761,7 @@ int app_server_status(int argc, const char *const *argv, struct command_line_opt int app_mdp_ping(int argc, const char *const *argv, struct command_line_option *o) { const char *sid; - if (cli_arg(argc, argv, o, "SID|broadcast", &sid, validateSid, "broadcast") == -1) + if (cli_arg(argc, argv, o, "SID|broadcast", &sid, str_is_subscriber_id, "broadcast") == -1) return -1; overlay_mdp_frame mdp; @@ -961,7 +961,7 @@ int app_rhizome_hash_file(int argc, const char *const *argv, struct command_line int cli_optional_sid(const char *arg) { - return !arg[0] || validateSid(arg); + return !arg[0] || str_is_subscriber_id(arg); } int cli_optional_bundle_key(const char *arg) diff --git a/dataformats.c b/dataformats.c index 3d4130bc..527bebfe 100644 --- a/dataformats.c +++ b/dataformats.c @@ -97,6 +97,26 @@ int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary) return (fromhex(dstBinary, srcHex, nbinary) == nbinary && srcHex[nbinary * 2] == '\0') ? 0 : -1; } +int str_is_subscriber_id(const char *sid) +{ + return strcasecmp(sid, "broadcast") == 0 || _is_xstring(sid, SID_STRLEN); +} + +int strn_is_subscriber_id(const char *sid, size_t *lenp) +{ + if (strncasecmp(sid, "broadcast", 9) == 0) { + if (lenp) + *lenp = 9; + return 1; + } + if (_is_xsubstring(sid, SID_STRLEN)) { + if (lenp) + *lenp = SID_STRLEN; + return 1; + } + return 0; +} + int rhizome_strn_is_manifest_id(const char *id) { return _is_xsubstring(id, RHIZOME_MANIFEST_ID_STRLEN); @@ -214,32 +234,6 @@ int extractSid(const unsigned char *packet, int *ofs, char *sid) return 0; } -int validateSid(const char *sid) -{ - if (!sid) { - WHY("invalid SID == NULL"); - return 0; - } - if (strcasecmp(sid, "broadcast") == 0) - return 1; - const char *s = sid; - const char *e = sid + SID_STRLEN; - while (s != e && isxdigit(*s)) - ++s; - if (s != e) { - if (*s) - WHYF("invalid SID, contains non-hex character 0x%02x at offset %d", *s, s - sid); - else - WHYF("invalid SID, too short (strlen %d)", s - sid); - return 0; - } - if (*s) { - WHYF("invalid SID, too long"); - return 0; - } - return 1; -} - int stowSid(unsigned char *packet, int ofs, const char *sid) { if (debug & DEBUG_PACKETFORMATS) diff --git a/rhizome.c b/rhizome.c index ec6acf10..83042382 100644 --- a/rhizome.c +++ b/rhizome.c @@ -130,16 +130,17 @@ int rhizome_manifest_check_sanity(rhizome_manifest *m_in) } else if (strcasecmp(service, RHIZOME_SERVICE_MESHMS) == 0) { if (sender == NULL || !sender[0]) return WHY("MeshMS Manifest missing 'sender' field"); - if (!validateSid(sender)) - return WHY("MeshMS Manifest contains invalid 'sender' field"); + if (!str_is_subscriber_id(sender)) + return WHYF("MeshMS Manifest contains invalid 'sender' field: %s", sender); if (recipient == NULL || !recipient[0]) return WHY("MeshMS Manifest missing 'recipient' field"); - if (!validateSid(recipient)) - return WHY("MeshMS Manifest contains invalid 'recipient' field"); + if (!str_is_subscriber_id(recipient)) + return WHYF("MeshMS Manifest contains invalid 'recipient' field: %s", recipient); } else { return WHY("Invalid service type"); } - if (debug & DEBUG_RHIZOME) DEBUGF("sender='%s'", sender ? sender : "(null)"); + if (debug & DEBUG_RHIZOME) + DEBUGF("sender='%s'", sender ? sender : "(null)"); /* passes all sanity checks */ return 0; diff --git a/serval.h b/serval.h index 45f81c29..eac2fac5 100755 --- a/serval.h +++ b/serval.h @@ -685,7 +685,9 @@ int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t bytes); int hexvalue(char c); char *str_toupper_inplace(char *s); -int validateSid(const char *sid); +int str_is_subscriber_id(const char *sid); +int strn_is_subscriber_id(const char *sid, size_t *lenp); + int stowSid(unsigned char *packet, int ofs, const char *sid); int stowDid(unsigned char *packet,int *ofs,char *did); int isFieldZeroP(unsigned char *packet,int start,int count);