aboutsummaryrefslogtreecommitdiffstats
path: root/docs/network-scripts.tex
blob: 4e713953a8fa2bf88d7f01fc0ee67158b4b66d76 (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
\subsubsection{Using the network scripts}

To be able to access the network functions, you need to include
the necessary shell scripts by running:

\begin{Verbatim}
. /etc/functions.sh      # common functions
include /lib/network     # include /lib/network/*.sh
scan_interfaces          # read and parse the network config
\end{Verbatim}

Some protocols, such as PPP might change the configured interface names
at run time (e.g. \texttt{eth0} => \texttt{ppp0} for PPPoE). That's why you have to run
\texttt{scan\_interfaces} instead of reading the values from the config directly.
After running \texttt{scan\_interfaces}, the \texttt{'ifname'} option will always contain
the effective interface name (which is used for IP traffic) and if the
physical device name differs from it, it will be stored in the \texttt{'device'}
option.
That means that running \texttt{config\_get lan ifname}
after \texttt{scan\_interfaces} might not return the same result as running it before.

After running \texttt{scan\_interfaces}, the following functions are available:

\begin{itemize}
    \item{\texttt{find\_config \textit{interface}}} \\
        looks for a network configuration that includes
        the specified network interface.

    \item{\texttt{setup\_interface \textit{interface [config] [protocol]}}} \\
      will set up the specified interface, optionally overriding the network configuration
      name or the protocol that it uses.
\end{itemize}

\subsubsection{Writing protocol handlers}

You can add custom protocol handlers (e.g: PPPoE, PPPoA, ATM, PPTP ...)
by adding shell scripts to \texttt{/lib/network}. They provide the following
two shell functions:

\begin{Verbatim}
scan_<protocolname>() {
    local config="$1"
    # change the interface names if necessary
}

setup_interface_<protocolname>() {
    local interface="$1"
    local config="$2"
    # set up the interface
}
\end{Verbatim}

\texttt{scan\_\textit{protocolname}} is optional and only necessary if your protocol
uses a custom device, e.g. a tunnel or a PPP device.
pan> const char *path, const char *name, const char *prop, size_t sz) { #define DEBUG_PROP #ifndef DEBUG_PROP if ( path[0] == '/' && path[1] == '\0' ) { path = ""; } printk("%s: %s/%s: 0x%lx\n", head, path, name, sz); #else int i; int isstr = sz; const char *b = prop; for ( i = 0; i < sz; i++ ) { /* see if there is any non printable characters */ if ( !isprint(b[i]) ) { /* not printable */ if (b[i] != '\0' || (i + 1) != sz) { /* not the end of string */ isstr = 0; break; } } } if ( isstr > 0 ) { printk("%s: \t%s\n", head, b); } else if ( sz != 0 ) { printk("%s: \t0x", head); for ( i = 0; i < sz; i++ ) { if ( (i % 4) == 0 && i != 0 ) { if ( (i % 16) == 0 && i != 0 ) { printk("\n%s: \t0x", head); } else { printk(" 0x"); } } if (b[i] < 0x10) { printk("0"); } printk("%x", b[i]); } printk("\n"); } #endif } void ofd_dump_props(void *mem, const char *pre, ofdn_t n, int dump) { ofdn_t p; char name[128]; char prop[256] __attribute__ ((aligned (__alignof__ (u64)))); int sz; const char *path; if ( n == OFD_ROOT ) { path = ""; } else { path = ofd_node_path(mem, n); } if (dump & OFD_DUMP_NAMES) { printk("%s: %s: phandle 0x%x\n", pre, path, n); } p = ofd_nextprop(mem, n, NULL, name); while ( p > 0 ) { sz = ofd_getprop(mem, n, name, prop, sizeof (prop)); if ( sz > 0 && sz > sizeof (prop) ) { sz = sizeof (prop); } if ( dump & OFD_DUMP_VALUES ) { ofd_prop_print(pre, path, name, prop, sz); } p = ofd_nextprop(mem, n, name, name); } } void ofd_walk(void *m, const char *pre, ofdn_t p, walk_fn fn, int arg) { ofdn_t n; if ( fn != NULL ) { (*fn)(m, pre, p, arg); } /* child */ n = ofd_node_child(m, p); if ( n != 0 ) { ofd_walk(m, pre, n, fn, arg); } /* peer */ n = ofd_node_peer(m, p); if ( n != 0 ) { ofd_walk(m, pre, n, fn, arg); } }