]> git.saurik.com Git - apple/network_cmds.git/blame - bootparams/bootparamd.tproj/bootparamd.c
network_cmds-307.1.1.tar.gz
[apple/network_cmds.git] / bootparams / bootparamd.tproj / bootparamd.c
CommitLineData
b7080c8e
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
a2c93a76
A
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.
b7080c8e
A
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,
a2c93a76
A
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."
b7080c8e
A
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>
b8dff150 39#include <sys/param.h>
b7080c8e
A
40#include <netdb.h>
41#include <unistd.h>
42#include <stdlib.h>
43#include <netinet/in.h>
44#include <arpa/inet.h>
45
46int debug = 0;
47char *progname;
48
49unsigned long route_addr;
50char domain_name[MAXHOSTNAMELEN];
51
52int _rpcsvcdirty;
53extern void bootparamprog_1();
54
55extern int getdomainname(char*, int);
56
57void
58usage(void)
59{
60 fprintf(stderr, "usage: %s [-d] [-r router]\n", progname);
61 exit(1);
62}
63
64int
65main(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