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 /* Null terminate LineBuffer */
122 Buffer
->LineBuffer
[Read
] = ANSI_NULL
;
124 /* Find out where the line ends */
125 p1
= Buffer
->LineBuffer
;
126 p
= strchr(Buffer
->LineBuffer
, '\n');
128 /* Bail out if the file is parsed */
131 /* Calculate our new position */
132 n
+= (LONG
)(p
- p1
) + 1;
134 /* Make it so we read from there next time */
135 SetFilePointer(DbHandle
, n
, 0, FILE_BEGIN
);
137 /* Null-terminate the buffer so it only contains this line */
140 /* If this line is a comment, skip it */
141 if (*p1
== '#') continue;
143 /* Get the entry in this line and null-terminate it */
144 Entry
= GetProtoPatternMatch(p1
, "#\n");
145 if (!Entry
) continue;
148 /* Start with the name */
149 Buffer
->Protoent
.p_name
= p1
;
151 /* Get the first tab and null-terminate */
152 Entry
= GetProtoPatternMatch(p1
, " \t");
153 if (!Entry
) continue;
154 *Entry
++ = ANSI_NULL
;
156 /* Handle remaining tabs or spaces */
157 while (*Entry
== ' ' || *Entry
== '\t') Entry
++;
159 /* Now move our read pointer */
160 p1
= GetProtoPatternMatch(Entry
, " \t");
161 if (p1
) *p1
++ = ANSI_NULL
;
163 /* This is where the address is */
164 Buffer
->Protoent
.p_proto
= (short)atoi(Entry
);
166 /* Setup the alias buffer */
167 Buffer
->Protoent
.p_aliases
= Buffer
->Aliases
;
168 Aliases
= Buffer
->Protoent
.p_aliases
;
170 /* Check if the pointer is stil valid */
173 /* The first entry is here */
176 /* Loop while there are non-null entries */
177 while (Entry
&& *Entry
)
179 /* Handle tabs and spaces */
180 while (*Entry
== ' ' || *Entry
== '\t') Entry
++;
182 /* Make sure we don't go over the buffer */
183 if (Aliases
< &Buffer
->Protoent
.p_aliases
[MAXALIASES
- 1])
185 /* Write the alias */
189 /* Get to the next entry */
190 Entry
= GetProtoPatternMatch(Entry
, " \t");
191 if (Entry
) *Entry
++ = ANSI_NULL
;
195 /* Terminate the list */
198 /* Return to caller */
199 ReturnedProtoent
= &Buffer
->Protoent
;
203 /* Return whatever we got */
204 return ReturnedProtoent
;
212 getprotobynumber(IN INT number
)
218 PVOID GetProtoBuffer
;
220 DPRINT("getprotobynumber: %lx\n", number
);
223 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) != ERROR_SUCCESS
)
226 SetLastError(ErrorCode
);
231 GetProtoBuffer
= WsThreadGetProtoBuffer(Thread
);
235 SetLastError(WSANO_DATA
);
239 /* Open the network database */
240 DbHandle
= GetProtoOpenNetworkDatabase("protocol");
241 if (DbHandle
== INVALID_HANDLE_VALUE
)
243 /* Couldn't open the DB; fail */
244 SetLastError(WSANO_DATA
);
248 /* Start the scan loop */
251 /* Get a protoent entry */
252 Protoent
= GetProtoGetNextEnt(DbHandle
, GetProtoBuffer
);
254 /* Break if we didn't get any new one */
255 if (!Protoent
) break;
257 /* Break if we have a match */
258 if (Protoent
->p_proto
== number
) break;
261 /* Close the network database */
262 CloseHandle(DbHandle
);
264 /* Set error if we don't have a protoent */
265 if (!Protoent
) SetLastError(WSANO_DATA
);
276 getprotobyname(IN CONST CHAR FAR
*name
)
282 PVOID GetProtoBuffer
;
284 DPRINT("getprotobyname: %s\n", name
);
287 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) != ERROR_SUCCESS
)
290 SetLastError(ErrorCode
);
295 GetProtoBuffer
= WsThreadGetProtoBuffer(Thread
);
299 SetLastError(WSANO_DATA
);
303 /* Open the network database */
304 DbHandle
= GetProtoOpenNetworkDatabase("protocol");
305 if (DbHandle
== INVALID_HANDLE_VALUE
)
307 /* Couldn't open the DB; fail */
308 SetLastError(WSANO_DATA
);
312 /* Start the scan loop */
315 /* Get a protoent entry */
316 Protoent
= GetProtoGetNextEnt(DbHandle
, GetProtoBuffer
);
318 /* Break if we didn't get any new one */
319 if (!Protoent
) break;
321 /* Break if we have a match */
322 if (!_stricmp(Protoent
->p_name
, name
)) break;
325 /* Close the network database */
326 CloseHandle(DbHandle
);
328 /* Set error if we don't have a protoent */
329 if (!Protoent
) SetLastError(WSANO_DATA
);