Commit | Line | Data |
---|---|---|
b47f3fa2 RP |
1 | /* This module supports sending and receiving data objects over a |
2 | socket conection. | |
3 | ||
4 | Copyright 1993 Free Software Foundation, Inc. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | ||
d7df9c3c SG |
22 | static char udr_c[]="@(#)udr.c 2.8 Daniel Mann"; |
23 | static char udr_c_AMD[]="@(#)udr.c 2.3, AMD"; | |
24 | /* | |
d7df9c3c SG |
25 | * All data is serialised into a character stream, |
26 | * and de-serialised back into the approproiate objects. | |
27 | ********************************************************************** HISTORY | |
28 | */ | |
29 | #include <stdio.h> | |
d7df9c3c | 30 | #include <sys/types.h> |
a2fbd4c8 JK |
31 | |
32 | /* This used to say sys/fcntl.h, but the only systems I know of that | |
33 | require that are old (pre-4.3, at least) BSD systems, which we | |
34 | probably don't need to worry about. */ | |
35 | #include <fcntl.h> | |
36 | ||
d7df9c3c SG |
37 | #include <sys/socket.h> |
38 | #include "udiproc.h" | |
39 | #include "udisoc.h" | |
40 | ||
41 | extern int errno; | |
42 | extern char* malloc(); | |
43 | ||
44 | /* local type decs. and macro defs. not in a .h file ************* MACRO/TYPE | |
45 | */ | |
46 | ||
47 | /* global dec/defs. which are not in a .h file ************* EXPORT DEC/DEFS | |
48 | */ | |
49 | int udr_errno; /* error occurs during UDR service */ | |
50 | ||
51 | /* local dec/defs. which are not in a .h file *************** LOCAL DEC/DEFS | |
52 | */ | |
53 | ||
54 | /****************************************************************** UDR_CREATE | |
55 | * Build UDR structure for character stream processing. | |
56 | */ | |
57 | int udr_create(udrs, sd, size) | |
58 | UDR* udrs; | |
59 | int sd; | |
60 | int size; | |
61 | { | |
62 | udrs->sd = sd; | |
63 | if(!udrs->buff) udrs->buff = malloc(size); | |
64 | udrs->getbytes = udrs->buff; /* set the buffer to the start */ | |
65 | udrs->putbytes = udrs->buff; | |
66 | udrs->putend = udrs->buff; | |
67 | udrs->udr_op = -1; /* don't know the direction */ | |
68 | udrs->previous_op = -1; /* don't know the direction */ | |
69 | udrs->bufsize = size; | |
70 | return 0; | |
71 | } | |
72 | ||
73 | /******************************************************************** UDR_FREE | |
74 | * Free USR structure and close socket. | |
75 | */ | |
76 | int udr_free(udrs) | |
77 | UDR* udrs; | |
78 | { | |
79 | close(udrs->sd); | |
80 | free(udrs->buff); | |
81 | return 0; | |
82 | } | |
83 | ||
84 | /****************************************************************** UDR_SIGNAL | |
85 | * Send a signal to the process at the other end of the socket, | |
86 | * indicating that it should expect to recieve a new message shortly. | |
87 | */ | |
88 | int udr_signal(udrs) | |
89 | UDR* udrs; | |
90 | { | |
91 | if(send(udrs->sd, "I", 1, MSG_OOB) == -1) | |
92 | { perror("ERROR, udr_signal(), send(...MSG_OOB)"); | |
93 | udr_errno = UDIErrorIPCInternal; | |
94 | return -1; /* return error code */ | |
95 | } | |
96 | return 0; | |
97 | } | |
98 | ||
99 | /***************************************************************** UDR_SENDNOW | |
100 | * used to flush the current character stream buffer to | |
101 | * the associated socket. */ | |
102 | int udr_sendnow(udrs) | |
103 | UDR* udrs; | |
104 | { | |
105 | int size = (UDIUInt32)(udrs->putend) - (UDIUInt32)(udrs->buff); | |
106 | if(udrs->previous_op == 0) | |
107 | { udr_errno = UDIErrorIPCInternal; | |
108 | return -1; | |
109 | } | |
110 | udrs->putbytes = udrs->buff; | |
111 | udrs->putend = udrs->buff; | |
112 | if (write(udrs->sd, udrs->buff, size) == -1) | |
113 | { perror("ERROR, udr_sendnow(), write() call: "); | |
114 | udr_errno = UDIErrorIPCInternal; | |
115 | return -1; /* return error code */ | |
116 | } | |
117 | return 0; | |
118 | } | |
119 | ||
120 | /******************************************************************** UDR_WORK | |
121 | * Function to send or recieve data from the buffers supporting | |
122 | * socket communication. The buffer contains serialised objects | |
123 | * sent/recieved over a socket connection. | |
124 | */ | |
125 | int udr_work(udrs, object_p, size) | |
126 | UDR* udrs; | |
127 | void* object_p; | |
128 | int size; | |
129 | { | |
130 | int cnt, remain; | |
131 | ||
132 | if(udrs->udr_op != udrs->previous_op) | |
133 | { if(udrs->previous_op == 0) | |
134 | { udr_errno = UDIErrorIPCInternal; | |
135 | return -1; | |
136 | } | |
137 | udrs->previous_op= udrs->udr_op; | |
138 | udrs->putbytes = udrs->buff; | |
139 | udrs->getbytes = udrs->buff; | |
140 | } | |
141 | ||
142 | if(udrs->udr_op == UDR_ENCODE) | |
143 | { /* write data into character stream buffer */ | |
144 | if( (UDIUInt32)(udrs->putbytes) + size > | |
145 | (UDIUInt32)(udrs->buff) + (UDIUInt32)(udrs->bufsize) ) | |
146 | { udr_errno = UDIErrorIPCInternal; | |
147 | return -1; | |
148 | } | |
149 | bcopy((char*)object_p, udrs->putbytes, size); | |
150 | udrs->putbytes += size; | |
151 | if(udrs->putbytes > udrs->putend) udrs->putend = udrs->putbytes; | |
152 | } | |
153 | else if(udrs->udr_op == UDR_DECODE) | |
154 | { | |
155 | if( (UDIUInt32)(udrs->putbytes)-(UDIUInt32)(udrs->getbytes) < size ) | |
156 | { /* need more data in character stream buffer */ | |
157 | remain = (UDIUInt32)(udrs->bufsize) - | |
158 | ( (UDIUInt32)(udrs->putbytes)-(UDIUInt32)(udrs->buff) ); | |
159 | if( ((UDIUInt32)(udrs->bufsize) + (UDIUInt32)(udrs->buff) | |
160 | - (UDIUInt32)(udrs->getbytes)) < size) | |
161 | { udr_errno = UDIErrorIPCInternal; | |
162 | return -1; | |
163 | } | |
164 | cnt = read(udrs->sd, (char*)udrs->putbytes, remain); | |
165 | if(cnt == -1) perror("ERROR udr_work(), read() failure: "); | |
166 | udrs->putbytes += cnt; | |
167 | if( (UDIUInt32)(udrs->putbytes)-(UDIUInt32)(udrs->getbytes) < size ) | |
168 | { udr_errno = UDIErrorIPCInternal; | |
169 | return -1; /* return error code */ | |
170 | } | |
171 | } /* read data from character stream buffer */ | |
172 | bcopy(udrs->getbytes, (char*)object_p, size); | |
173 | udrs->getbytes += size; | |
174 | } | |
175 | else | |
176 | { udr_errno = UDIErrorIPCInternal; | |
177 | return -1; | |
178 | } | |
179 | return 0; | |
180 | } | |
181 | ||
182 | /************************************************************* UDR_UDIResource | |
183 | */ | |
184 | int udr_UDIResource(udrs, object_p) | |
185 | UDR* udrs; | |
186 | UDIResource* object_p; | |
187 | { | |
188 | int retval; | |
189 | ||
190 | retval = udr_CPUSpace(udrs, &object_p->Space); | |
191 | retval = retval | udr_CPUOffset(udrs, &object_p->Offset); | |
192 | return retval; | |
193 | } | |
194 | ||
195 | /**************************************************************** UDR_UDIRange | |
196 | */ | |
197 | int udr_UDIRange(udrs, object_p) | |
198 | UDR* udrs; | |
199 | UDIRange* object_p; | |
200 | { | |
201 | int retval; | |
202 | ||
203 | retval = udr_CPUOffset(udrs, &object_p->Low); | |
204 | retval = retval | udr_CPUOffset(udrs, &object_p->High); | |
205 | return retval; | |
206 | } | |
207 | ||
208 | /********************************************************** UDR_UDIMemoryRange | |
209 | */ | |
210 | int udr_UDIMemoryRange(udrs, object_p) | |
211 | UDR* udrs; | |
212 | UDIMemoryRange* object_p; | |
213 | { | |
214 | int retval; | |
215 | ||
216 | retval = udr_CPUSpace(udrs, &object_p->Space); | |
217 | retval = retval | udr_CPUOffset(udrs, &object_p->Offset); | |
218 | retval = retval | udr_CPUSizeT(udrs, &object_p->Size); | |
219 | return retval; | |
220 | } | |
221 | ||
222 | /****************************************************************** UDR_string | |
223 | */ | |
224 | int udr_string(udrs, sp) | |
225 | UDR* udrs; | |
226 | char* sp; | |
227 | { | |
228 | int len, retval; | |
229 | ||
230 | if(udrs->udr_op == UDR_ENCODE) | |
231 | { | |
232 | if(sp) | |
233 | { len = strlen(sp) + 1; | |
234 | retval = udr_UDIInt32(udrs, &len); | |
235 | retval = retval | udr_work(udrs, sp, len); | |
236 | } | |
237 | else /* deal with NULL pointer */ | |
238 | { len = 0; | |
239 | retval = udr_UDIInt32(udrs, &len); | |
240 | } | |
241 | } | |
242 | else if(udrs->udr_op == UDR_DECODE) | |
243 | { | |
244 | retval = udr_UDIInt32(udrs, &len); | |
245 | if(len) | |
246 | retval = retval | udr_work(udrs, sp, len); | |
247 | else *sp = '\0'; /* terminate string */ | |
248 | } | |
249 | else | |
250 | { udr_errno = UDIErrorIPCInternal; | |
251 | return -1; | |
252 | } | |
253 | return retval; | |
254 | } | |
255 | ||
256 | /******************************************************************* UDR_BYTES | |
257 | */ | |
258 | int udr_bytes(udrs, ptr, len) | |
259 | UDR* udrs; | |
260 | char* ptr; | |
261 | int len; | |
262 | { | |
263 | return udr_work(udrs, ptr, len); | |
264 | } | |
265 | ||
266 | /********************************************************************* UDR_INT | |
267 | */ | |
268 | int udr_int(udrs, int_p) | |
269 | UDR* udrs; | |
270 | int* int_p; | |
271 | { | |
272 | int ret_val; | |
273 | UDIInt32 udr_obj; /* object of know size */ | |
274 | ||
275 | if(udrs->udr_op == UDR_ENCODE) | |
276 | { | |
277 | udr_obj = *int_p; /* copy into know object size */ | |
278 | return udr_UDIInt32(udrs, &udr_obj); | |
279 | } | |
280 | else if(udrs->udr_op == UDR_DECODE) | |
281 | { | |
282 | ret_val = udr_UDIInt32(udrs, &udr_obj); /* get object of known size */ | |
283 | *int_p = udr_obj; | |
284 | return ret_val; | |
285 | } | |
286 | else | |
287 | { udr_errno = UDIErrorIPCInternal; | |
288 | return -1; | |
289 | } | |
290 | } | |
291 | ||
292 | /****************************************************************** UDR_INLINE | |
293 | */ | |
294 | char* udr_inline(udrs, size) | |
295 | UDR* udrs; | |
296 | int size; | |
297 | { | |
298 | if(udrs->udr_op != udrs->previous_op) | |
299 | { if(udrs->previous_op == 0) | |
300 | { udr_errno = UDIErrorIPCInternal; | |
301 | return 0; | |
302 | } | |
303 | udrs->previous_op= udrs->udr_op; | |
304 | udrs->putbytes = udrs->buff; | |
305 | udrs->getbytes = udrs->buff; | |
306 | } | |
307 | if(udrs->udr_op == UDR_ENCODE) | |
308 | { | |
309 | if(udrs->putbytes + size > udrs->bufsize + udrs->buff) | |
310 | return 0; | |
311 | udrs->putbytes += size; | |
312 | return udrs->putbytes - size; | |
313 | } | |
314 | else if(udrs->udr_op == UDR_DECODE) | |
315 | { | |
316 | if(udrs->getbytes + size > udrs->bufsize + udrs->buff) | |
317 | return 0; | |
318 | udrs->getbytes += size; | |
319 | return udrs->getbytes - size; | |
320 | } | |
321 | else | |
322 | { udr_errno = UDIErrorIPCInternal; | |
323 | return 0; | |
324 | } | |
325 | } | |
326 | ||
327 | /****************************************************************** UDR_GETPOS | |
328 | */ | |
329 | char* udr_getpos(udrs) | |
330 | UDR* udrs; | |
331 | { | |
332 | if(udrs->udr_op == UDR_ENCODE) | |
333 | { | |
334 | return udrs->putbytes; | |
335 | } | |
336 | else if(udrs->udr_op == UDR_DECODE) | |
337 | { | |
338 | return udrs->getbytes; | |
339 | } | |
340 | else | |
341 | { udr_errno = UDIErrorIPCInternal; | |
342 | return 0; | |
343 | } | |
344 | } | |
345 | ||
346 | /****************************************************************** UDR_SETPOS | |
347 | */ | |
348 | int udr_setpos(udrs, pos) | |
349 | UDR* udrs; | |
350 | char* pos; | |
351 | { | |
352 | if( ((UDIUInt32)pos > (UDIUInt32)(udrs->buff) + (UDIUInt32)(udrs->bufsize)) | |
353 | || ((UDIUInt32)pos < (UDIUInt32)(udrs->buff) ) ) | |
354 | { udr_errno = UDIErrorIPCInternal; | |
355 | return 0; | |
356 | } | |
357 | if(udrs->udr_op == UDR_ENCODE) | |
358 | { | |
359 | udrs->putbytes = pos; | |
360 | return 1; | |
361 | } | |
362 | else if(udrs->udr_op == UDR_DECODE) | |
363 | { | |
364 | udrs->getbytes = pos; | |
365 | return 1; | |
366 | } | |
367 | else | |
368 | { udr_errno = UDIErrorIPCInternal; | |
369 | return 0; | |
370 | } | |
371 | } | |
372 | ||
373 | /***************************************************************** UDR_READNOW | |
374 | * Try and ensure "size" bytes are available in the | |
375 | * receive buffer character stream. | |
376 | */ | |
377 | int udr_readnow(udrs, size) | |
378 | UDR* udrs; | |
379 | int size; | |
380 | { | |
381 | int cnt, remain; | |
382 | ||
383 | if(udrs->udr_op == UDR_ENCODE) | |
384 | { | |
385 | udr_errno = UDIErrorIPCInternal; | |
386 | return -1; | |
387 | } | |
388 | else if(udrs->udr_op == UDR_DECODE) | |
389 | { | |
390 | if( (UDIUInt32)(udrs->putbytes)-(UDIUInt32)(udrs->getbytes) < size ) | |
391 | { /* need more data in character stream buffer */ | |
392 | remain = (UDIUInt32)(udrs->bufsize) - | |
393 | ( (UDIUInt32)(udrs->putbytes)-(UDIUInt32)(udrs->buff) ); | |
394 | cnt = read(udrs->sd, (char*)udrs->putbytes, remain); | |
395 | if(cnt == -1) perror("ERROR udr_work(), read() failure: "); | |
396 | udrs->putbytes += cnt; | |
397 | if( (UDIUInt32)(udrs->putbytes)-(UDIUInt32)(udrs->getbytes) < size ) | |
398 | { fprintf(stderr,"ERROR, udr_readnow() too few bytes in stream\n"); | |
399 | return -1; /* return error code */ | |
400 | } | |
401 | } | |
402 | } | |
403 | else | |
404 | { udr_errno = UDIErrorIPCInternal; | |
405 | return -1; | |
406 | } | |
407 | return 0; | |
408 | } | |
409 | ||
410 | /******************************************************************* UDR_ALIGN | |
411 | */ | |
412 | int udr_align(udrs, size) | |
413 | UDR* udrs; | |
414 | int size; | |
415 | { | |
416 | char* align; | |
417 | int offset; | |
418 | ||
419 | align = udr_getpos(udrs); | |
420 | offset = size - ((int)align & (size -1)); | |
421 | offset = offset & (size -1); | |
422 | if(offset) udr_setpos(udrs, align + offset); | |
423 | } |