Replace validateSid() with str_is_subscriber_id()

This commit is contained in:
Andrew Bettison 2012-07-12 16:14:25 +09:30
parent d111f763c7
commit 208a95a233
4 changed files with 31 additions and 34 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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;

View File

@ -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);