]> git.saurik.com Git - apple/network_cmds.git/blob - bootparams/bootparamd.tproj/bootparamd.c
network_cmds-201.tar.gz
[apple/network_cmds.git] / bootparams / bootparamd.tproj / bootparamd.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * BOOTPARAMS server
27 * Copyright 1998, Apple Computer Inc. Unpublished.
28 *
29 * Written by Marc Majka
30 */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <rpc/rpc.h>
36 #include <rpc/pmap_clnt.h>
37 #include "bootparam_prot.h"
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netdb.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45
46 int debug = 0;
47 char *progname;
48
49 unsigned long route_addr;
50 char domain_name[MAXHOSTNAMELEN];
51
52 int _rpcsvcdirty;
53 extern void bootparamprog_1();
54
55 extern int getdomainname(char*, int);
56
57 void
58 usage(void)
59 {
60 fprintf(stderr, "usage: %s [-d] [-r router]\n", progname);
61 exit(1);
62 }
63
64 int
65 main(int argc, char *argv[])
66 {
67 SVCXPRT *transp;
68 int i;
69 struct hostent *h;
70 char str[256];
71 FILE *fp;
72
73 progname = strrchr(argv[0],'/');
74 if (progname) progname++;
75 else progname = argv[0];
76
77 route_addr = 0;
78
79 for (i = 1; i < argc; i++)
80 {
81 if (!strcmp(argv[i], "-d"))
82 {
83 debug = 1;
84 }
85 else if (!strcmp(argv[i], "-r"))
86 {
87 i++;
88 if (i >= argc) usage();
89
90 route_addr = inet_addr(argv[i]);
91 if (route_addr == -1)
92 {
93 h = gethostbyname(argv[i]);
94 if (h == NULL)
95 {
96 fprintf(stderr, "%s: Can't find host %s\n",
97 progname, argv[i]);
98 exit(1);
99 }
100
101 bcopy(h->h_addr, (char *)&route_addr, sizeof(route_addr));
102 }
103 }
104 else usage();
105 }
106
107 if (route_addr == 0)
108 {
109 fp = popen("/usr/sbin/netstat -r -n", "r");
110 while (NULL != fgets(str, 256, fp))
111 {
112 if (strncmp(str, "default ", 8)) continue;
113 if (!strncmp(str + 17, "link", 4)) continue;
114 route_addr = inet_addr(str+17);
115 break;
116 }
117 pclose(fp);
118 }
119
120 domain_name[0] = '\0';
121 if (getdomainname(domain_name, sizeof(domain_name)) != 0)
122 domain_name[0] = '\0';
123
124 if (debug == 0) daemon(0,0);
125
126 openlog(progname, 0, LOG_DAEMON);
127 pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
128
129 transp = svcudp_create(RPC_ANYSOCK);
130 if (transp == NULL)
131 {
132 syslog(LOG_ERR, "Can't create udp service.");
133 exit(1);
134 }
135
136 if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1, IPPROTO_UDP))
137 {
138 syslog(LOG_ERR, "Can't register service.");
139 exit(1);
140 }
141
142 svc_run();
143
144 syslog(LOG_ERR, "svc_run returned");
145 exit(1);
146 }
147
148