discarded unused functions
[strongswan.git] / src / starter / interfaces.c
1 /* strongSwan IPsec interfaces management
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * RCSID $Id: interfaces.c,v 1.15 2006/02/05 10:51:55 as Exp $
15 */
16
17 #include <sys/socket.h>
18 #include <sys/ioctl.h>
19 #include <linux/if.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <errno.h>
24
25 #include <freeswan.h>
26 #include <ipsec_tunnel.h>
27
28 #include <constants.h>
29 #include <defs.h>
30 #include <log.h>
31
32 #include "interfaces.h"
33 #include "exec.h"
34 #include "files.h"
35
36 /*
37 * discover the default route via /proc/net/route
38 */
39 void
40 get_defaultroute(defaultroute_t *defaultroute)
41 {
42 FILE *fd;
43 char line[BUF_LEN];
44 bool first = TRUE;
45
46 memset(defaultroute, 0, sizeof(defaultroute_t));
47
48 fd = fopen("/proc/net/route", "r");
49
50 if (!fd)
51 {
52 plog("could not open 'proc/net/route'");
53 return;
54 }
55
56 while (fgets(line, sizeof(line), fd) != 0)
57 {
58 char iface[11];
59 char destination[9];
60 char gateway[11];
61 char flags[5];
62 char mask[9];
63
64 int refcnt;
65 int use;
66 int metric;
67 int items;
68
69 /* proc/net/route returns IP addresses in host order */
70 strcpy(gateway, "0h");
71
72 /* skip the header line */
73 if (first)
74 {
75 first = FALSE;
76 continue;
77 }
78
79 /* parsing a single line of proc/net/route */
80 items = sscanf(line, "%10s\t%8s\t%8s\t%5s\t%d\t%d\t%d\t%8s\t"
81 , iface, destination, gateway+2, flags, &refcnt, &use, &metric, mask);
82 if (items < 8)
83 {
84 plog("parsing error while scanning /proc/net/route");
85 continue;
86 }
87
88 /* check for defaultroute (destination 0.0.0.0 and mask 0.0.0.0) */
89 if (streq(destination, "00000000") && streq(mask, "00000000"))
90 {
91 if (defaultroute->defined)
92 {
93 plog("multiple default routes - cannot cope with %%defaultroute!!!");
94 defaultroute->defined = FALSE;
95 fclose(fd);
96 return;
97 }
98 ttoaddr(gateway, strlen(gateway), AF_INET, &defaultroute->nexthop);
99 strncpy(defaultroute->iface, iface, IFNAMSIZ);
100 defaultroute->defined = TRUE;
101 }
102 }
103 fclose(fd);
104
105 if (!defaultroute->defined)
106 {
107 plog("no default route - cannot cope with %%defaultroute!!!");
108 }
109 else
110 {
111 char addr_buf[20], nexthop_buf[20];
112 struct ifreq physreq;
113
114 int sock = socket(AF_INET, SOCK_DGRAM, 0);
115
116 /* determine IP address of iface */
117 if (sock < 0)
118 {
119 plog("could not open SOCK_DGRAM socket");
120 defaultroute->defined = FALSE;
121 return;
122 }
123 memset ((void*)&physreq, 0, sizeof(physreq));
124 strncpy(physreq.ifr_name, defaultroute->iface, IFNAMSIZ);
125 ioctl(sock, SIOCGIFADDR, &physreq);
126 close(sock);
127 defaultroute->addr.u.v4 = *((struct sockaddr_in *)&physreq.ifr_addr);
128
129 addrtot(&defaultroute->addr, 0, addr_buf, sizeof(addr_buf));
130 addrtot(&defaultroute->nexthop, 0, nexthop_buf, sizeof(nexthop_buf));
131
132 DBG(DBG_CONTROL,
133 DBG_log("Default route found: iface=%s, addr=%s, nexthop=%s"
134 , defaultroute->iface, addr_buf, nexthop_buf)
135 )
136
137 /* for backwards-compatibility with the awk shell scripts
138 * store the defaultroute in /var/run/ipsec.info
139 */
140 fd = fopen(INFO_FILE, "w");
141
142 if (fd)
143 {
144 fprintf(fd, "defaultroutephys=%s\n", defaultroute->iface );
145 fprintf(fd, "defaultroutevirt=ipsec0\n");
146 fprintf(fd, "defaultrouteaddr=%s\n", addr_buf);
147 fprintf(fd, "defaultroutenexthop=%s\n", nexthop_buf);
148 fclose(fd);
149 }
150 }
151 return;
152 }