source: trunk/src/wsock32/wsastruct.cpp

Last change on this file was 3229, checked in by sandervl, 25 years ago

replaced with new wsock32

File size: 8.8 KB
Line 
1/* $Id: wsastruct.cpp,v 1.3 2000-03-24 19:28:08 sandervl Exp $ */
2
3/*
4 *
5 * Win32 SOCK32 for OS/2 (WSA apis)
6 *
7 * Based on Wine code: (dlls\winsock\async.c,socket.c)
8 * (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
9 * (C) 1999 Marcus Meissner
10 *
11 * Project Odin Software License can be found in LICENSE.TXT
12 *
13 */
14
15/* ----------------------------------- helper functions -
16 *
17 * TODO: Merge WS_dup_..() stuff into one function that
18 * would operate with a generic structure containing internal
19 * pointers (via a template of some kind).
20 */
21
22#define INCL_BASE
23#include <os2wrap.h> //Odin32 OS/2 api wrappers
24
25#include <string.h>
26#include <odinwrap.h>
27#include <os2sel.h>
28#include <stdlib.h>
29#include <win32api.h>
30#include <misc.h>
31
32#include "wsock32.h"
33#include "wsastruct.h"
34
35struct ws_hostent* _check_buffer_he(LPWSINFO pwsi, int size)
36{
37 if( pwsi->he && pwsi->helen >= size ) return pwsi->he;
38 else free(pwsi->he);
39
40 pwsi->he = (struct ws_hostent*)malloc((pwsi->helen = size));
41 return pwsi->he;
42}
43
44struct ws_servent* _check_buffer_se(LPWSINFO pwsi, int size)
45{
46 if( pwsi->se && pwsi->selen >= size ) return pwsi->se;
47 else free(pwsi->se);
48
49 pwsi->se = (struct ws_servent*)malloc((pwsi->selen = size));
50 return pwsi->se;
51}
52
53struct ws_protoent* _check_buffer_pe(LPWSINFO pwsi, int size)
54{
55 if( pwsi->pe && pwsi->pelen >= size ) return pwsi->pe;
56 else free(pwsi->pe);
57
58 pwsi->pe = (struct ws_protoent*)malloc((pwsi->pelen = size));
59 return pwsi->pe;
60}
61
62static int list_size(char** l, int item_size)
63{
64 int i,j = 0;
65 if(l)
66 { for(i=0;l[i];i++)
67 j += (item_size) ? item_size : strlen(l[i]) + 1;
68 j += (i + 1) * sizeof(char*); }
69 return j;
70}
71
72static int list_dup(char** l_src, char* ref, char* base, int item_size)
73{
74 /* base is either either equal to ref or 0 or SEGPTR */
75
76 char* p = ref;
77 char** l_to = (char**)ref;
78 int i,j,k;
79
80 for(j=0;l_src[j];j++) ;
81 p += (j + 1) * sizeof(char*);
82 for(i=0;i<j;i++)
83 { l_to[i] = base + (p - ref);
84 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
85 memcpy(p, l_src[i], k); p += k; }
86 l_to[i] = NULL;
87 return (p - ref);
88}
89
90/* ----- hostent */
91
92static int hostent_size(struct hostent* p_he)
93{
94 int size = 0;
95 if( p_he )
96 { size = sizeof(struct hostent);
97 size += strlen(p_he->h_name) + 1;
98 size += list_size(p_he->h_aliases, 0);
99 size += list_size(p_he->h_addr_list, p_he->h_length ); }
100 return size;
101}
102
103int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag)
104{
105 /* Convert hostent structure into ws_hostent so that the data fits
106 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
107 * relative to pwsi->buffer depending on "flag" value. Returns size
108 * of the data copied (also in the pwsi->buflen).
109 */
110
111 int size = hostent_size(p_he);
112
113 if( size )
114 {
115 struct ws_hostent* p_to;
116 char* p_name,*p_aliases,*p_addr,*p_base,*p;
117
118 _check_buffer_he(pwsi, size);
119 p_to = (struct ws_hostent*)pwsi->he;
120 p = (char*)pwsi->he;
121 p_base = (flag & WS_DUP_OFFSET) ? NULL : p;
122//// : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
123 p += sizeof(struct ws_hostent);
124 p_name = p;
125 strcpy(p, p_he->h_name); p += strlen(p) + 1;
126 p_aliases = p;
127 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)pwsi->he), 0);
128 p_addr = p;
129 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)pwsi->he), p_he->h_length);
130
131 p_to->h_addrtype = (INT16)p_he->h_addrtype;
132 p_to->h_length = (INT16)p_he->h_length;
133 p_to->h_name = (char *)(p_base + (p_name - (char*)pwsi->he));
134 p_to->h_aliases = (char **)(p_base + (p_aliases - (char*)pwsi->he));
135 p_to->h_addr_list = (char **)(p_base + (p_addr - (char*)pwsi->he));
136
137 size += (sizeof(struct ws_hostent) - sizeof(struct hostent));
138 }
139 return size;
140}
141
142/* ----- protoent */
143
144static int protoent_size(struct protoent* p_pe)
145{
146 int size = 0;
147 if( p_pe )
148 { size = sizeof(struct protoent);
149 size += strlen(p_pe->p_name) + 1;
150 size += list_size(p_pe->p_aliases, 0); }
151 return size;
152}
153
154int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag)
155{
156 int size = protoent_size(p_pe);
157 if( size )
158 {
159 struct ws_protoent* p_to;
160 char* p_name,*p_aliases,*p_base,*p;
161
162 _check_buffer_pe(pwsi, size);
163 p_to = (struct ws_protoent*)pwsi->pe;
164 p = (char*)pwsi->pe;
165 p_base = (flag & WS_DUP_OFFSET) ? NULL : p;
166//// : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
167 p += sizeof(struct ws_protoent);
168 p_name = p;
169 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
170 p_aliases = p;
171 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)pwsi->pe), 0);
172
173 p_to->p_proto = (INT16)p_pe->p_proto;
174 p_to->p_name = (char *)(p_base) + (p_name - (char*)pwsi->pe);
175 p_to->p_aliases = (char **)((p_base) + (p_aliases - (char*)pwsi->pe));
176
177 size += (sizeof(struct ws_protoent) - sizeof(struct protoent));
178 }
179 return size;
180}
181
182/* ----- servent */
183
184static int servent_size(struct servent* p_se)
185{
186 int size = 0;
187 if( p_se )
188 { size += sizeof(struct servent);
189 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
190 size += list_size(p_se->s_aliases, 0); }
191 return size;
192}
193
194int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag)
195{
196 int size = servent_size(p_se);
197 if( size )
198 {
199 struct ws_servent* p_to;
200 char* p_name,*p_aliases,*p_proto,*p_base,*p;
201
202 _check_buffer_se(pwsi, size);
203 p_to = (struct ws_servent*)pwsi->se;
204 p = (char*)pwsi->se;
205 p_base = (flag & WS_DUP_OFFSET) ? NULL : p;
206//// : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
207 p += sizeof(struct ws_servent);
208 p_name = p;
209 strcpy(p, p_se->s_name); p += strlen(p) + 1;
210 p_proto = p;
211 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
212 p_aliases = p;
213 list_dup(p_se->s_aliases, p, p_base + (p - (char*)pwsi->se), 0);
214
215 p_to->s_port = (INT16)p_se->s_port;
216 p_to->s_name = (char *)(p_base + (p_name - (char*)pwsi->se));
217 p_to->s_proto = (char *)(p_base + (p_proto - (char*)pwsi->se));
218 p_to->s_aliases = (char **)(p_base + (p_aliases - (char*)pwsi->se));
219
220 size += (sizeof(struct ws_servent) - sizeof(struct servent));
221 }
222 return size;
223}
224/* ----------------------------------- helper functions - */
225
226
227/* ----- hostent */
228
229/* Copy hostent to p_to, fix up inside pointers using p_base (different for
230 * Win16 (linear vs. segmented). Return -neededsize on overrun.
231 */
232int WS_copy_he(struct ws_hostent *p_to,char *p_base,int t_size,struct hostent* p_he)
233{
234 char* p_name,*p_aliases,*p_addr,*p;
235 int size=hostent_size(p_he)+(sizeof(struct ws_hostent)-sizeof(struct hostent));
236
237 if (t_size < size)
238 return -size;
239 p = (char*)p_to;
240 p += sizeof(struct ws_hostent);
241 p_name = p;
242 strcpy(p, p_he->h_name); p += strlen(p) + 1;
243 p_aliases = p;
244 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)p_to), 0);
245 p_addr = p;
246 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)p_to), p_he->h_length);
247
248 p_to->h_addrtype = (INT16)p_he->h_addrtype;
249 p_to->h_length = (INT16)p_he->h_length;
250 p_to->h_name = (p_base + (p_name - (char*)p_to));
251 p_to->h_aliases = (char **)(p_base + (p_aliases - (char*)p_to));
252 p_to->h_addr_list = (char **)(p_base + (p_addr - (char*)p_to));
253
254 return size;
255}
256
257/* ----- protoent */
258
259/* Copy protoent to p_to, fix up inside pointers using p_base (different for
260 * Win16 (linear vs. segmented). Return -neededsize on overrun.
261 */
262int WS_copy_pe(struct ws_protoent *p_to,char *p_base,int t_size,struct protoent* p_pe)
263{
264 char* p_name,*p_aliases,*p;
265 int size=protoent_size(p_pe)+(sizeof(struct ws_protoent)-sizeof(struct protoent));
266
267 if (t_size < size)
268 return -size;
269 p = (char*)p_to;
270 p += sizeof(struct ws_protoent);
271 p_name = p;
272 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
273 p_aliases = p;
274 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)p_to), 0);
275
276 p_to->p_proto = (INT16)p_pe->p_proto;
277 p_to->p_name = (p_base) + (p_name - (char*)p_to);
278 p_to->p_aliases = (char **)((p_base) + (p_aliases - (char*)p_to));
279
280
281 return size;
282}
283
284/* ----- servent */
285
286/* Copy servent to p_to, fix up inside pointers using p_base (different for
287 * Win16 (linear vs. segmented). Return -neededsize on overrun.
288 */
289int WS_copy_se(struct ws_servent *p_to, char *p_base, int t_size, struct servent* p_se)
290{
291 char* p_name,*p_aliases,*p_proto,*p;
292 int size = servent_size(p_se)+(sizeof(struct ws_servent)-sizeof(struct servent));
293
294 if (t_size < size )
295 return -size;
296 p = (char*)p_to;
297 p += sizeof(struct ws_servent);
298 p_name = p;
299 strcpy(p, p_se->s_name); p += strlen(p) + 1;
300 p_proto = p;
301 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
302 p_aliases = p;
303 list_dup(p_se->s_aliases, p, p_base + (p - (char*)p_to), 0);
304
305 p_to->s_port = (INT16)p_se->s_port;
306 p_to->s_name = (p_base + (p_name - (char*)p_to));
307 p_to->s_proto = (p_base + (p_proto - (char*)p_to));
308 p_to->s_aliases = (char **)(p_base + (p_aliases - (char*)p_to));
309
310 return size;
311}
Note: See TracBrowser for help on using the repository browser.