46 #include "EST_Pathname.h"
47 #include "EST_socket.h"
48 #include "EST_error.h"
49 #include "EST_Token.h"
50 #include "EST_ServiceTable.h"
52 #if defined(SYSTEM_IS_WIN32)
53 # define seed_random() srand((unsigned)time(NULL))
54 # define get_random() rand()
56 # define seed_random() srandom(time(NULL));
57 # define get_random() random()
60 #if defined(__sun__) && defined(__sparc__) && defined(__svr4__)
64 #if defined(older_solaris)
65 int srandom(
unsigned seed);
66 int gethostname(
char *name,
int namelen);
72 EST_ServiceTable::EntryTable EST_ServiceTable::entries;
73 bool EST_ServiceTable::random_init=FALSE;
75 void EST_ServiceTable::init_random(
void)
115 if ((sfile = fopen(socketsFileName,
"r"))==NULL)
118 if (str.
open(sfile, 1))
119 EST_sys_error(
"Can't access fringe file '%s'",
120 (
const char *)socketsFileName);
145 if (!entries.t.present(name))
149 entries.t.add_item(name, newent);
152 Entry &entry = entries.t.val(name);
156 else if (type==
"address")
158 else if (type==
"type")
160 else if (type==
"port")
162 else if (type==
"cookie")
165 EST_warning(
"Unknown entry field '%s' at %s",
177 if ((f=fopen(filename,
"w"))==NULL)
178 EST_sys_error(
"can't write serice file");
180 fprintf(f,
"#Services\n");
184 for(p.
begin(entries.t); p ; ++p)
187 const Entry &entry = p->v;
189 fprintf(f,
"%s.type=%s\n", (
const char *)name, (
const char *)entry.
type);
190 fprintf(f,
"%s.cookie=%s\n", (
const char *)name, (
const char *)entry.
cookie);
191 fprintf(f,
"%s.host=%s\n", (
const char *)name, (
const char *)entry.
hostname);
192 fprintf(f,
"%s.address=%s\n", (
const char *)name, (
const char *)entry.
address);
193 fprintf(f,
"%s.port=%d\n", (
const char *)name, entry.
port);
202 EST_Pathname socketsFileName(getenv(
"HOME")?getenv(
"HOME"):
"/");
204 socketsFileName +=
".estServices";
206 read_table(socketsFileName);
211 EST_Pathname socketsFileName(getenv(
"HOME")?getenv(
"HOME"):
"/");
213 socketsFileName +=
".estServices";
215 write_table(socketsFileName);
234 s <<
"Entries of type " << type <<
":\n";
243 sprintf(buff,
"%10s",
"Name");
245 sprintf(buff,
"%30s",
" Hostname");
247 sprintf(buff,
"%20s",
" IP Address");
249 sprintf(buff,
"%5s\n",
"Port");
252 for(them.
begin(entries.t); them; them++)
254 const Entry &entry = them->v;
256 if (type !=
"" && entry.
type != type)
260 sprintf(buff,
"%10s",(
const char *)entry.
name);
262 sprintf(buff,
"%30s",(
const char *)entry.
hostname);
264 sprintf(buff,
"%20s",(
const char *)entry.
address);
266 s << entry.
port <<
"\n";
276 for(them.
begin(entries.t); them; them++)
277 if (type==
"" || type == them->v.name)
284 if (entries.t.present(name))
286 Entry &entry = entries.t.val(name);
288 if (entry.
type == type)
292 return *(entries.t.default_val);
301 long cookie = get_random();
302 struct sockaddr_in sin;
304 socklen_t size=
sizeof(
struct sockaddr_in);
308 if (getsockname(socket, (
struct sockaddr *)&sin, &size) != 0)
309 EST_sys_error(
"Can't find my address");
315 gethostname(myname, 100);
317 struct hostent *hent = gethostbyname(myname);
320 EST_sys_error(
"Can't look up my address");
323 if (hent->h_addr_list != NULL)
324 memcpy(&(sin.sin_addr.s_addr),hent->h_addr_list[0], sizeof (sin.sin_addr.s_addr));
331 entry.
port = ntohs(sin.sin_port);
335 entries.t.add_item(name, entry);
337 return entries.t.val(name);
344 #if defined(INSTANTIATE_TEMPLATES)
346 #include "../base_class/EST_TList.cc"
347 #include "../base_class/EST_TSortable.cc"
348 #include "../base_class/EST_TKVL.cc"