ipsec_types: Add utility function to parse mark_t from strings

This commit is contained in:
Tobias Brunner
2013-10-11 15:32:44 +02:00
parent 6d7710a744
commit 434e530f75
4 changed files with 98 additions and 46 deletions
+36 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012 Tobias Brunner
* Copyright (C) 2012-2013 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
@@ -36,3 +36,38 @@ ENUM(ipcomp_transform_names, IPCOMP_NONE, IPCOMP_LZJH,
"IPCOMP_LZS",
"IPCOMP_LZJH"
);
/*
* See header
*/
bool mark_from_string(const char *value, mark_t *mark)
{
char *endptr;
if (!value)
{
return FALSE;
}
mark->value = strtoul(value, &endptr, 0);
if (*endptr)
{
if (*endptr != '/')
{
DBG1(DBG_APP, "invalid mark value: %s", value);
return FALSE;
}
mark->mask = strtoul(endptr+1, &endptr, 0);
if (*endptr)
{
DBG1(DBG_LIB, "invalid mark mask: %s", endptr);
return FALSE;
}
}
else
{
mark->mask = 0xffffffff;
}
/* apply the mask to ensure the value is in range */
mark->value &= mark->mask;
return TRUE;
}
+10 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012 Tobias Brunner
* Copyright (C) 2012-2013 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
@@ -169,4 +169,13 @@ struct mark_t {
*/
#define MARK_REQID (0xFFFFFFFF)
/**
* Try to parse a mark_t from the given string of the form <mark>[/<mask>].
*
* @param value string to parse
* @param mark mark to fill
* @return TRUE if parsing was successful
*/
bool mark_from_string(const char *value, mark_t *mark);
#endif /** IPSEC_TYPES_H_ @}*/