|
|
1.1 root 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 are permitted provided
6: * that: (1) source distributions retain this entire copyright notice and
7: * comment, and (2) distributions including binaries display the following
8: * acknowledgement: ``This product includes software developed by the
9: * University of California, Berkeley and its contributors'' in the
10: * documentation or other materials provided with the distribution and in
11: * all advertising materials mentioning features or use of this software.
12: * Neither the name of the University nor the names of its contributors may
13: * be used to endorse or promote products derived from this software without
14: * specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #ifndef lint
21: static char sccsid[] = "@(#)db_glue.c 4.4 (Berkeley) 6/1/90";
22: #endif /* not lint */
23:
24: #include <sys/param.h>
25: #include <sys/time.h>
26: #include <sys/stat.h>
27: #include <netinet/in.h>
28: #include <stdio.h>
29: #include <syslog.h>
30: #include <ctype.h>
31: #include <netdb.h>
32: #include <arpa/nameser.h>
33: #include "ns.h"
34: #include "db.h"
35:
36: struct valuelist {
37: struct valuelist *next, *prev;
38: char *name;
39: char *proto;
40: short port;
41: } *servicelist, *protolist;
42:
43: buildservicelist()
44: {
45: struct servent *sp;
46: struct valuelist *slp;
47:
48: setservent(1);
49: while (sp = getservent()) {
50: slp = (struct valuelist *)malloc(sizeof(struct valuelist));
51: slp->name = savestr(sp->s_name);
52: slp->proto = savestr(sp->s_proto);
53: slp->port = ntohs((u_short)sp->s_port);
54: slp->next = servicelist;
55: slp->prev = NULL;
56: if (servicelist)
57: servicelist->prev = slp;
58: servicelist = slp;
59: }
60: endservent();
61: }
62:
63: buildprotolist()
64: {
65: struct protoent *pp;
66: struct valuelist *slp;
67:
68: setprotoent(1);
69: while (pp = getprotoent()) {
70: slp = (struct valuelist *)malloc(sizeof(struct valuelist));
71: slp->name = savestr(pp->p_name);
72: slp->port = pp->p_proto;
73: slp->next = protolist;
74: slp->prev = NULL;
75: if (protolist)
76: protolist->prev = slp;
77: protolist = slp;
78: }
79: endprotoent();
80: }
81:
82: /*
83: * Convert service name or (ascii) number to int.
84: */
85: servicenumber(p)
86: char *p;
87: {
88:
89: return (findservice(p, &servicelist));
90: }
91:
92: /*
93: * Convert protocol name or (ascii) number to int.
94: */
95: protocolnumber(p)
96: char *p;
97: {
98:
99: return (findservice(p, &protolist));
100: }
101:
102: findservice(s, list)
103: register char *s;
104: register struct valuelist **list;
105: {
106: register struct valuelist *lp = *list;
107: int n;
108:
109: for (; lp != NULL; lp = lp->next)
110: if (strcasecmp(lp->name, s) == 0) {
111: if (lp != *list) {
112: lp->prev->next = lp->next;
113: if (lp->next)
114: lp->next->prev = lp->prev;
115: (*list)->prev = lp;
116: lp->next = *list;
117: *list = lp;
118: }
119: return(lp->port);
120: }
121: if (sscanf(s, "%d", &n) != 1 || n <= 0)
122: n = -1;
123: return(n);
124: }
125:
126: struct servent *
127: cgetservbyport(port, proto)
128: u_short port;
129: char *proto;
130: {
131: register struct valuelist **list = &servicelist;
132: register struct valuelist *lp = *list;
133: static struct servent serv;
134:
135: port = htons(port);
136: for (; lp != NULL; lp = lp->next) {
137: if (port != lp->port)
138: continue;
139: if (strcasecmp(lp->proto, proto) == 0) {
140: if (lp != *list) {
141: lp->prev->next = lp->next;
142: if (lp->next)
143: lp->next->prev = lp->prev;
144: (*list)->prev = lp;
145: lp->next = *list;
146: *list = lp;
147: }
148: serv.s_name = lp->name;
149: serv.s_port = htons((u_short)lp->port);
150: serv.s_proto = lp->proto;
151: return(&serv);
152: }
153: }
154: return(0);
155: }
156:
157: struct protoent *
158: cgetprotobynumber(proto)
159: register int proto;
160: {
161:
162: register struct valuelist **list = &protolist;
163: register struct valuelist *lp = *list;
164: static struct protoent prot;
165:
166: for (; lp != NULL; lp = lp->next)
167: if (lp->port == proto) {
168: if (lp != *list) {
169: lp->prev->next = lp->next;
170: if (lp->next)
171: lp->next->prev = lp->prev;
172: (*list)->prev = lp;
173: lp->next = *list;
174: *list = lp;
175: }
176: prot.p_name = lp->name;
177: prot.p_proto = lp->port;
178: return(&prot);
179: }
180: return(0);
181: }
182:
183: char *
184: protocolname(num)
185: int num;
186: {
187: static char number[8];
188: struct protoent *pp;
189:
190: pp = cgetprotobynumber(num);
191: if(pp == 0) {
192: (void) sprintf(number, "%d", num);
193: return(number);
194: }
195: return(pp->p_name);
196: }
197:
198: char *
199: servicename(port, proto)
200: u_short port;
201: char *proto;
202: {
203: static char number[8];
204: struct servent *ss;
205:
206: ss = cgetservbyport(htons(port), proto);
207: if(ss == 0) {
208: (void) sprintf(number, "%d", port);
209: return(number);
210: }
211: return(ss->s_name);
212: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.