summaryrefslogtreecommitdiffstats
path: root/urunlevel/runlevel/network.c
blob: 99d4717e780202bbfee37ce41cdf7f03ed8a155b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Mini network boot implementation for busybox.
 *
 * Copyright (C) 2004 by David Seikel won_fang@yahoo.com.au
 *
 * Originally based on code from Trinux.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 *
 */

#include <string.h>
#include <unistd.h>

#include "busybox.h"
#include "lib_init_d.h"


static int start(struct init_d_handle_s *, int);
static int status(struct init_d_handle_s *init_d, int);
static int stop(struct init_d_handle_s *, int);


static init_d_handle_t my_commands = {
	&start,
	&stop,
	NULL,
	NULL,
	NULL,
	NULL,
	&status,
	NULL,
	"network",
	NULL,
	NULL,
	NULL,
	INIT_D_BEGIN
		INIT_D_PROV "$network"
		INIT_D_RSTART "$syslog"
		INIT_D_DSTART "3 4 5"
		INIT_D_DSTOP "0 1 2 6"
		INIT_D_SDESC "Basic network setup"
		INIT_D_DESC "Configures all your network interfaces,"
		INIT_D_CONT "brings them all up and sets up routing."
		INIT_D_CONT "Also configures things like port forwarding,"
		INIT_D_CONT "hostnames, etc." INIT_D_END
};


int network_main(int argc, char **argv)
{
	return do_init_from_main(argc, argv, &my_commands);
}


static int start(struct init_d_handle_s *init_d, int just_checking)
{
	int i;
	int my_status = (init_d->status) (init_d, 0);
	char *IPADDR = 0;

	if (my_status == INIT_D_STATUS_NOT_RUNNING) {
		my_status = INIT_D_ERROR_NOT_RUNNING;
//  if (stat("/proc/net/pnp", &path_stat) == 0)
//      copy_file("/proc/net/pnp", "/etc/resolv.conf", FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);

		fflush(stdout);
		doit(QUIET, "ifup -af");
		for (i = 20; stat("/var/lib/network/ipaddr", &path_stat) != 0; i--) {
			bb_printf(".");
			fflush(stdout);
			sleep(2);
			if (i <= 0) {
				bb_printf(" timeout.");
				break;
			}
		}

		if (stat("/var/lib/network/ipaddr", &path_stat) == 0)
			IPADDR = quick_read("/var/lib/network/ipaddr");

		if ((IPADDR != 0) && (IPADDR[0] != '\0')) {
			char *tcp_syn_retries = getenv("tcp_syn_retries");
			char *ip_forward = getenv("ip_forward");
			char *icmp_echo_ignore_all = getenv("icmp_echo_ignore_all");

			if (tcp_syn_retries)
				quick_write("/proc/sys/net/ipv4/tcp_syn_retries", tcp_syn_retries);
			if (ip_forward) {
				bb_printf("Enabling IP forwarding\n");
				quick_write("/proc/sys/net/ipv4/ip_forward", ip_forward);
			}
			if (icmp_echo_ignore_all)
				quick_write("/proc/sys/net/ipv4/icmp_echo_ignore_all", icmp_echo_ignore_all);
			my_status = INIT_D_OK;
		} else
			my_status = INIT_D_ERROR_GENERIC;

		free(IPADDR);
	} else
		my_status = INIT_D_OK;

	return my_status;
}


static int status(struct init_d_handle_s *init_d, int quiet)
{
	int my_status = INIT_D_STATUS_NOT_RUNNING;
	char *ip = NULL;

	bb_xasprintf(&ip, "%s",
				 doit(REDIR,
					  "ip -o addr | grep lo: | cut -d\" \" -f3 | cut -d\",\" -f2"));
	if ((ip != NULL) && (strncmp("UP", ip, 2) == 0))
		my_status = INIT_D_STATUS_OK;

	return print_status(init_d, quiet, my_status);
}


static int stop(struct init_d_handle_s *init_d, int just_checking)
{
	int my_status = default_stop(init_d, 1);

	if (my_status == INIT_D_ERROR_JUST_RUNNING) {
		doit(QUIET, "ifdown -af");
		my_status = INIT_D_OK;
	}
	return my_status;
}