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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
/* netdb.cc: network database related routines.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <shared_info.h>
/* Locate and open a system network database file. relative_path
should be one of the following values:
"protocol"
"services"
"networks"
"hosts"
It is the callers responsibility to close the file. */
static FILE *
open_system_file (const char *relative_path)
{
/* system dir path is never longer. */
char win32_name[MAX_PATH];
user_shared->warned_msdos = true;
sys_wcstombs (win32_name, MAX_PATH, windows_system_directory);
strcat (win32_name, "drivers\\etc\\");
strcat (win32_name, relative_path);
FILE *result = fopen (win32_name, "rt");
debug_printf ("handle to netdb file %s: %p", win32_name, result);
return result;
}
inline static FILE *
open_protocol_file ()
{
return open_system_file ("protocol");
}
/* Wrapper for open_system_file(), fixing the constant name
"services". Returns the open file. */
inline static FILE *
open_services_file ()
{
return open_system_file ("services");
}
/* Read an entire line up till the next \n character. Memory for the
line is dynamically allocated, and the caller must call free() to
deallocate it. When the end of file is reached, NULL is returned. */
static char *
get_entire_line (FILE *fd)
{
static const int BUFF_SIZE = 1024;
struct line_fragment
{
char buffer[BUFF_SIZE];
line_fragment *next;
};
line_fragment *fragment_list_head = NULL;
line_fragment *fragment = NULL;
int fragment_count = 0;
char *result;
do
{
line_fragment *new_fragment = (line_fragment *) malloc (sizeof (line_fragment));
paranoid_printf ("line fragment allocated %p", new_fragment);
if (!fragment_list_head)
fragment_list_head = new_fragment;
if (fragment)
fragment->next = new_fragment;
fragment = new_fragment;
fragment->next = NULL;
*fragment->buffer = '\0';
result = fgets (fragment->buffer, BUFF_SIZE, fd);
++fragment_count;
}
while (result && !strchr (fragment->buffer, '\n'));
if (*fragment_list_head->buffer != '\0')
{
char *concatenated_line = (char *) calloc (fragment_count * BUFF_SIZE , sizeof (char));
paranoid_printf ("concatenated line allocated %p", concatenated_line);
*concatenated_line = '\0';
fragment = fragment_list_head;
while (fragment != NULL)
{
line_fragment *previous = fragment;
strcat (concatenated_line, fragment->buffer);
fragment = fragment->next;
free (previous);
}
return concatenated_line;
}
else
{
fragment = fragment_list_head;
while (fragment != NULL)
{
line_fragment *previous = fragment;
fragment = fragment->next;
free (previous);
}
return NULL;
}
}
/* Characters representing whitespace. Used by parse_* routines to
delimit tokens. */
static const char *SPACE = " \t\n\r\f";
/* Parse a list aliases from a network database file. Returns a
char** structure terminated by a NULL. */
static void
parse_alias_list (char ***aliases, char **lasts)
{
struct alias_t
{
char *alias_name;
alias_t *next;
};
alias_t *alias_list_head = NULL, *alias_list_tail = NULL;
char *alias;
int alias_count = 0;
alias = strtok_r (NULL, SPACE, lasts);
while (alias)
{
++alias_count;
alias_t *new_alias = (alias_t *) malloc (sizeof (alias_t));
paranoid_printf ("new alias alloc %p", new_alias);
if (!alias_list_head)
alias_list_head = new_alias;
if (alias_list_tail)
alias_list_tail->next = new_alias;
new_alias->next = NULL;
new_alias->alias_name = alias;
alias_list_tail = new_alias;
alias = strtok_r (NULL, SPACE, lasts);
}
*aliases = (char**) calloc (alias_count + 1, sizeof (char *));
paranoid_printf ("aliases alloc %p", *aliases);
char **current_entry = *aliases;
while (alias_list_head)
{
alias_t *previous = alias_list_head;
*current_entry = strdup (alias_list_head->alias_name);
paranoid_printf ("*current entry strdup %p", *current_entry);
alias_list_head = alias_list_head->next;
free (previous);
++current_entry;
}
*current_entry = NULL;
}
/* Read the next line from svc_file, and parse it into the structure
pointed to by sep. sep can point to stack or static data, but it's
members will be overwritten with pointers to dynamically allocated
heap data accommodating parsed data. It is the responsibility of the
caller to free up the allocated structures. The function returns true
to indicate that a line was successfully read and parsed. False is
used to indicate that no more lines can be read and parsed. This
should also interpreted as end of file. */
static bool
parse_services_line (FILE *svc_file, struct servent *sep)
{
char *line;
while ((line = get_entire_line (svc_file)))
{
char *name, *port, *protocol, *lasts;
line[strcspn (line, "#")] = '\0'; // truncate at comment marker.
name = strtok_r (line, SPACE, &lasts);
if (!name)
{
free (line);
continue;
}
port = strtok_r (NULL, SPACE, &lasts);
protocol = strchr (port, '/');
*protocol++ = '\0';
sep->s_name = strdup (name);
paranoid_printf ("sep->s_name strdup %p", sep->s_name);
sep->s_port = htons (atoi (port));
sep->s_proto = strdup (protocol);
paranoid_printf ("sep->s_proto strdup %p", sep->s_proto);
/* parse_alias_list relies on side effects. Read the comments
for that function.*/
parse_alias_list (& sep->s_aliases, &lasts);
free (line);
return true;
}
return false;
}
static FILE *svc_file = NULL;
static long int svc_read_pos = 0;
static struct servent current_servent;
/* Steps through a struct servent, and frees all of the internal
structures.*/
static void
free_servent (struct servent *sep)
{
free (sep->s_name);
free (sep->s_proto);
char ** current = sep->s_aliases;
while (current && *current)
{
free (*current);
++current;
}
free (sep->s_aliases);
sep->s_name = NULL;
sep->s_port = 0;
sep->s_proto = NULL;
sep->s_aliases = NULL;
}
extern "C" void
cygwin_setservent (int stay_open)
{
if (svc_file)
fclose (svc_file);
if (stay_open)
svc_file = open_services_file ();
free_servent (¤t_servent);
svc_read_pos = 0;
syscall_printf ("setservent (%d)", stay_open);
}
extern "C" struct servent *
cygwin_getservent (void)
{
FILE *fd;
if (svc_file)
fd = svc_file;
else
{
fd = open_services_file ();
if (!fd)
{
syscall_printf ("%p = getservent()", NULL);
return NULL;
}
fseek (fd, svc_read_pos, SEEK_SET);
}
free_servent (¤t_servent);
bool found = parse_services_line (fd, ¤t_servent);
if (!svc_file)
{
svc_read_pos = ftell (fd);
fclose (fd);
}
struct servent *result;
if (found)
result = ¤t_servent;
else
result = NULL;
syscall_printf ("%p = getservent()", result);
return result;
}
extern "C" void
cygwin_endservent (void)
{
if (svc_file)
{
fclose (svc_file);
svc_file = NULL;
}
free_servent (¤t_servent);
svc_read_pos = 0;
syscall_printf ("endservent ()");
}
/* Read the next line from proto_file, and parse it into the structure
pointed to by pep. pep can point to stack or static data, but it's
members will be overwritten with pointers to dynamically allocated
heap data accommodating parsed data. It is the responsibility of the
caller to free up the allocated structures. The function returns true
to indicate that a line was successfully read and parsed. False is
used to indicate that no more lines can be read and parsed. This
should also interpreted as end of file. */
static bool
parse_protocol_line (FILE *proto_file, struct protoent *pep)
{
char *line;
while ((line = get_entire_line (proto_file)))
{
char *name, *protocol, *lasts;
line[strcspn (line, "#")] = '\0'; // truncate at comment marker.
name = strtok_r (line, SPACE, &lasts);
if (!name)
{
free (line);
continue;
}
protocol = strtok_r (NULL, SPACE, &lasts);
pep->p_name = strdup (name);
paranoid_printf ("pep->p_name strdup %p", pep->p_name);
pep->p_proto = atoi (protocol);
/* parse_alias_list relies on side effects. Read the comments
for that function.*/
parse_alias_list (& pep->p_aliases, &lasts);
free (line);
return true;
}
return false;
}
static FILE *proto_file = NULL;
static long int proto_read_pos = 0;
static struct protoent current_protoent;
/* Steps through a struct protoent, and frees all the internal
structures. */
static void
free_protoent (struct protoent *pep)
{
free (pep->p_name);
char ** current = pep->p_aliases;
while (current && *current)
{
free (*current);
++current;
}
free (pep->p_aliases);
pep->p_name = NULL;
pep->p_proto = 0;
pep->p_aliases = NULL;
}
extern "C" void
cygwin_setprotoent (int stay_open)
{
if (proto_file)
fclose (proto_file);
if (stay_open)
proto_file = open_protocol_file ();
free_protoent (¤t_protoent);
proto_read_pos = 0;
syscall_printf ("setprotoent (%d)", stay_open);
}
extern "C" struct protoent *
cygwin_getprotoent (void)
{
FILE *fd;
if (proto_file)
fd = proto_file;
else
{
fd = open_protocol_file ();
if (!fd)
{
syscall_printf ("%p = getprotoent()", NULL);
return NULL;
}
fseek (fd, proto_read_pos, SEEK_SET);
}
free_protoent (¤t_protoent);
bool found = parse_protocol_line (fd, ¤t_protoent);
if (!proto_file)
{
proto_read_pos = ftell (fd);
fclose (fd);
}
struct protoent *result;
if (found)
result = ¤t_protoent;
else
result = NULL;
syscall_printf ("%p = getprotoent()", result);
return result;
}
extern "C" void
cygwin_endprotoent (void)
{
if (proto_file)
{
fclose (proto_file);
proto_file = NULL;
}
free_protoent (¤t_protoent);
proto_read_pos = 0;
syscall_printf ("endprotoent ()");
}
|