move plugin_radius_reset from *ctl to auto* plugins
[l2tpns.git] / autothrottle.c
index 9096691..051b9a8 100644 (file)
@@ -4,7 +4,16 @@
 
 /* set up throttling based on RADIUS reply */
 
-char const *cvs_id = "$Id: autothrottle.c,v 1.12 2004-11-30 05:49:47 bodea Exp $";
+/*
+ * lcp:interface-config#1=service-policy input N
+ * lcp:interface-config#2=service-policy output N
+ *
+ * throttle=N
+ * throttle=yes (use throttle_rate from config)
+ * throttle=no
+ */
+
+char const *cvs_id = "$Id: autothrottle.c,v 1.15 2005-10-11 07:59:09 bodea Exp $";
 
 int plugin_api_version = PLUGIN_API_VERSION;
 struct pluginfuncs *p;
@@ -31,7 +40,7 @@ int plugin_radius_response(struct param_radius_response *data)
                    strncmp("output", data->value, sp - data->value)))
                {
                        p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
-                               "         Not throttling user (invalid type %s)\n",
+                               "         Not throttling user (invalid type %.*s)\n",
                                sp - data->value, data->value);
 
                        return PLUGIN_RET_OK;
@@ -69,36 +78,44 @@ int plugin_radius_response(struct param_radius_response *data)
 
        if (!strcmp(data->key, "throttle"))
        {
-               if (!strcmp(data->value, "yes"))
+               char *e;
+               int rate;
+
+               if ((rate = strtol(data->value, &e, 10)) < 0 || *e)
                {
-                       unsigned long *rate = p->getconfig("throttle_speed", UNSIGNED_LONG);
-                       if (rate)
+                       rate = -1;
+                       if (!strcmp(data->value, "yes"))
                        {
-                               if (*rate)
-                                       p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
-                                               "         Throttling user to %dkb/s\n", *rate);
-                               else
-                                       p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
-                                               "         Not throttling user (throttle_speed=0)\n");
-
-                               data->s->throttle_in = data->s->throttle_out = *rate;
+                               unsigned long *ts = p->getconfig("throttle_speed", UNSIGNED_LONG);
+                               if (ts)
+                                       rate = *ts;
                        }
-                       else
-                               p->log(1, p->get_id_by_session(data->s), data->s->tunnel,
-                                       "         Not throttling user (can't get throttle_speed)\n");
+                       else if (!strcmp(data->value, "no"))
+                               rate = 0;
                }
-               else if (!strcmp(data->value, "no"))
-               {
+
+               if (rate < 0)
+                       return PLUGIN_RET_OK;
+
+               if (rate)
+                       p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
+                               "         Throttling user to %dkb/s\n", rate);
+               else
                        p->log(3, p->get_id_by_session(data->s), data->s->tunnel,
                                "         Not throttling user\n");
 
-                       data->s->throttle_in = data->s->throttle_out = 0;
-               }
+               data->s->throttle_in = data->s->throttle_out = rate;
        }
 
        return PLUGIN_RET_OK;
 }
 
+int plugin_radius_reset(struct param_radius_reset *data)
+{
+       p->throttle(p->get_id_by_session(data->s), 0, 0);
+       return PLUGIN_RET_OK;
+}
+
 int plugin_init(struct pluginfuncs *funcs)
 {
        return ((p = funcs)) ? 1 : 0;