| 1 | /* |
| 2 | * Copyright (c) 1986, 1988 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #ifndef lint |
| 35 | static char sccsid[] = "@(#)db_glue.c 4.4 (Berkeley) 6/1/90"; |
| 36 | #endif /* not lint */ |
| 37 | |
| 38 | #include <sys/param.h> |
| 39 | #include <sys/time.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <netinet/in.h> |
| 42 | #include <stdio.h> |
| 43 | #include <syslog.h> |
| 44 | #include <ctype.h> |
| 45 | #include <netdb.h> |
| 46 | #include <arpa/nameser.h> |
| 47 | #include "ns.h" |
| 48 | #include "db.h" |
| 49 | |
| 50 | struct valuelist { |
| 51 | struct valuelist *next, *prev; |
| 52 | char *name; |
| 53 | char *proto; |
| 54 | short port; |
| 55 | } *servicelist, *protolist; |
| 56 | |
| 57 | buildservicelist() |
| 58 | { |
| 59 | struct servent *sp; |
| 60 | struct valuelist *slp; |
| 61 | |
| 62 | setservent(1); |
| 63 | while (sp = getservent()) { |
| 64 | slp = (struct valuelist *)malloc(sizeof(struct valuelist)); |
| 65 | slp->name = savestr(sp->s_name); |
| 66 | slp->proto = savestr(sp->s_proto); |
| 67 | slp->port = ntohs((u_short)sp->s_port); |
| 68 | slp->next = servicelist; |
| 69 | slp->prev = NULL; |
| 70 | if (servicelist) |
| 71 | servicelist->prev = slp; |
| 72 | servicelist = slp; |
| 73 | } |
| 74 | endservent(); |
| 75 | } |
| 76 | |
| 77 | buildprotolist() |
| 78 | { |
| 79 | struct protoent *pp; |
| 80 | struct valuelist *slp; |
| 81 | |
| 82 | setprotoent(1); |
| 83 | while (pp = getprotoent()) { |
| 84 | slp = (struct valuelist *)malloc(sizeof(struct valuelist)); |
| 85 | slp->name = savestr(pp->p_name); |
| 86 | slp->port = pp->p_proto; |
| 87 | slp->next = protolist; |
| 88 | slp->prev = NULL; |
| 89 | if (protolist) |
| 90 | protolist->prev = slp; |
| 91 | protolist = slp; |
| 92 | } |
| 93 | endprotoent(); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Convert service name or (ascii) number to int. |
| 98 | */ |
| 99 | servicenumber(p) |
| 100 | char *p; |
| 101 | { |
| 102 | |
| 103 | return (findservice(p, &servicelist)); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Convert protocol name or (ascii) number to int. |
| 108 | */ |
| 109 | protocolnumber(p) |
| 110 | char *p; |
| 111 | { |
| 112 | |
| 113 | return (findservice(p, &protolist)); |
| 114 | } |
| 115 | |
| 116 | findservice(s, list) |
| 117 | register char *s; |
| 118 | register struct valuelist **list; |
| 119 | { |
| 120 | register struct valuelist *lp = *list; |
| 121 | int n; |
| 122 | |
| 123 | for (; lp != NULL; lp = lp->next) |
| 124 | if (strcasecmp(lp->name, s) == 0) { |
| 125 | if (lp != *list) { |
| 126 | lp->prev->next = lp->next; |
| 127 | if (lp->next) |
| 128 | lp->next->prev = lp->prev; |
| 129 | (*list)->prev = lp; |
| 130 | lp->next = *list; |
| 131 | *list = lp; |
| 132 | } |
| 133 | return(lp->port); |
| 134 | } |
| 135 | if (sscanf(s, "%d", &n) != 1 || n <= 0) |
| 136 | n = -1; |
| 137 | return(n); |
| 138 | } |
| 139 | |
| 140 | struct servent * |
| 141 | cgetservbyport(port, proto) |
| 142 | u_short port; |
| 143 | char *proto; |
| 144 | { |
| 145 | register struct valuelist **list = &servicelist; |
| 146 | register struct valuelist *lp = *list; |
| 147 | static struct servent serv; |
| 148 | |
| 149 | port = htons(port); |
| 150 | for (; lp != NULL; lp = lp->next) { |
| 151 | if (port != lp->port) |
| 152 | continue; |
| 153 | if (strcasecmp(lp->proto, proto) == 0) { |
| 154 | if (lp != *list) { |
| 155 | lp->prev->next = lp->next; |
| 156 | if (lp->next) |
| 157 | lp->next->prev = lp->prev; |
| 158 | (*list)->prev = lp; |
| 159 | lp->next = *list; |
| 160 | *list = lp; |
| 161 | } |
| 162 | serv.s_name = lp->name; |
| 163 | serv.s_port = htons((u_short)lp->port); |
| 164 | serv.s_proto = lp->proto; |
| 165 | return(&serv); |
| 166 | } |
| 167 | } |
| 168 | return(0); |
| 169 | } |
| 170 | |
| 171 | struct protoent * |
| 172 | cgetprotobynumber(proto) |
| 173 | register int proto; |
| 174 | { |
| 175 | |
| 176 | register struct valuelist **list = &protolist; |
| 177 | register struct valuelist *lp = *list; |
| 178 | static struct protoent prot; |
| 179 | |
| 180 | for (; lp != NULL; lp = lp->next) |
| 181 | if (lp->port == proto) { |
| 182 | if (lp != *list) { |
| 183 | lp->prev->next = lp->next; |
| 184 | if (lp->next) |
| 185 | lp->next->prev = lp->prev; |
| 186 | (*list)->prev = lp; |
| 187 | lp->next = *list; |
| 188 | *list = lp; |
| 189 | } |
| 190 | prot.p_name = lp->name; |
| 191 | prot.p_proto = lp->port; |
| 192 | return(&prot); |
| 193 | } |
| 194 | return(0); |
| 195 | } |
| 196 | |
| 197 | char * |
| 198 | protocolname(num) |
| 199 | int num; |
| 200 | { |
| 201 | static char number[8]; |
| 202 | struct protoent *pp; |
| 203 | |
| 204 | pp = cgetprotobynumber(num); |
| 205 | if(pp == 0) { |
| 206 | (void) sprintf(number, "%d", num); |
| 207 | return(number); |
| 208 | } |
| 209 | return(pp->p_name); |
| 210 | } |
| 211 | |
| 212 | char * |
| 213 | servicename(port, proto) |
| 214 | u_short port; |
| 215 | char *proto; |
| 216 | { |
| 217 | static char number[8]; |
| 218 | struct servent *ss; |
| 219 | |
| 220 | ss = cgetservbyport(htons(port), proto); |
| 221 | if(ss == 0) { |
| 222 | (void) sprintf(number, "%d", port); |
| 223 | return(number); |
| 224 | } |
| 225 | return(ss->s_name); |
| 226 | } |