2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
4 * FILE: dll/win32/ws2_32_new/src/getproto.c
5 * PURPOSE: GetProtoByY Functions.
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
9 /* INCLUDES ******************************************************************/
16 /* FUNCTIONS *****************************************************************/
20 GetProtoOpenNetworkDatabase(PCHAR Name
)
22 CHAR ExpandedPath
[MAX_PATH
];
23 CHAR DatabasePath
[MAX_PATH
];
27 DWORD RegSize
= sizeof(DatabasePath
);
29 /* Open the database path key */
30 ErrorCode
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
31 "System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
35 if (ErrorCode
== NO_ERROR
)
37 /* Read the actual path */
38 ErrorCode
= RegQueryValueEx(DatabaseKey
,
46 RegCloseKey(DatabaseKey
);
49 ExpandEnvironmentStrings(DatabasePath
, ExpandedPath
, MAX_PATH
);
54 GetSystemDirectory(ExpandedPath
, MAX_PATH
);
55 strcat(ExpandedPath
, "DRIVERS\\ETC\\");
58 /* Make sure that the path is backslash-terminated */
59 if (ExpandedPath
[strlen(ExpandedPath
) - 1] != '\\')
61 /* It isn't, so add it ourselves */
62 strcat(ExpandedPath
, "\\");
65 /* Add the database name */
66 strcat(ExpandedPath
, Name
);
68 /* Return a handle to the file */
69 return CreateFile(ExpandedPath
,
74 FILE_ATTRIBUTE_NORMAL
,
80 GetProtoPatternMatch(IN PCHAR Buffer
,
85 /* Loop as long as we have data */
86 while ((ScanChar
= *Buffer
))
88 /* Check for a match and return its pointer if found */
89 if (strchr(Lookup
, ScanChar
)) return Buffer
;
101 GetProtoGetNextEnt(IN HANDLE DbHandle
,
102 IN PWSPROTO_BUFFER Buffer
)
106 PCHAR p
, p1
, Entry
, *Aliases
;
107 PPROTOENT ReturnedProtoent
;
109 /* Find out where we currently are in the file */
110 n
= SetFilePointer(DbHandle
, 0, 0, FILE_CURRENT
);
115 if (!ReadFile(DbHandle
,
121 /* Find out where the line ends */
122 p1
= Buffer
->LineBuffer
;
123 p
= strchr(Buffer
->LineBuffer
, '\n');
125 /* Bail out if the file is parsed */
128 /* Calculate our new position */
129 n
+= (LONG
)(p
- p1
) + 1;
131 /* Make it so we read from there next time */
132 SetFilePointer(DbHandle
, n
, 0, FILE_BEGIN
);
134 /* Null-terminate the buffer so it only contains this line */
137 /* If this line is a comment, skip it */
138 if (*p1
== '#') continue;
140 /* Get the entry in this line and null-terminate it */
141 Entry
= GetProtoPatternMatch(p1
, "#\n");
142 if (!Entry
) continue;
145 /* Start with the name */
146 Buffer
->Protoent
.p_name
= p1
;
148 /* Get the first tab and null-terminate */
149 Entry
= GetProtoPatternMatch(p1
, " \t");
150 if (!Entry
) continue;
151 *Entry
++ = ANSI_NULL
;
153 /* Handle remaining tabs or spaces */
154 while (*Entry
== ' ' || *Entry
== '\t') Entry
++;
156 /* Now move our read pointer */
157 p1
= GetProtoPatternMatch(Entry
, " \t");
158 if (p1
) *p1
++ = ANSI_NULL
;
160 /* This is where the address is */
161 Buffer
->Protoent
.p_proto
= (short)atoi(Entry
);
163 /* Setup the alias buffer */
164 Buffer
->Protoent
.p_aliases
= Buffer
->Aliases
;
165 Aliases
= Buffer
->Protoent
.p_aliases
;
167 /* Check if the pointer is stil valid */
170 /* The first entry is here */
173 /* Loop while there are non-null entries */
174 while (Entry
&& *Entry
)
176 /* Handle tabs and spaces */
177 while (*Entry
== ' ' || *Entry
== '\t') Entry
++;
179 /* Make sure we don't go over the buffer */
180 if (Aliases
< &Buffer
->Protoent
.p_aliases
[MAXALIASES
- 1])
182 /* Write the alias */
186 /* Get to the next entry */
187 Entry
= GetProtoPatternMatch(Entry
, " \t");
188 if (Entry
) *Entry
++ = ANSI_NULL
;
192 /* Terminate the list */
195 /* Return to caller */
196 ReturnedProtoent
= &Buffer
->Protoent
;
200 /* Return whatever we got */
201 return ReturnedProtoent
;
209 getprotobynumber(IN INT number
)
215 PVOID GetProtoBuffer
;
217 DPRINT("getprotobynumber: %lx\n", number
);
220 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) != ERROR_SUCCESS
)
223 SetLastError(ErrorCode
);
228 GetProtoBuffer
= WsThreadGetProtoBuffer(Thread
);
232 SetLastError(WSANO_DATA
);
236 /* Open the network database */
237 DbHandle
= GetProtoOpenNetworkDatabase("protocol");
238 if (DbHandle
== INVALID_HANDLE_VALUE
)
240 /* Couldn't open the DB; fail */
241 SetLastError(WSANO_DATA
);
245 /* Start the scan loop */
248 /* Get a protoent entry */
249 Protoent
= GetProtoGetNextEnt(DbHandle
, GetProtoBuffer
);
251 /* Break if we didn't get any new one */
252 if (!Protoent
) break;
254 /* Break if we have a match */
255 if (Protoent
->p_proto
== number
) break;
258 /* Close the network database */
259 CloseHandle(DbHandle
);
261 /* Set error if we don't have a protoent */
262 if (!Protoent
) SetLastError(WSANO_DATA
);
273 getprotobyname(IN CONST CHAR FAR
*name
)
279 PVOID GetProtoBuffer
;
281 DPRINT("getprotobyname: %s\n", name
);
284 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) != ERROR_SUCCESS
)
287 SetLastError(ErrorCode
);
292 GetProtoBuffer
= WsThreadGetProtoBuffer(Thread
);
296 SetLastError(WSANO_DATA
);
300 /* Open the network database */
301 DbHandle
= GetProtoOpenNetworkDatabase("protocol");
302 if (DbHandle
== INVALID_HANDLE_VALUE
)
304 /* Couldn't open the DB; fail */
305 SetLastError(WSANO_DATA
);
309 /* Start the scan loop */
312 /* Get a protoent entry */
313 Protoent
= GetProtoGetNextEnt(DbHandle
, GetProtoBuffer
);
315 /* Break if we didn't get any new one */
316 if (!Protoent
) break;
318 /* Break if we have a match */
319 if (!_stricmp(Protoent
->p_name
, name
)) break;
322 /* Close the network database */
323 CloseHandle(DbHandle
);
325 /* Set error if we don't have a protoent */
326 if (!Protoent
) SetLastError(WSANO_DATA
);