rxsock.h
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /***************************************************************************/
39 /* REXX sockets function support rxsock.h */
40 /* sockets utility function package */
41 /***************************************************************************/
42 
43 #include "oorexxapi.h"
44 
45 /*------------------------------------------------------------------
46  * typedef for struct
47  *------------------------------------------------------------------*/
48 typedef struct sockaddr_in sockaddr_in;
49 typedef struct in_addr in_addr;
50 
51 #if defined(WIN32)
52 typedef int socklen_t;
53 #endif
54 
55 class StemManager;
56 
57 
58 /*------------------------------------------------------------------
59  * strip blanks from a line
60  *------------------------------------------------------------------*/
61 void stripBlanks(char *string);
62 
63 /*------------------------------------------------------------------
64  * convert a stem variable to an array of ints
65  *------------------------------------------------------------------*/
66 void stemToIntArray(RexxCallContext *context, RexxObjectPtr stem, int &count, int *&arr);
67 
68 /*------------------------------------------------------------------
69  * convert an array of ints to a stem variable
70  *------------------------------------------------------------------*/
71 void intArrayToStem(RexxCallContext *context, RexxObjectPtr stem, int count, int *arr);
72 
73 /*------------------------------------------------------------------
74  * convert a stemmed variable to a sockaddr
75  *------------------------------------------------------------------*/
77 
78 /*------------------------------------------------------------------
79  * convert a sockaddr to a stemmed variable
80  *------------------------------------------------------------------*/
82 
83 /*------------------------------------------------------------------
84  * convert a hostent to a stemmed variable
85  *------------------------------------------------------------------*/
86 void hostEntToStem(RexxCallContext *context, struct hostent *pHostEnt, StemManager &stem);
87 
88 /*------------------------------------------------------------------
89  * convert a string sock option to an integer
90  *------------------------------------------------------------------*/
91 int stringToSockOpt(const char *pszOptName);
92 
93 /*------------------------------------------------------------------
94  * set errno
95  *------------------------------------------------------------------*/
97 
98 /*------------------------------------------------------------------
99  * set h_errno
100  *------------------------------------------------------------------*/
102 
103 /*------------------------------------------------------------------
104  * perform end-of-function processing (mostly setting error info
105  *------------------------------------------------------------------*/
107 
108 /*------------------------------------------------------------------
109  * portable caseless compare function.
110  *------------------------------------------------------------------*/
111 int caselessCompare(const char *op1, const char *op2);
112 
113 
115 {
116 public:
117  StemManager(RexxCallContext *c) : context(c), stem(NULL), prefix(NULL) { }
119  {
120  if (prefix != NULL)
121  {
122  free(prefix);
123  }
124  }
125 
126  /**
127  * Resolve the stem object that was passed as an argument.
128  *
129  * @param source The source argument object.
130  *
131  * @return true if the stem could be resolved, false for any errors
132  * resolving the stem object.
133  */
135  {
136  // handle the case where no stem was provided at all
137  if (source == NULL) {
138  return false;
139  }
140  // this is the simplest solution
141  if (context->IsStem(source))
142  {
143  stem = (RexxStemObject)source;
144  }
145  else
146  {
147  const char *stemName = context->ObjectToStringValue(source);
148  const char *dotPos = strchr(stemName, '.');
149  // if no dot or the dot is the last character, this is a standard
150  // stem value
151  if (dotPos == NULL || dotPos == (stemName + strlen(stemName) - 1))
152  {
153  stem = context->ResolveStemVariable(source);
154  }
155  else
156  {
157  prefix = strdup(dotPos + 1);
158  if (prefix == NULL)
159  {
160  context->InvalidRoutine();
161  return false;
162  }
163 
164  // uppercase the rest of the prefix value
165  char *scanner = prefix;
166  while (*scanner != '\0')
167  {
168  *scanner = toupper(*scanner);
169  scanner++;
170  }
171  RexxStringObject stemPortion = context->String(stemName, (dotPos - stemName) + 1);
172  stem = context->ResolveStemVariable(stemPortion);
173  }
174  if (stem == NULL)
175  {
176  // context->InvalidRoutine();
177  // don't call context->InvalidRoutine() because we
178  // want to allow an empty string as a parm
179  return false;
180  }
181  }
182  return true;
183  }
184 
185  /**
186  * Set a value in the argument stem.
187  *
188  * @param name The name to set.
189  * @param value
190  */
191  void setValue(const char *name, RexxObjectPtr value)
192  {
193  if (prefix == NULL)
194  {
195  context->SetStemElement(stem, name, value);
196  }
197  else
198  {
199  char fullName[256];
200  sprintf(fullName, "%s%s", prefix, name);
201  context->SetStemElement(stem, fullName, value);
202  }
203  }
204 
205  /**
206  * Set a value in the argument stem.
207  *
208  * @param index The index to set.
209  * @param value
210  */
211  void setValue(size_t index, RexxObjectPtr value)
212  {
213  if (prefix == NULL)
214  {
215  context->SetStemArrayElement(stem, index, value);
216  }
217  else
218  {
219  char fullName[256];
220  sprintf(fullName, "%s.%d", prefix, (int)index);
221  context->SetStemElement(stem, fullName, value);
222  }
223  }
224 
225  /**
226  * Retrieve a value from an argument stem.
227  *
228  * @param name The argument stem name.
229  *
230  * @return The retrieved object, if any.
231  */
232  RexxObjectPtr getValue(const char *name)
233  {
234  if (prefix == NULL)
235  {
236  return context->GetStemElement(stem, name);
237  }
238  else
239  {
240  char fullName[256];
241  sprintf(fullName, "%s%s", prefix, name);
242  return context->GetStemElement(stem, fullName);
243  }
244  }
245 
246  RexxObjectPtr getValue(size_t index)
247  {
248  if (prefix == NULL)
249  {
250  return context->GetStemArrayElement(stem, index);
251  }
252  else
253  {
254  char fullName[256];
255  sprintf(fullName, "%s.%d", prefix, (int)index);
256  return context->GetStemElement(stem, fullName);
257  }
258  }
259 
260 
261 protected:
262  RexxCallContext *context; // the context pointer
263  RexxStemObject stem; // the target stem
264  char *prefix; // extra prefix to use on the stem
265 };
~StemManager()
Definition: rxsock.h:118
RexxCallContext * context
Definition: rxsock.h:262
char * prefix
Definition: rxsock.h:264
StemManager(RexxCallContext *c)
Definition: rxsock.h:117
RexxObjectPtr getValue(const char *name)
Definition: rxsock.h:232
bool resolveStem(RexxObjectPtr source)
Definition: rxsock.h:134
RexxStemObject stem
Definition: rxsock.h:263
void setValue(size_t index, RexxObjectPtr value)
Definition: rxsock.h:211
void setValue(const char *name, RexxObjectPtr value)
Definition: rxsock.h:191
RexxObjectPtr getValue(size_t index)
Definition: rxsock.h:246
struct _RexxStringObject * RexxStringObject
Definition: rexx.h:128
struct _RexxObjectPtr * RexxObjectPtr
Definition: rexx.h:127
struct _RexxStemObject * RexxStemObject
Definition: rexx.h:139
void intArrayToStem(RexxCallContext *context, RexxObjectPtr stem, int count, int *arr)
Definition: rxsock.cpp:232
void stemToIntArray(RexxCallContext *context, RexxObjectPtr stem, int &count, int *&arr)
Definition: rxsock.cpp:173
struct sockaddr_in sockaddr_in
Definition: rxsock.h:48
int stringToSockOpt(const char *pszOptName)
Definition: rxsock.cpp:422
void stemToSockAddr(RexxCallContext *context, StemManager &stem, sockaddr_in *pSockAddr)
Definition: rxsock.cpp:261
void hostEntToStem(RexxCallContext *context, struct hostent *pHostEnt, StemManager &stem)
Definition: rxsock.cpp:364
void sockAddrToStem(RexxCallContext *context, sockaddr_in *pSockAddr, StemManager &stem)
Definition: rxsock.cpp:343
int caselessCompare(const char *op1, const char *op2)
Definition: rxsock.cpp:108
void cleanup(RexxCallContext *context)
Definition: rxsock.cpp:584
void stripBlanks(char *string)
Definition: rxsock.cpp:124
void SetH_Errno(RexxCallContext *context)
struct in_addr in_addr
Definition: rxsock.h:49
void setErrno(RexxCallContext *context)
Definition: rxsock.cpp:453