ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

getproto.c
Go to the documentation of this file.
00001 /*
00002  * COPYRIGHT:   See COPYING in the top level directory
00003  * PROJECT:     ReactOS WinSock 2 API
00004  * FILE:        getproto.c
00005  * PURPOSE:     GetProtoByY Functions.
00006  * PROGRAMMER:  Alex Ionescu (alex@relsoft.net)
00007  */
00008 
00009 /* INCLUDES ******************************************************************/
00010 #include "ws2_32.h"
00011 
00012 //#define NDEBUG
00013 #include <debug.h>
00014 
00015 /* DATA **********************************************************************/
00016 
00017 /* FUNCTIONS *****************************************************************/
00018 
00019 HANDLE
00020 WSAAPI
00021 GetProtoOpenNetworkDatabase(PCHAR Name)
00022 {
00023     CHAR ExpandedPath[MAX_PATH];
00024     CHAR DatabasePath[MAX_PATH];
00025     INT ErrorCode;
00026     HKEY DatabaseKey;
00027     DWORD RegType;
00028     DWORD RegSize = sizeof(DatabasePath);
00029 
00030     /* Open the database path key */
00031     ErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
00032                              "System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
00033                              0,
00034                              KEY_READ,
00035                              &DatabaseKey);
00036     if (ErrorCode == NO_ERROR)
00037     {
00038         /* Read the actual path */
00039         ErrorCode = RegQueryValueEx(DatabaseKey,
00040                                     "DatabasePath",
00041                                     NULL,
00042                                     &RegType,
00043                                     (LPBYTE)DatabasePath,
00044                                     &RegSize);
00045 
00046         /* Close the key */
00047         RegCloseKey(DatabaseKey);
00048 
00049         /* Expand the name */
00050         ExpandEnvironmentStrings(DatabasePath, ExpandedPath, MAX_PATH);
00051     }
00052     else
00053     {
00054         /* Use defalt path */
00055         GetSystemDirectory(ExpandedPath, MAX_PATH);
00056         strcat(ExpandedPath, "DRIVERS\\ETC\\");
00057     }
00058 
00059     /* Make sure that the path is backslash-terminated */
00060     if (ExpandedPath[strlen(ExpandedPath) - 1] != '\\')
00061     {
00062         /* It isn't, so add it ourselves */
00063         strcat(ExpandedPath, "\\");
00064     }
00065 
00066     /* Add the database name */
00067     strcat(ExpandedPath, Name);
00068 
00069     /* Return a handle to the file */
00070     return CreateFile(ExpandedPath,
00071                       FILE_READ_ACCESS,
00072                       0,
00073                       NULL,
00074                       OPEN_EXISTING,
00075                       FILE_ATTRIBUTE_NORMAL,
00076                       NULL); 
00077 }
00078 
00079 PCHAR
00080 WSAAPI
00081 GetProtoPatternMatch(IN PCHAR Buffer,
00082                      IN PCHAR Lookup)
00083 {
00084     CHAR ScanChar;
00085 
00086     /* Loop as long as we have data */
00087     while ((ScanChar = *Buffer))
00088     {
00089         /* Check for a match and return its pointer if found */
00090         if (strchr(Lookup, ScanChar)) return Buffer;
00091 
00092         /* Keep going */
00093         Buffer++;
00094     }
00095 
00096     /* Nothing found */
00097     return NULL;
00098 }
00099 
00100 PPROTOENT
00101 WSAAPI
00102 GetProtoGetNextEnt(IN HANDLE DbHandle,
00103                    IN PWSPROTO_BUFFER Buffer)
00104 {
00105     DWORD Read;
00106     LONG n;
00107     PCHAR p, p1, Entry, *Aliases;
00108     PPROTOENT ReturnedProtoent;
00109 
00110     /* Find out where we currently are in the file */
00111     n = SetFilePointer(DbHandle, 0, 0, FILE_CURRENT);
00112 
00113     while (TRUE)
00114     {
00115         /* Read 512 bytes */
00116         if (!ReadFile(DbHandle,
00117                       Buffer->LineBuffer,
00118                       512,
00119                       &Read,
00120                       NULL)) return NULL;
00121 
00122         /* Find out where the line ends */
00123         p1 = Buffer->LineBuffer;
00124         p = strchr(Buffer->LineBuffer, '\n');
00125 
00126         /* Bail out if the file is parsed */
00127         if (!p) return NULL;
00128 
00129         /* Calculate our new position */
00130         n += (LONG)(p - p1) + 1;
00131 
00132         /* Make it so we read from there next time */
00133         SetFilePointer(DbHandle, n, 0, FILE_BEGIN);
00134 
00135         /* Null-terminate the buffer so it only contains this line */
00136         *p = ANSI_NULL;
00137 
00138         /* If this line is a comment, skip it */
00139         if (*p1 == '#') continue;
00140 
00141         /* Get the entry in this line and null-terminate it */
00142         Entry = GetProtoPatternMatch(p1, "#\n");
00143         if (!Entry) continue;
00144         *Entry = ANSI_NULL;
00145 
00146         /* Start with the name */
00147         Buffer->Protoent.p_name = p1;
00148 
00149         /* Get the first tab and null-terminate */
00150         Entry = GetProtoPatternMatch(p1, " \t");
00151         if (!Entry) continue;
00152         *Entry++ = ANSI_NULL;
00153 
00154         /* Handle remaining tabs or spaces */
00155         while (*Entry == ' ' || *Entry == '\t') Entry++;
00156 
00157         /* Now move our read pointer */
00158         p1 = GetProtoPatternMatch(Entry, " \t");
00159         if (p1) *p1++ = ANSI_NULL;
00160 
00161         /* This is where the address is */
00162         Buffer->Protoent.p_proto = (short)atoi(Entry);
00163 
00164         /* Setup the alias buffer */
00165         Buffer->Protoent.p_aliases = Buffer->Aliases;
00166         Aliases = Buffer->Protoent.p_aliases;
00167 
00168         /* Check if the pointer is stil valid */
00169         if (p1)
00170         {
00171             /* The first entry is here */
00172             Entry = p1;
00173 
00174             /* Loop while there are non-null entries */
00175             while (Entry && *Entry)
00176             {
00177                 /* Handle tabs and spaces */
00178                 while (*Entry == ' ' || *Entry == '\t') Entry++;
00179 
00180                 /* Make sure we don't go over the buffer */
00181                 if (Aliases < &Buffer->Protoent.p_aliases[MAXALIASES - 1])
00182                 {
00183                     /* Write the alias */
00184                     *Aliases++ = Entry;
00185                 }
00186 
00187                 /* Get to the next entry */
00188                 Entry = GetProtoPatternMatch(Entry, " \t");
00189                 if (Entry) *Entry++ = ANSI_NULL;
00190             }
00191         }
00192 
00193         /* Terminate the list */
00194         *Aliases = NULL;
00195 
00196         /* Return to caller */
00197         ReturnedProtoent = &Buffer->Protoent;
00198         break;
00199     }
00200 
00201     /* Return whatever we got */
00202     return ReturnedProtoent;
00203 }
00204 
00205 /*
00206  * @implemented
00207  */
00208 LPPROTOENT
00209 WSAAPI
00210 getprotobynumber(IN INT number)
00211 {
00212     PWSPROCESS Process;
00213     PWSTHREAD Thread;
00214     INT ErrorCode;
00215     PPROTOENT Protoent;
00216     PVOID GetProtoBuffer; 
00217     HANDLE DbHandle;
00218     DPRINT("getprotobynumber: %lx\n", number);
00219 
00220     /* Enter prolog */
00221     if ((ErrorCode = WsApiProlog(&Process, &Thread)) != ERROR_SUCCESS)
00222     {
00223         /* Leave now */
00224         SetLastError(ErrorCode);
00225         return NULL;
00226     }
00227 
00228     /* Get our buffer */
00229     GetProtoBuffer = WsThreadGetProtoBuffer(Thread);
00230     if (!GetProtoBuffer)
00231     {
00232         /* Fail */
00233         SetLastError(WSANO_DATA);
00234         return NULL;
00235     }
00236 
00237     /* Open the network database */
00238     DbHandle = GetProtoOpenNetworkDatabase("protocol");
00239     if (DbHandle == INVALID_HANDLE_VALUE)
00240     {
00241         /* Couldn't open the DB; fail */
00242         SetLastError(WSANO_DATA);
00243         return NULL;
00244     }
00245 
00246     /* Start the scan loop */
00247     while (TRUE)
00248     {
00249         /* Get a protoent entry */
00250         Protoent = GetProtoGetNextEnt(DbHandle, GetProtoBuffer);
00251 
00252         /* Break if we didn't get any new one */
00253         if (!Protoent) break;
00254 
00255         /* Break if we have a match */
00256         if (Protoent->p_proto == number) break;
00257     }
00258 
00259     /* Close the network database */
00260     CloseHandle(DbHandle);
00261 
00262     /* Set error if we don't have a protoent */
00263     if (!Protoent) SetLastError(WSANO_DATA);
00264 
00265     /* Return it */
00266     return Protoent;
00267 }
00268 
00269 /*
00270  * @implemented
00271  */
00272 LPPROTOENT
00273 WSAAPI
00274 getprotobyname(IN CONST CHAR FAR *name)
00275 {
00276     PWSPROCESS Process;
00277     PWSTHREAD Thread;
00278     INT ErrorCode;
00279     PPROTOENT Protoent;
00280     PVOID GetProtoBuffer; 
00281     HANDLE DbHandle;
00282     DPRINT("getprotobyname: %s\n", name);
00283 
00284     /* Enter prolog */
00285     if ((ErrorCode = WsApiProlog(&Process, &Thread)) != ERROR_SUCCESS)
00286     {
00287         /* Leave now */
00288         SetLastError(ErrorCode);
00289         return NULL;
00290     }
00291 
00292     /* Get our buffer */
00293     GetProtoBuffer = WsThreadGetProtoBuffer(Thread);
00294     if (!GetProtoBuffer)
00295     {
00296         /* Fail */
00297         SetLastError(WSANO_DATA);
00298         return NULL;
00299     }
00300 
00301     /* Open the network database */
00302     DbHandle = GetProtoOpenNetworkDatabase("protocol");
00303     if (DbHandle == INVALID_HANDLE_VALUE)
00304     {
00305         /* Couldn't open the DB; fail */
00306         SetLastError(WSANO_DATA);
00307         return NULL;
00308     }
00309 
00310     /* Start the scan loop */
00311     while (TRUE)
00312     {
00313         /* Get a protoent entry */
00314         Protoent = GetProtoGetNextEnt(DbHandle, GetProtoBuffer);
00315 
00316         /* Break if we didn't get any new one */
00317         if (!Protoent) break;
00318 
00319         /* Break if we have a match */
00320         if (!_stricmp(Protoent->p_name, name)) break;
00321     }
00322 
00323     /* Close the network database */
00324     CloseHandle(DbHandle);
00325 
00326     /* Set error if we don't have a protoent */
00327     if (!Protoent) SetLastError(WSANO_DATA);
00328 
00329     /* Return it */
00330     return Protoent;
00331 }

Generated on Sat May 26 2012 04:25:39 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.