ReactOS  0.4.14-dev-323-g6fe6a88
getnetpath.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 //#include <sys/cdefs.h>
29 
30 /*
31  * Copyright (c) 1989 by Sun Microsystems, Inc.
32  */
33 
34 //#include <sys/cdefs.h>
35 #include <wintirpc.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <netconfig.h>
39 #include <stdlib.h>
40 #include <string.h>
41 //#include <syslog.h>
42 
43 /*
44  * internal structure to keep track of a netpath "session"
45  */
46 struct netpath_chain {
47  struct netconfig *ncp; /* an nconf entry */
48  struct netpath_chain *nchain_next; /* next nconf entry allocated */
49 };
50 
51 
52 struct netpath_vars {
53  int valid; /* token that indicates a valid netpath_vars */
54  void *nc_handlep; /* handle for current netconfig "session" */
55  char *netpath; /* pointer to current view-point in NETPATH */
56  char *netpath_start; /* pointer to start of our copy of NETPATH */
57  struct netpath_chain *ncp_list; /* list of nconfs allocated this session*/
58 };
59 
60 #define NP_VALID 0xf00d
61 #define NP_INVALID 0
62 
63 char *_get_next_token(char *, int);
64 
65 
66 /*
67  * A call to setnetpath() establishes a NETPATH "session". setnetpath()
68  * must be called before the first call to getnetpath(). A "handle" is
69  * returned to distinguish the session; this handle should be passed
70  * subsequently to getnetpath(). (Handles are used to allow for nested calls
71  * to setnetpath()).
72  * If setnetpath() is unable to establish a session (due to lack of memory
73  * resources, or the absence of the /etc/netconfig file), a NULL pointer is
74  * returned.
75  */
76 
77 void *
79 {
80 
81  struct netpath_vars *np_sessionp; /* this session's variables */
82  char *npp; /* NETPATH env variable */
83 
84 #ifdef MEM_CHK
85  malloc_debug(1);
86 #endif
87 
88  if ((np_sessionp =
89  (struct netpath_vars *)malloc(sizeof (struct netpath_vars))) == NULL) {
90  return (NULL);
91  }
92  if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
93  //syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
94  return (NULL);
95  }
96  np_sessionp->valid = NP_VALID;
97  np_sessionp->ncp_list = NULL;
98  if ((npp = getenv(NETPATH)) == NULL) {
99  np_sessionp->netpath = NULL;
100  } else {
101  (void) endnetconfig(np_sessionp->nc_handlep);/* won't need nc session*/
102  np_sessionp->nc_handlep = NULL;
103  if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) {
104  free(np_sessionp);
105  return (NULL);
106  } else {
107  (void) strcpy(np_sessionp->netpath, npp);
108  }
109  }
110  np_sessionp->netpath_start = np_sessionp->netpath;
111  return ((void *)np_sessionp);
112 }
113 
114 /*
115  * When first called, getnetpath() returns a pointer to the netconfig
116  * database entry corresponding to the first valid NETPATH component. The
117  * netconfig entry is formatted as a struct netconfig.
118  * On each subsequent call, getnetpath returns a pointer to the netconfig
119  * entry that corresponds to the next valid NETPATH component. getnetpath
120  * can thus be used to search the netconfig database for all networks
121  * included in the NETPATH variable.
122  * When NETPATH has been exhausted, getnetpath() returns NULL. It returns
123  * NULL and sets errno in case of an error (e.g., setnetpath was not called
124  * previously).
125  * getnetpath() silently ignores invalid NETPATH components. A NETPATH
126  * compnent is invalid if there is no corresponding entry in the netconfig
127  * database.
128  * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
129  * were set to the sequence of default or visible networks in the netconfig
130  * database, in the order in which they are listed.
131  */
132 
133 struct netconfig *
134 getnetpath(handlep)
135  void *handlep;
136 {
137  struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
138  struct netconfig *ncp = NULL; /* temp. holds a netconfig session */
139  struct netpath_chain *chainp; /* holds chain of ncp's we alloc */
140  char *npp; /* holds current NETPATH */
141 
142  if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
143  errno = EINVAL;
144  return (NULL);
145  }
146  if (np_sessionp->netpath_start == NULL) { /* NETPATH was not set */
147  do { /* select next visible network */
148  if (np_sessionp->nc_handlep == NULL) {
149  np_sessionp->nc_handlep = setnetconfig();
150  if (np_sessionp->nc_handlep == NULL) {
151  //syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
152  }
153  }
154  if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {
155  return(NULL);
156  }
157  } while ((ncp->nc_flag & NC_VISIBLE) == 0);
158  return (ncp);
159  }
160  /*
161  * Find first valid network ID in netpath.
162  */
163  while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
164  np_sessionp->netpath = _get_next_token(npp, ':');
165  /*
166  * npp is a network identifier.
167  */
168  if ((ncp = getnetconfigent(npp)) != NULL) {
169  chainp = (struct netpath_chain *) /* cobble alloc chain entry */
170  malloc(sizeof (struct netpath_chain));
171  chainp->ncp = ncp;
172  chainp->nchain_next = NULL;
173  if (np_sessionp->ncp_list == NULL) {
174  np_sessionp->ncp_list = chainp;
175  } else {
176  np_sessionp->ncp_list->nchain_next = chainp;
177  }
178  return (ncp);
179  }
180  /* couldn't find this token in the database; go to next one. */
181  }
182  return (NULL);
183 }
184 
185 /*
186  * endnetpath() may be called to unbind NETPATH when processing is complete,
187  * releasing resources for reuse. It returns 0 on success and -1 on failure
188  * (e.g. if setnetpath() was not called previously.
189  */
190 int
191 endnetpath(handlep)
192  void *handlep;
193 {
194  struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
195  struct netpath_chain *chainp, *lastp;
196 
197  if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
198  errno = EINVAL;
199  return (-1);
200  }
201  if (np_sessionp->nc_handlep != NULL)
202  endnetconfig(np_sessionp->nc_handlep);
203  if (np_sessionp->netpath_start != NULL)
204  free(np_sessionp->netpath_start);
205  for (chainp = np_sessionp->ncp_list; chainp != NULL;
206  lastp=chainp, chainp=chainp->nchain_next, free(lastp)) {
207  freenetconfigent(chainp->ncp);
208  }
209  free(np_sessionp);
210 #ifdef MEM_CHK
211  if (malloc_verify() == 0) {
212  fprintf(stderr, "memory heap corrupted in endnetpath\n");
213  exit(1);
214  }
215 #endif
216  return (0);
217 }
218 
219 
220 
221 /*
222  * Returns pointer to the rest-of-the-string after the current token.
223  * The token itself starts at arg, and we null terminate it. We return NULL
224  * if either the arg is empty, or if this is the last token.
225  */
226 
227 char *
229 char *npp; /* string */
230 int token; /* char to parse string for */
231 {
232  char *cp; /* char pointer */
233  char *np; /* netpath pointer */
234  char *ep; /* escape pointer */
235 
236  if ((cp = strchr(npp, token)) == NULL) {
237  return (NULL);
238  }
239  /*
240  * did find a token, but it might be escaped.
241  */
242  if ((cp > npp) && (cp[-1] == '\\')) {
243  /* if slash was also escaped, carry on, otherwise find next token */
244  if ((cp > npp + 1) && (cp[-2] != '\\')) {
245  /* shift r-o-s onto the escaped token */
246  strcpy(&cp[-1], cp); /* XXX: overlapping string copy */
247  /*
248  * Do a recursive call.
249  * We don't know how many escaped tokens there might be.
250  */
251  return (_get_next_token(cp, token));
252  }
253  }
254 
255  *cp++ = '\0'; /* null-terminate token */
256  /* get rid of any backslash escapes */
257  ep = npp;
258  while ((np = strchr(ep, '\\')) != 0) {
259  if (np[1] == '\\')
260  np++;
261  strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */
262  }
263  return (cp); /* return ptr to r-o-s */
264 }
#define NETPATH
Definition: netconfig.h:8
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
void * nc_handlep
Definition: getnetpath.c:54
void * setnetpath()
Definition: getnetpath.c:78
Definition: arc.h:39
#define free
Definition: debug_ros.c:5
char * _get_next_token(char *, int)
Definition: getnetpath.c:228
int errno
#define NP_VALID
Definition: getnetpath.c:60
struct netconfig * getnetconfig(void *handlep)
Definition: getnetconfig.c:253
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat token
Definition: glfuncs.h:210
void freenetconfigent(struct netconfig *netconfigp)
Definition: getnetconfig.c:530
smooth NULL
Definition: ftsmooth.c:416
struct netconfig * getnetconfigent(char *netid) const
Definition: getnetconfig.c:432
#define NC_VISIBLE
Definition: netconfig.h:39
struct netconfig * ncp
Definition: getnetpath.c:47
int endnetpath(void *handlep)
Definition: getnetpath.c:191
int endnetconfig(void *handlep)
Definition: getnetconfig.c:373
_Check_return_ char *__cdecl getenv(_In_z_ const char *_VarName)
char * netpath
Definition: getnetpath.c:55
char * strchr(const char *String, int ch)
Definition: utclib.c:501
POINT cp
Definition: magnifier.c:59
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
FILE * stderr
struct netpath_chain * nchain_next
Definition: getnetpath.c:48
struct netconfig * getnetpath(void *handlep)
Definition: getnetpath.c:134
#define malloc
Definition: debug_ros.c:4
char * netpath_start
Definition: getnetpath.c:56
struct netpath_chain * ncp_list
Definition: getnetpath.c:57
void exit(int exitcode)
Definition: _exit.c:33
void * setnetconfig()
Definition: getnetconfig.c:217