and added files
[bcm963xx.git] / userapps / opensource / zebra / zebra / ipforward_proc.c
1 /*
2  * Fetch ipforward value by reading /proc filesystem.
3  * Copyright (C) 1997 Kunihiro Ishiguro
4  *
5  * This file is part of GNU Zebra.
6  *
7  * GNU Zebra is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2, or (at your option) any
10  * later version.
11  *
12  * GNU Zebra is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Zebra; see the file COPYING.  If not, write to the Free
19  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20  * 02111-1307, USA.  
21  */
22
23 #if BRCM_CMD_SUPPORT
24
25 #include <zebra.h>
26
27 char proc_net_snmp[] = "/proc/net/snmp";
28
29 static void
30 dropline (FILE *fp)
31 {
32   int c;
33
34   while ((c = getc (fp)) != '\n')
35     ;
36 }
37
38 int
39 ipforward ()
40 {
41   FILE *fp;
42   int ipforwarding = 0;
43   char *pnt;
44   char buf[10];
45
46   fp = fopen (proc_net_snmp, "r");
47
48   if (fp == NULL)
49     return -1;
50
51   /* We don't care about the first line. */
52   dropline (fp);
53   
54   /* Get ip_statistics.IpForwarding : 
55      1 => ip forwarding enabled 
56      2 => ip forwarding off. */
57   pnt = fgets (buf, 6, fp);
58   sscanf (buf, "Ip: %d", &ipforwarding);
59
60   if (ipforwarding == 1)
61     return 1;
62
63   return 0;
64 }
65
66 /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
67 char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
68
69 int
70 ipforward_on ()
71 {
72   FILE *fp;
73
74   fp = fopen (proc_ipv4_forwarding, "w");
75   
76   if (fp == NULL)
77     return -1;
78
79   fprintf (fp, "1\n");
80
81   fclose (fp);
82
83   return ipforward ();
84 }
85
86 int
87 ipforward_off ()
88 {
89   FILE *fp;
90
91   fp = fopen (proc_ipv4_forwarding, "w");
92   
93   if (fp == NULL)
94     return -1;
95
96   fprintf (fp, "0\n");
97
98   fclose (fp);
99
100   return ipforward ();
101 }
102 #ifdef HAVE_IPV6
103
104 char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
105
106 int
107 ipforward_ipv6 ()
108 {
109   FILE *fp;
110   char buf[5];
111   int ipforwarding = 0;
112
113   fp = fopen (proc_ipv6_forwarding, "r");
114
115   if (fp == NULL)
116     return -1;
117
118   fgets (buf, 2, fp);
119   sscanf (buf, "%d", &ipforwarding);
120
121   return ipforwarding;
122 }
123
124 int
125 ipforward_ipv6_on ()
126 {
127   FILE *fp;
128
129   fp = fopen (proc_ipv6_forwarding, "w");
130   
131   if (fp == NULL)
132     return -1;
133
134   fprintf (fp, "1\n");
135
136   fclose (fp);
137
138   return ipforward_ipv6 ();
139 }
140
141 int
142 ipforward_ipv6_off ()
143 {
144   FILE *fp;
145
146   fp = fopen (proc_ipv6_forwarding, "w");
147   
148   if (fp == NULL)
149     return -1;
150
151   fprintf (fp, "0\n");
152
153   fclose (fp);
154
155   return ipforward_ipv6 ();
156 }
157 #endif /* HAVE_IPV6 */
158
159 #endif /* BRCM_CMD_SUPPORT */