OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / ifattach / ifattach.c
1 /* ifattach.c:
2  *
3  * Copied and hacked from loattach.c which was:
4  *
5  * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>
6  * Copyright (C) 1999  Greg Ungerer       <gerg@snapgear.com>
7  * Copyright (C) 1999  D. Jeff Dionne     <jeff@lineo.ca>
8  * Copyright (C) 2000  Lineo Inc.          (www.lineo.com)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <sys/time.h>
24 #include <dirent.h>
25 #include <errno.h>
26 #include <termios.h>
27
28 #include <fcntl.h>
29
30 #include <linux/sockios.h>
31 #include <linux/socket.h>
32 #include <linux/if.h>
33 #include <linux/in.h>
34 #include <linux/icmp.h>
35 #include <linux/route.h>
36
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <termios.h>
40 #include <signal.h>
41 #include <sys/time.h>
42
43 usage(char *name, int rc)
44 {
45         fprintf(stderr, "usage: %s --addr x.x.x.x [--mask x.x.x.x] "
46                 "[--net x.x.x.x] [--gw x.x.x.x] [--if x] [--verbose] [--help]\n"
47                 "usage: %s --addr x.x.x.x [--mask x.x.x.x] "
48                 "[--net x.x.x.x] [--gw x.x.x.x] iface\n",name,name);
49         exit(rc);
50 }
51
52 int main(int argc, char *argv[])
53 {
54         int i,f;
55         int verbose = 0;
56         char *p;
57         FILE *fp;
58         char ** a;
59         struct ifreq ifr;
60         struct sockaddr_in *in;
61         struct rtentry rt;
62         char * dev = "lo";
63         struct termios tty;
64         int speed;
65         char* ipAddr =  "127.0.0.1";
66         char* ipMask  = "255.0.0.0";
67         char* ipNet  =  "127.0.0.0";
68         char* ipGateway = NULL;
69
70         if (argc > 1) {
71             for(i=1; i<argc; i++) {
72                if (!strcmp(argv[i], "--addr")) {
73                    ipAddr = argv[++i];
74                } else if (!strcmp(argv[i], "--net")) {
75                    ipNet = argv[++i];
76                } else if (!strcmp(argv[i], "--mask")) {
77                    ipMask = argv[++i];
78                } else if (!strcmp(argv[i], "--gw")) {
79                    ipGateway = argv[++i];
80                } else if (!strcmp(argv[i], "--verbose")) {
81                    verbose++;
82                } else if (!strcmp(argv[i], "--if")) {
83                    dev = argv[++i];
84                } else if (argc == i+1) {
85                    dev = argv[i];
86                } else {
87                    usage(argv[0], strcmp(argv[i], "--help"));
88                }
89             }
90         }
91
92         open_raw_socket();
93
94         if ((ipAddr == NULL) && (ipGateway == NULL))
95                 usage(argv[0], 1);
96
97         if (verbose)
98                 printf("%s: address: %s, mask: %s, net: %s, gateway: %s\n",
99                         dev, ipAddr, ipMask, ipNet, ipGateway);
100         setifaddr(dev, ipAddr);
101         setifflags(dev, IFF_UP | IFF_RUNNING);
102
103         addroute(dev, RTF_UP/* | RTF_HOST*/,
104                 ipNet /* dest net */,
105                 ipMask /* netmask */,
106                 0 /* gateway */);
107
108         if (ipGateway) {
109                 addroute(dev, RTF_UP/* | RTF_HOST*/,
110                         "0.0.0.0" /* dest net */,
111                         "0.0.0.0" /* netmask */,
112                         ipGateway /* gateway */);
113         }
114
115         close_raw_socket();
116
117     exit(0);
118 }
119