ba551d249e9a2b8a5670da43fe72f298dd5edf23
[l2tpns.git] / throttle.c
1 // L2TPNS Throttle Stuff
2 // $Id: throttle.c,v 1.1 2003-12-16 07:07:39 fred_nerk Exp $
3
4 #include <stdio.h>
5 #include <sys/file.h>
6 #include <sys/stat.h>
7 #include <sys/wait.h>
8 #include <sys/types.h>
9 #include <malloc.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include <arpa/inet.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include "l2tpns.h"
17 #include "util.h"
18
19 extern char *radiussecret;
20 extern radiust *radius;
21 extern sessiont *session;
22 extern ipt radiusserver[MAXRADSERVER]; // radius servers
23 extern u32 sessionid;
24 extern u8 radiusfree;
25 extern int radfd;
26 extern u8 numradiusservers;
27 extern char debug;
28 extern unsigned long rl_rate;
29 extern tbft *filter_buckets;
30
31 // Throttle or Unthrottle a session
32 int throttle_session(sessionidt s, int throttle)
33 {
34 if (!rl_rate) return 0;
35
36 if (!*session[s].user)
37 return 0; // User not logged in
38
39 if (throttle)
40 {
41 // Throttle them
42 char cmd[2048] = {0};
43 log(2, 0, s, session[s].tunnel, "Throttling session %d for user %s\n", s, session[s].user);
44 if (!session[s].tbf) session[s].tbf = rl_get_tbf();
45 snprintf(cmd, 2048, "iptables -t mangle -A throttle -d %s -j MARK --set-mark %d", inet_toa(ntohl(session[s].ip)),
46 session[s].tbf);
47 log(4, 0, s, session[s].tunnel, "Running %s\n", cmd);
48 system(cmd);
49 }
50 else
51 {
52 char cmd[2048] = {0};
53 log(2, 0, s, session[s].tunnel, "Unthrottling session %d for user %s\n", s, session[s].user);
54 if (session[s].tbf)
55 {
56 int count = 10;
57 snprintf(cmd, 2048, "iptables -t mangle -D throttle -d %s -j MARK --set-mark %d", inet_toa(ntohl(session[s].ip)), session[s].tbf);
58 log(4, 0, s, session[s].tunnel, "Running %s\n", cmd);
59 while (--count)
60 {
61 int status = system(cmd);
62 if (WEXITSTATUS(status) != 0) break;
63 }
64 system(cmd);
65
66 rl_done_tbf(session[s].tbf);
67 session[s].tbf = 0;
68 }
69 }
70 session[s].throttle = throttle;
71 return 0;
72 }
73