| 228 | }; |
| 229 | |
| 230 | static const char *rate_limit_check(const tal_t *ctx, |
| 231 | const struct rune *rune, |
| 232 | const struct rune_altern *alt, |
| 233 | struct cond_info *cinfo) |
| 234 | { |
| 235 | unsigned long r; |
| 236 | char *endp; |
| 237 | if (alt->condition != '=') |
| 238 | return "rate operator must be ="; |
| 239 | |
| 240 | r = strtoul(alt->value, &endp, 10); |
| 241 | if (endp == alt->value || *endp || r == 0 || r >= UINT32_MAX) |
| 242 | return "malformed rate"; |
| 243 | |
| 244 | /* We cache this: we only add usage counter if whole rune succeeds! */ |
| 245 | if (!cinfo->usage) { |
| 246 | cinfo->usage = usage_table_get(&usage_table, atol(rune->unique_id)); |
| 247 | if (!cinfo->usage) { |
| 248 | cinfo->usage = tal(plugin, struct usage); |
| 249 | cinfo->usage->id = atol(rune->unique_id); |
| 250 | cinfo->usage->counter = 0; |
| 251 | usage_table_add(&usage_table, cinfo->usage); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* >= becuase if we allow this, counter will increment */ |
| 256 | if (cinfo->usage->counter >= r) |
| 257 | return tal_fmt(ctx, "Rate of %lu per minute exceeded", r); |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | static const char *check_condition(const tal_t *ctx, |
| 262 | const struct rune *rune, |