4 #include "sgwDynSpeed.h"
21 #define SPEED_UPLINK_NAME "upstream"
22 #define SPEED_DOWNLINK_NAME "downstream"
24 /** the maximal length of a line that is read from the file */
25 #define LINE_LENGTH 256
27 /** regular expression describing a comment */
28 static const char * regexCommentString = "^([[:space:]]*|[[:space:]#]+.*)$";
30 /** regular expression describing a key/value pair */
31 static const char * regexNameValueString =
32 "^[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*([[:digit:]]+)[[:space:]]*$";
34 /** the number of matches in regexNameValueString */
35 static const size_t regexNameValuematchCount = 3;
37 /** the compiled regular expression describing a comment */
38 static regex_t regexComment;
40 /** the compiled regular expression describing a key/value pair */
41 static regex_t regexNameValue;
43 /** true when the plugin has been started */
44 static bool started = false;
46 /** type to hold the cached stat result */
47 typedef struct _CachedStat {
48 time_t timeStamp; /* Time of last modification (second resolution) */
51 /** the cached stat result */
52 static CachedStat cachedStat;
55 Read an unsigned long number from a value string
60 the string to convert to a number
62 a pointer to the location where to store the number upon successful conversion
68 static bool readUL(const char * valueName, const char * value, unsigned long * valueNumber) {
70 unsigned long valueNew;
72 assert(valueName != NULL);
73 assert(value != NULL);
74 assert(valueNumber != NULL);
77 valueNew = strtoul(value, &endPtr, 10);
79 if (!((endPtr != value) && (*value != '\0') && (*endPtr == '\0'))) {
80 /* invalid conversion */
81 sgwDynSpeedError(false, "Value of parameter %s (%s) could not be converted to a number", valueName, value);
85 *valueNumber = valueNew;
91 * Initialises the speedFile reader.
92 * @return true upon success, false otherwise
94 bool startSpeedFile(void) {
99 if (regcomp(®exComment, regexCommentString, REG_EXTENDED | REG_ICASE)) {
100 sgwDynSpeedError(false, "Could not compile regex \"%s\"", regexCommentString);
104 if (regcomp(®exNameValue, regexNameValueString, REG_EXTENDED | REG_ICASE)) {
105 sgwDynSpeedError(false, "Could not compile regex \"%s\"", regexNameValueString);
106 regfree(®exComment);
110 cachedStat.timeStamp = -1;
117 * Cleans up the speedFile reader.
119 void stopSpeedFile(void) {
121 regfree(®exNameValue);
122 regfree(®exComment);
128 * Performs a regex match
129 * @param regex the compiled regex to match against
130 * @param line the line to match
131 * @param nmatch the number of matches to produce
132 * @param pmatch the array with match information
133 * @return true upon success, false otherwise
135 static bool regexMatch(regex_t * regex, char * line, size_t nmatch, regmatch_t pmatch[]) {
136 int result = regexec(regex, line, nmatch, pmatch, 0);
141 if (result == REG_NOMATCH) {
147 regerror(result, regex, msgbuf, sizeof(msgbuf));
148 sgwDynSpeedError(false, "Regex match failed: %s", msgbuf);
154 /** the buffer in which to store a line read from the file */
155 static char line[LINE_LENGTH];
158 * Read the speed file
159 * @param fileName the filename
161 void readSpeedFile(char * fileName) {
165 unsigned int lineNumber = 0;
168 unsigned long uplink = DEF_UPLINK_SPEED;
169 unsigned long downlink = DEF_DOWNLINK_SPEED;
170 bool uplinkSet = false;
171 bool downlinkSet = false;
173 fd = open(fileName, O_RDONLY);
175 /* could not access the file */
179 if (fstat(fd, &statBuf)) {
180 /* could not access the file */
184 if (!memcmp(&cachedStat.timeStamp, &statBuf.st_mtime, sizeof(cachedStat.timeStamp))) {
185 /* file did not change since last read */
189 fp = fdopen(fd, "r");
194 memcpy(&cachedStat.timeStamp, &statBuf.st_mtime, sizeof(cachedStat.timeStamp));
196 while (fgets(line, LINE_LENGTH, fp)) {
197 regmatch_t pmatch[regexNameValuematchCount];
201 if (regexMatch(®exComment, line, 0, NULL)) {
205 if (!regexMatch(®exNameValue, line, regexNameValuematchCount, pmatch)) {
206 sgwDynSpeedError(false, "Gateway speed file \"%s\", line %d uses invalid syntax: %s", fileName, lineNumber,
211 /* determine name/value */
212 name = &line[pmatch[1].rm_so];
213 line[pmatch[1].rm_eo] = '\0';
214 value = &line[pmatch[2].rm_so];
215 line[pmatch[2].rm_eo] = '\0';
217 if (!strncasecmp(SPEED_UPLINK_NAME, name, sizeof(line))) {
218 if (!readUL(SPEED_UPLINK_NAME, value, &uplink)) {
222 } else if (!strncasecmp(SPEED_DOWNLINK_NAME, name, sizeof(line))) {
223 if (!readUL(SPEED_DOWNLINK_NAME, value, &downlink)) {
228 sgwDynSpeedError(false, "Gateway speed file \"%s\", line %d uses an invalid option \"%s\","
229 " valid options are [%s|%s]", fileName, lineNumber, name, SPEED_UPLINK_NAME, SPEED_DOWNLINK_NAME);
238 olsr_cnf->smart_gw_uplink = uplink;
241 olsr_cnf->smart_gw_downlink = downlink;
243 if (uplinkSet || downlinkSet) {
244 refresh_smartgw_netmask();