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