chiark / gitweb /
server/servutil.c: Add utilities for simple leaky-bucket rate limiting.
[tripe] / server / servutil.c
index ed73e6d2bb8a5985157ff0e30ce0fe3c139ac01c..72de38a2f9d47ece2d6110f92db1b1c39c9c7ccd 100644 (file)
 
 octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
 
-/*----- Main code ---------------------------------------------------------*/
-
-/* --- @timestr@ --- *
- *
- * Arguments:  @time_t t@ = a time to convert
- *
- * Returns:    A pointer to a textual representation of the time.
- *
- * Use:                Converts a time to a textual representation.  Corrupts
- *             @buf_u@.
- */
-
-const char *timestr(time_t t)
-{
-  struct tm *tm;
-  if (!t)
-    return ("NEVER");
-  tm = localtime(&t);
-  strftime((char *)buf_u, sizeof(buf_u), "%Y-%m-%dT%H:%M:%S", tm);
-  return ((const char *)buf_u);
-}
-
-/* --- @mystrieq@ --- *
- *
- * Arguments:  @const char *x, *y@ = two strings
- *
- * Returns:    True if @x@ and @y are equal, up to case.
- */
-
-int mystrieq(const char *x, const char *y)
-{
-  for (;;) {
-    if (!*x && !*y) return (1);
-    if (tolower((unsigned char)*x) != tolower((unsigned char)*y))
-      return (0);
-    x++; y++;
-  }
-}
+/*----- Sequence numbers --------------------------------------------------*/
 
 /* --- @seq_reset@ --- *
  *
@@ -119,4 +82,107 @@ int seq_check(seqwin *s, uint32 q, const char *service)
   return (0);
 }
 
+/*----- Rate limiting -----------------------------------------------------*/
+
+/* --- @ratelim_init@ --- *
+ *
+ * Arguments:  @ratelim *r@ = rate-limiting state to fill in
+ *             @unsigned persec@ = credit to accumulate per second
+ *             @unsigned max@ = maximum credit to retain
+ *
+ * Returns:    ---
+ *
+ * Use:                Initialize a rate-limiting state.
+ */
+
+void ratelim_init(ratelim *r, unsigned persec, unsigned max)
+{
+  r->n = r->max = max;
+  r->persec = persec;
+  gettimeofday(&r->when, 0);
+}
+
+/* --- @ratelim_withdraw@ --- *
+ *
+ * Arguments:  @ratelim *r@ = rate-limiting state
+ *             @unsigned n@ = credit to withdraw
+ *
+ * Returns:    Zero if successful; @-1@ if there is unsufficient credit
+ *
+ * Use:                Updates the state with any accumulated credit.  Then, if
+ *             there there are more than @n@ credits available, withdraw @n@
+ *             and return successfully; otherwise, report failure.
+ */
+
+int ratelim_withdraw(ratelim *r, unsigned n)
+{
+  struct timeval now, delta;
+  unsigned long d;
+
+  gettimeofday(&now, 0);
+  TV_SUB(&delta, &now, &r->when);
+  d = (unsigned long)r->persec*delta.tv_sec +
+    (unsigned long)r->persec*delta.tv_usec/MILLION;
+  if (d < r->max - r->n) r->n += d;
+  else r->n = r->max;
+  r->when = now;
+
+  if (n > r->n) return (-1);
+  else { r->n -= n; return (0); }
+}
+
+/*----- Random odds and sods ----------------------------------------------*/
+
+/* --- @timestr@ --- *
+ *
+ * Arguments:  @time_t t@ = a time to convert
+ *
+ * Returns:    A pointer to a textual representation of the time.
+ *
+ * Use:                Converts a time to a textual representation.  Corrupts
+ *             @buf_u@.
+ */
+
+const char *timestr(time_t t)
+{
+  struct tm *tm;
+  if (!t)
+    return ("NEVER");
+  tm = localtime(&t);
+  strftime((char *)buf_u, sizeof(buf_u), "%Y-%m-%dT%H:%M:%S", tm);
+  return ((const char *)buf_u);
+}
+
+/* --- @mystrieq@ --- *
+ *
+ * Arguments:  @const char *x, *y@ = two strings
+ *
+ * Returns:    True if @x@ and @y are equal, up to case.
+ */
+
+int mystrieq(const char *x, const char *y)
+{
+  for (;;) {
+    if (!*x && !*y) return (1);
+    if (tolower((unsigned char)*x) != tolower((unsigned char)*y))
+      return (0);
+    x++; y++;
+  }
+}
+
+/* --- @addrsz@ --- *
+ *
+ * Arguments:  @const addr *a@ = a network address
+ *
+ * Returns:    The size of the address, for passing into the sockets API.
+ */
+
+socklen_t addrsz(const addr *a)
+{
+  switch (a->sa.sa_family) {
+    case AF_INET: return (sizeof(a->sin));
+    default: abort();
+  }
+}
+
 /*----- That's all, folks -------------------------------------------------*/