Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libcs/csname.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1990-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
/*
22
* Glenn Fowler
23
* AT&T Research
24
*
25
* return host name given address
26
* if addr==0 then permanent pointer to local host name returned
27
* otherwise temporary pointer returned
28
* `.' qualification deleted if possible
29
*/
30
31
#if defined(__STDPP__directive) && defined(__STDPP__hide)
32
__STDPP__directive pragma pp:hide gethostname
33
#else
34
#define gethostname ______gethostname
35
#endif
36
37
#include "cslib.h"
38
39
#if _lib_uname && _sys_utsname
40
#include <sys/utsname.h>
41
#endif
42
43
#if defined(__STDPP__directive) && defined(__STDPP__hide)
44
__STDPP__directive pragma pp:nohide gethostname
45
#else
46
#undef gethostname
47
#endif
48
49
#if _lib_gethostname
50
extern int gethostname(char*, size_t);
51
#endif
52
53
#if !CS_LIB_LOCAL && CS_LIB_SOCKET
54
55
/*
56
* keep host db connection alive
57
* the lib should make hidden fd's close-on-exec
58
*/
59
60
void
61
cssetdb(register Cs_t* state)
62
{
63
register int fd;
64
register int i;
65
66
if (state->db <= 0 && ++state->db)
67
{
68
if ((fd = dup(0)) >= 0)
69
close(fd);
70
sethostent(1);
71
gethostbyname("localhost");
72
if (fd >= 0)
73
for (i = fd; i < fd + 3 && fcntl(i, F_SETFD, FD_CLOEXEC) < 0; i++);
74
}
75
}
76
77
#endif
78
79
/*
80
* return host name for addr
81
*/
82
83
char*
84
csname(register Cs_t* state, unsigned long addr)
85
{
86
register char* s;
87
88
messagef((state->id, NiL, -8, "name(%s) call", csntoa(state, addr)));
89
#if CS_LIB_LOCAL
90
NoP(addr);
91
#else
92
if (addr)
93
{
94
if (addr != CS_LOCAL)
95
{
96
#if CS_LIB_SOCKET
97
struct hostent* hp;
98
struct in_addr ha;
99
#endif
100
101
csdb(state);
102
#if CS_LIB_SOCKET || CS_LIB_V10
103
#if CS_LIB_SOCKET
104
ha.s_addr = addr;
105
if ((hp = gethostbyaddr((char*)&ha, sizeof(ha), AF_INET)) && (s = hp->h_name))
106
#else
107
if (s = in_host(addr))
108
#endif
109
{
110
if (!((state->flags | state->disc->flags) & CS_ADDR_FULL))
111
{
112
register char* t;
113
114
if ((t = strrchr(s, '.')) && !*(t + 1)) *t = 0;
115
if (t = strchr(s, '.'))
116
{
117
strncpy(state->temp, s, sizeof(state->temp) - 1);
118
*(t = state->temp + (t - s)) = 0;
119
s = state->temp;
120
if (csaddr(state, s) != addr) *t = '.';
121
}
122
}
123
return s;
124
}
125
#endif
126
messagef((state->id, NiL, -1, "name: %s: gethostbyaddr error", csntoa(state, addr)));
127
s = csntoa(state, addr);
128
return s;
129
}
130
}
131
#endif
132
if (!state->name[0])
133
{
134
135
#if _lib_gethostname
136
137
if (gethostname(state->full, sizeof(state->full) - 1))
138
139
#else
140
141
#if _lib_uname && _sys_utsname
142
143
struct utsname un;
144
145
/*
146
* NOTE: uname(2) may return >0 on success -- go ask your dad
147
*/
148
149
if (uname(&un) >= 0) strncpy(state->full, un.nodename, sizeof(state->full) - 1);
150
else
151
152
#else
153
154
int fd;
155
int n;
156
157
if ((fd = open("/etc/whoami", O_RDONLY)) >= 0)
158
{
159
if ((n = read(fd, state->full, sizeof(state->full))) > 0) state->full[n - 1] = 0;
160
close(fd);
161
}
162
else
163
164
#endif
165
166
#endif
167
168
{
169
messagef((state->id, NiL, -1, "name: %s: gethostname error", csntoa(state, addr)));
170
strcpy(state->full, CS_HOST_LOCAL);
171
}
172
state->full[sizeof(state->full) - 1] = 0;
173
strncpy(state->name, state->full, sizeof(state->name) - 1);
174
if (s = strchr(state->name, '.')) *s = 0;
175
}
176
return ((state->flags | state->disc->flags) & CS_ADDR_FULL) ? state->full : state->name;
177
}
178
179
char*
180
_cs_name(unsigned long addr)
181
{
182
return csname(&cs, addr);
183
}
184
185