This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
names.c
276 lines (232 loc) · 7.29 KB
/
names.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
* 2002, 2003, 2004
* Ohio University.
*
* ---
*
* Starting with the release of tcptrace version 6 in 2001, tcptrace
* is licensed under the GNU General Public License (GPL). We believe
* that, among the available licenses, the GPL will do the best job of
* allowing tcptrace to continue to be a valuable, freely-available
* and well-maintained tool for the networking community.
*
* Previous versions of tcptrace were released under a license that
* was much less restrictive with respect to how tcptrace could be
* used in commercial products. Because of this, I am willing to
* consider alternate license arrangements as allowed in Section 10 of
* the GNU GPL. Before I would consider licensing tcptrace under an
* alternate agreement with a particular individual or company,
* however, I would have to be convinced that such an alternative
* would be to the greater benefit of the networking community.
*
* ---
*
* This file is part of Tcptrace.
*
* Tcptrace was originally written and continues to be maintained by
* Shawn Ostermann with the help of a group of devoted students and
* users (see the file 'THANKS'). The work on tcptrace has been made
* possible over the years through the generous support of NASA GRC,
* the National Science Foundation, and Sun Microsystems.
*
* Tcptrace is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Tcptrace is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tcptrace (in the file 'COPYING'); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Author: Shawn Ostermann
* School of Electrical Engineering and Computer Science
* Ohio University
* Athens, OH
* ostermann@cs.ohiou.edu
* http://www.tcptrace.org/
*/
#include "tcptrace.h"
static char const GCC_UNUSED copyright[] =
"@(#)Copyright (c) 2004 -- Ohio University.\n";
static char const GCC_UNUSED rcsid[] =
"@(#)$Header$";
/*
* name.c -- name binding stuff
*
* Author: Shawn Ostermann
* Date: Tue Nov 1, 1994
*/
#include "gcache.h"
/* local routines */
/* N.B. - result comes from static memory, save it before calling back! */
char *
ServiceName(
portnum port)
{
static int cache = -1;
tcelen len;
struct servent *pse;
#define NUM_PORT_BUFS 4
#define SIZEOF_PORT_BUF 20
static char port_bufs[NUM_PORT_BUFS][SIZEOF_PORT_BUF];
static int bufix = 0;
char *port_buf;
char *sb_port;
/* rotate through multiple static buffers, so multiple calls per printf work */
port_buf = port_bufs[bufix = ((bufix+1)%NUM_PORT_BUFS)];
if (!resolve_ports) {
snprintf(port_buf,SIZEOF_PORT_BUF,"%hu",port);
return(port_buf);
}
/* only small numbers have names */
if (port > 1023) {
snprintf(port_buf,SIZEOF_PORT_BUF,"%hu",port);
return(port_buf);
}
/* check the cache */
if (cache == -1) {
cache = cacreate("service",250,0);
}
len = SIZEOF_PORT_BUF;
if (debug > 2)
fprintf(stderr,"Searching cache for service %d='%s'\n",
port, port_buf);
if (calookup(cache,
(char *) &port, (tcelen) sizeof(port),
(char *) port_buf, &len) == OK) {
if (debug > 2)
fprintf(stderr,"Found service %d='%s' in cache\n",
port, port_buf);
return(port_buf);
}
/* get port name as a string */
pse = getservbyport(port,"tcp");
if (pse != NULL) {
sb_port = pse->s_name;
} else {
snprintf(port_buf,SIZEOF_PORT_BUF,"%d",port);
sb_port = port_buf;
}
if (debug > 2)
fprintf(stderr,"Putting service %d='%s' in cache\n",
port, sb_port);
cainsert(cache,
(char *) &port, (tcelen) sizeof(port),
(char *) sb_port, (tcelen) (strlen(sb_port)+1));
return(sb_port);
}
/* turn an ipaddr into a printable format */
/* N.B. - result comes from static memory, save it before calling back! */
char *
HostAddr(
ipaddr ipaddress)
{
#define NUM_NAME_BUFS 4
#define SIZEOF_NAME_BUF 100
static char name_bufs[NUM_NAME_BUFS][SIZEOF_NAME_BUF];
static int bufix = 0;
char *name_buf;
char *adr;
if (ADDR_ISV6(&ipaddress)) {
static char adrv6[INET6_ADDRSTRLEN];
my_inet_ntop(AF_INET6,(char *) ipaddress.un.ip6.s6_addr,
adrv6, INET6_ADDRSTRLEN);
adr = adrv6;
} else {
adr = inet_ntoa(ipaddress.un.ip4);
}
/* rotate through multiple static buffers, so multiple calls per printf work */
name_buf = name_bufs[bufix = ((bufix+1)%NUM_NAME_BUFS)];
strncpy(name_buf,adr,SIZEOF_NAME_BUF);
return(name_buf);
}
/* turn an ipaddr into a printable name */
/* N.B. - result comes from static memory, save it before calling back! */
char *
HostName(
ipaddr ipaddress)
{
tcelen len;
static int cache = -1;
struct hostent *phe;
char *sb_host;
#define NUM_NAME_BUFS 4
#define SIZEOF_NAME_BUF 100
static char name_bufs[NUM_NAME_BUFS][SIZEOF_NAME_BUF];
static int bufix = 0;
char *name_buf;
char *adr;
/* rotate through multiple static buffers, so multiple calls per printf work */
name_buf = name_bufs[bufix = ((bufix+1)%NUM_NAME_BUFS)];
adr = HostAddr(ipaddress);
if (!resolve_ipaddresses) {
return(adr);
}
/* check the cache */
if (cache == -1) {
cache = cacreate("host",250,0);
}
len = SIZEOF_NAME_BUF;
if (debug > 2)
fprintf(stderr,"Searching cache for host '%s'\n",
adr);
if (calookup(cache,
(char *) &ipaddress, (tcelen) sizeof(ipaddress),
(char *) name_buf, &len) == OK) {
if (debug > 2)
fprintf(stderr,"Found host %s='%s' in cache\n",
adr, name_buf);
return(name_buf);
}
if (ADDR_ISV6(&ipaddress))
phe = gethostbyaddr ((char *)&ipaddress.un.ip6,
sizeof(ipaddress.un.ip6), AF_INET6);
else
phe = gethostbyaddr((char *)&ipaddress.un.ip4,
sizeof(ipaddress.un.ip4), AF_INET);
if (phe != NULL) {
sb_host = phe->h_name;
} else {
sb_host = adr;
}
if (use_short_names) {
char *pdot;
if ((pdot = strchr(sb_host,'.')) != NULL) {
*pdot = '\00'; /* chop off the end */
}
}
if (debug > 2)
fprintf(stderr,"Putting host %s='%s' in cache\n",
adr, sb_host);
cainsert(cache,
(char *) &ipaddress, (tcelen)sizeof(ipaddress),
(char *) sb_host, (tcelen)(strlen(sb_host)+1));
return(sb_host);
}
/* N.B. returns memory that is overwritten by subsequent calls */
char *
EndpointName(
ipaddr ipaddress,
portnum port)
{
#define NUM_NAME_BUFS 4
#define SIZEOF_NAME_BUF 100
static char name_bufs[NUM_NAME_BUFS][SIZEOF_NAME_BUF];
static int bufix = 0;
char *name_buf;
char *sb_host;
char *sb_port;
/* rotate through multiple static buffers, so multiple calls per printf work */
name_buf = name_bufs[bufix = ((bufix+1)%NUM_NAME_BUFS)];
sb_host = HostName(ipaddress);
sb_port = ServiceName(port);
snprintf(name_buf,SIZEOF_NAME_BUF,"%s:%s", sb_host, sb_port);
return(name_buf);
}