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