1/*
2 * Copyright (c) 1999 Apple 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 * hostlist.c
25 * - in-core host entry list manipulation routines
26 * - these are used for storing the in-core version of the
27 *   file-based host list and the in-core ignore list
28 */
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <ctype.h>
33#include <errno.h>
34#include <mach/boolean.h>
35#include <netdb.h>
36#include <sys/socket.h>
37#include <sys/ioctl.h>
38#include <netinet/in.h>
39#include <netinet/in_systm.h>
40#include <netinet/ip.h>
41#include <netinet/udp.h>
42#include <net/if.h>
43#include <netinet/if_ether.h>
44#include <arpa/inet.h>
45#include <signal.h>
46#include <string.h>
47#include <sys/file.h>
48#include <sys/stat.h>
49#include <sys/time.h>
50#include <sys/types.h>
51#include <sys/uio.h>
52#include <syslog.h>
53#include "hostlist.h"
54
55void
56hostinsert(struct hosts * * hosts, struct hosts * hp)
57{
58    hp->next = *hosts;
59    hp->prev = NULL;
60    if (*hosts)
61	(*hosts)->prev = hp;
62    *hosts = hp;
63}
64
65void
66hostremove(struct hosts * * hosts, struct hosts * hp)
67{
68    if (hp->prev)
69	hp->prev->next = hp->next;
70    else
71	*hosts = hp->next;
72    if (hp->next)
73	hp->next->prev = hp->prev;
74}
75
76void
77hostprint(struct hosts * hp)
78{
79    if (hp)
80	syslog(LOG_INFO,
81	       "hw %s type %d len %d ip %s host '%s' bootfile '%s'\n",
82	       ether_ntoa((struct ether_addr *)&hp->haddr), hp->htype,
83	       hp->hlen, inet_ntoa(hp->iaddr),
84	       hp->hostname ? hp->hostname : "",
85	       hp->bootfile ? hp->bootfile : "");
86}
87
88void
89hostfree(struct hosts * * hosts,
90	 struct hosts *hp
91	 )
92{
93    hostremove(hosts, hp);
94    if (hp->hostname) {
95	free(hp->hostname);
96	hp->hostname = NULL;
97    }
98    if (hp->bootfile) {
99	free(hp->bootfile);
100	hp->bootfile = NULL;
101    }
102    free((char *)hp);
103}
104
105void
106hostlistfree(struct hosts * * hosts)
107{
108    while (*hosts)
109	hostfree(hosts, *hosts); /* pop off the head of the queue */
110    return;
111}
112
113struct hosts *
114hostadd(struct hosts * * hosts, struct timeval * tv_p, int htype,
115	char * haddr, int hlen, struct in_addr * iaddr_p,
116	char * hostname, char * bootfile)
117{
118    struct hosts * hp;
119
120    hp = (struct hosts *)malloc(sizeof(*hp));
121    if (!hp)
122	return (NULL);
123    bzero(hp, sizeof(*hp));
124    if (tv_p)
125	hp->tv = *tv_p;
126    hp->htype = htype;
127    hp->hlen = hlen;
128    if (hlen > sizeof(hp->haddr)) {
129	hlen = sizeof(hp->haddr);
130    }
131    bcopy(haddr, &hp->haddr, hlen);
132    if (iaddr_p)
133	hp->iaddr = *iaddr_p;
134    if (hostname)
135	hp->hostname = strdup(hostname);
136    if (bootfile)
137	hp->bootfile = strdup(bootfile);
138    hostinsert(hosts, hp);
139    return (hp);
140}
141