6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
9 * Revision 1.3 1988-04-19 12:45:20 mar
10 * new version number passing and checking
12 * Revision 1.2 87/08/02 21:49:15 wesommer
13 * Clean up error handling, memory handling.
15 * Revision 1.1 87/06/16 17:48:21 wesommer
18 * Revision 1.4 87/06/04 01:32:18 wesommer
21 * Revision 1.3 87/06/01 03:33:54 wesommer
22 * Added destroy_reply.
24 * Revision 1.2 87/05/31 22:03:37 wesommer
25 * Fixed numerous bugs; still shaky.
30 static char *rcsid_sms_param_c = "$Header$";
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include "sms_private.h"
38 * GDB operations to send and recieve RPC requests and replies.
42 * This doesn't get called until after the actual buffered write completes.
43 * In a non-preflattening version of this, this would then queue the
44 * write of the next bunch of data.
47 sms_cont_send(op, hcon, arg)
50 struct sms_params *arg;
52 op->result = OP_SUCCESS;
53 free(arg->sms_flattened);
54 arg->sms_flattened = NULL;
59 sms_start_send(op, hcon, arg)
62 register struct sms_params *arg;
65 unsigned int sms_size;
70 * This should probably be split into several routines.
71 * It could also probably be made more efficient (punting most
72 * of the argument marshalling stuff) by doing I/O directly
73 * from the strings. Anyone for a scatter/gather sms_send_data?
75 * that would look a lot like the uio stuff in the kernel.. hmm.
79 * Marshall the entire data right now..
80 * We are sending the version number,
81 * total request size, request number,
82 * argument count, and then each argument.
83 * At least for now, each argument is a string, which is
84 * sent as a count of bytes followed by the bytes
85 * (including the trailing '\0'), padded
86 * to a longword boundary.
89 sms_size = 4 * sizeof(long);
91 argl = (int *)malloc((unsigned)(sizeof(int) * arg->sms_argc));
94 * For each argument, figure out how much space is needed.
97 for (i = 0; i < arg->sms_argc; ++i) {
99 argl[i] = len = arg->sms_argl[i];
101 argl[i] = len = strlen(arg->sms_argv[i]) + 1;
102 sms_size += sizeof(long) + len;
103 /* Round up to next longword boundary.. */
104 sms_size = sizeof(long) * howmany(sms_size, sizeof(long));
107 arg->sms_flattened = buf = malloc(sms_size);
109 bzero(arg->sms_flattened, sms_size);
111 arg->sms_size = sms_size;
114 * This is gross. Any better suggestions, anyone?
115 * It should work on the RT's, since malloc is guaranteed to
116 * return a pointer which is aligned correctly for any data.
119 ((long *)buf)[0] = htonl(sms_size);
120 ((long *)buf)[1] = htonl(arg->sms_version_no);
121 ((long *)buf)[2] = htonl(arg->sms_procno);
122 ((long *)buf)[3] = htonl(arg->sms_argc);
125 * bp is a pointer into the point in the buffer to put
129 bp = (char *)(((long *)buf) + 4);
131 for (i = 0; i<arg->sms_argc; ++i) {
133 *((long *)bp) = htonl(len);
135 bcopy(arg->sms_argv[i], bp, len);
136 bp += sizeof(long) * howmany(len, sizeof(long));
138 op->fcn.cont = sms_cont_send;
139 arg->sms_size = sms_size;
143 if (gdb_send_data(hcon, arg->sms_flattened, sms_size) == OP_COMPLETE)
144 return sms_cont_send(op, hcon, arg);
145 else return OP_RUNNING;
148 sms_cont_recv(op, hcon, argp)
150 HALF_CONNECTION hcon;
157 register sms_params *arg = *argp;
160 switch (arg->sms_state) {
162 arg->sms_state = S_RECV_DATA;
163 if (gdb_receive_data(hcon, (caddr_t)&arg->sms_size,
164 sizeof(long)) == OP_COMPLETE)
170 /* Should validate that length is reasonable */
171 arg->sms_size = ntohl(arg->sms_size);
172 if (arg->sms_size > 65536) {
175 arg->sms_flattened = malloc(arg->sms_size);
176 arg->sms_state = S_DECODE_DATA;
177 bcopy((caddr_t)&arg->sms_size, arg->sms_flattened, sizeof(long));
179 if (gdb_receive_data(hcon,
180 arg->sms_flattened + sizeof(long),
181 arg->sms_size - sizeof(long))
187 cp = arg->sms_flattened;
189 /* we already got the overall length.. */
190 for(i=1; i <4; i++) ip[i] = ntohl(ip[i]);
191 arg->sms_version_no = ip[1];
192 if (arg->sms_version_no != SMS_VERSION_1 &&
193 arg->sms_version_no != SMS_VERSION_2)
194 arg->sms_status = SMS_VERSION_MISMATCH;
195 else arg->sms_status = ip[2];
196 arg->sms_argc = ip[3];
197 cp += 4 * sizeof(int);
198 arg->sms_argv=(char **)malloc(arg->sms_argc *sizeof(char **));
199 arg->sms_argl=(int *)malloc(arg->sms_argc *sizeof(int *));
201 for (i = 0; i<arg->sms_argc; ++i) {
202 u_short nlen = ntohl(* (int *) cp);
204 if (cp + nlen > arg->sms_flattened + arg->sms_size) {
205 free(arg->sms_flattened);
206 arg->sms_flattened = NULL;
209 arg->sms_argv[i] = (char *)malloc(nlen);
210 bcopy(cp, arg->sms_argv[i], nlen);
211 arg->sms_argl[i]=nlen;
212 cp += sizeof(long) * howmany(nlen, sizeof(long));
214 free(arg->sms_flattened);
215 arg->sms_flattened = NULL;
223 sms_start_recv(op, hcon, argp)
225 HALF_CONNECTION hcon;
226 struct sms_params **argp;
228 register sms_params *arg = *argp;
230 *argp = arg = (sms_params *)malloc(sizeof(sms_params));
231 arg->sms_argl = NULL;
232 arg->sms_argv = NULL;
233 arg->sms_flattened = NULL;
235 arg->sms_state = S_RECV_START;
236 op->fcn.cont = sms_cont_recv;
237 return sms_cont_recv(op, hcon, argp);
240 sms_destroy_reply(reply)
246 free(reply->sms_argl);
247 reply->sms_argl = NULL;
248 if (reply->sms_flattened)
249 free(reply->sms_flattened);
250 reply->sms_flattened = NULL;
251 if (reply->sms_argv) {
252 for (i=0; i<reply->sms_argc; i++) {
253 if (reply->sms_argv[i])
254 free (reply->sms_argv[i]);
255 reply->sms_argv[i] = NULL;
257 free(reply->sms_argv);
259 reply->sms_argv = NULL;
268 * c-continued-statement-offset: 4
270 * c-argdecl-indent: 4