Fix logging parameter errors
[l2tpns.git] / throttle.c
1 // L2TPNS Throttle Stuff
2 // $Id: throttle.c,v 1.2 2004/03/05 00:09:03 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 radiust *radius;
20 extern sessiont *session;
21 extern u32 sessionid;
22 extern int radfd;
23 extern tbft *filter_buckets;
24 extern struct configt *config;
25
26 // Throttle or Unthrottle a session
27 int throttle_session(sessionidt s, int throttle)
28 {
29 if (!config->rl_rate) return 0;
30
31 if (!*session[s].user)
32 return 0; // User not logged in
33
34 if (throttle)
35 {
36 // Throttle them
37 char cmd[2048] = {0};
38 if (!session[s].tbf) session[s].tbf = rl_get_tbf();
39 if (!session[s].tbf)
40 {
41 log(1, 0, s, session[s].tunnel, "Error creating a filtering bucket for user %s\n", session[s].user);
42 return 0;
43 }
44 log(2, 0, s, session[s].tunnel, "Throttling session %d for user %s\n", s, session[s].user);
45 snprintf(cmd, 2048, "iptables -t mangle -A throttle -d %s -j MARK --set-mark %d",
46 inet_toa(ntohl(session[s].ip)),
47 session[s].tbf);
48 log(4, 0, s, session[s].tunnel, "Running %s\n", cmd);
49 system(cmd);
50 }
51 else
52 {
53 char cmd[2048] = {0};
54 log(2, 0, s, session[s].tunnel, "Unthrottling session %d for user %s\n", s, session[s].user);
55 if (session[s].tbf)
56 {
57 int count = 10;
58 snprintf(cmd, 2048, "iptables -t mangle -D throttle -d %s -j MARK --set-mark %d", inet_toa(ntohl(session[s].ip)), session[s].tbf);
59 log(4, 0, s, session[s].tunnel, "Running %s\n", cmd);
60 while (--count)
61 {
62 int status = system(cmd);
63 if (WEXITSTATUS(status) != 0) break;
64 }
65 system(cmd);
66
67 rl_done_tbf(session[s].tbf);
68 session[s].tbf = 0;
69 }
70 }
71 session[s].throttle = throttle;
72 return 0;
73 }
74