]>
Commit | Line | Data |
---|---|---|
6ff26f0b | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
8defc06b | 6 | * Copyright (C) 1987, 1990 by the Massachusetts Institute of Technology |
babbc197 | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
6ff26f0b | 9 | * |
6ff26f0b | 10 | */ |
11 | ||
12 | #ifndef lint | |
13 | static char *rcsid_sms_param_c = "$Header$"; | |
14 | #endif lint | |
15 | ||
babbc197 | 16 | #include <mit-copyright.h> |
9f56cedf | 17 | #include <sys/types.h> |
18 | #include <netinet/in.h> | |
8defc06b | 19 | #include "mr_private.h" |
8fd777cf | 20 | #include <string.h> |
6ff26f0b | 21 | |
22 | /* | |
23 | * GDB operations to send and recieve RPC requests and replies. | |
24 | */ | |
25 | ||
26 | /* | |
27 | * This doesn't get called until after the actual buffered write completes. | |
28 | * In a non-preflattening version of this, this would then queue the | |
29 | * write of the next bunch of data. | |
30 | */ | |
31 | ||
24582af9 | 32 | /*ARGSUSED*/ |
8defc06b | 33 | mr_cont_send(op, hcon, arg) |
6ff26f0b | 34 | OPERATION op; |
35 | HALF_CONNECTION hcon; | |
8defc06b | 36 | struct mr_params *arg; |
6ff26f0b | 37 | { |
38 | op->result = OP_SUCCESS; | |
8defc06b | 39 | free(arg->mr_flattened); |
40 | arg->mr_flattened = NULL; | |
9f56cedf | 41 | |
6ff26f0b | 42 | return OP_COMPLETE; |
43 | } | |
44 | ||
8defc06b | 45 | mr_start_send(op, hcon, arg) |
6ff26f0b | 46 | OPERATION op; |
47 | HALF_CONNECTION hcon; | |
8defc06b | 48 | register struct mr_params *arg; |
6ff26f0b | 49 | { |
50 | int i, len; | |
8defc06b | 51 | unsigned int mr_size; |
6ff26f0b | 52 | int *argl; |
53 | char *buf, *bp; | |
54 | ||
55 | /* | |
56 | * This should probably be split into several routines. | |
57 | * It could also probably be made more efficient (punting most | |
58 | * of the argument marshalling stuff) by doing I/O directly | |
8defc06b | 59 | * from the strings. Anyone for a scatter/gather mr_send_data? |
6ff26f0b | 60 | * |
61 | * that would look a lot like the uio stuff in the kernel.. hmm. | |
62 | */ | |
63 | ||
64 | /* | |
65 | * Marshall the entire data right now.. | |
66 | * We are sending the version number, | |
67 | * total request size, request number, | |
68 | * argument count, and then each argument. | |
69 | * At least for now, each argument is a string, which is | |
70 | * sent as a count of bytes followed by the bytes | |
71 | * (including the trailing '\0'), padded | |
72 | * to a longword boundary. | |
73 | */ | |
74 | ||
8defc06b | 75 | mr_size = 4 * sizeof(long); |
6ff26f0b | 76 | |
8defc06b | 77 | argl = (int *)malloc((unsigned)(sizeof(int) * arg->mr_argc)); |
6ff26f0b | 78 | |
79 | /* | |
80 | * For each argument, figure out how much space is needed. | |
81 | */ | |
82 | ||
8defc06b | 83 | for (i = 0; i < arg->mr_argc; ++i) { |
84 | if (arg->mr_argl) | |
85 | argl[i] = len = arg->mr_argl[i]; | |
6ff26f0b | 86 | else |
8defc06b | 87 | argl[i] = len = strlen(arg->mr_argv[i]) + 1; |
88 | mr_size += sizeof(long) + len; | |
6ff26f0b | 89 | /* Round up to next longword boundary.. */ |
8defc06b | 90 | mr_size = sizeof(long) * howmany(mr_size, sizeof(long)); |
6ff26f0b | 91 | } |
92 | ||
8defc06b | 93 | arg->mr_flattened = buf = malloc(mr_size); |
6ff26f0b | 94 | |
8fd777cf | 95 | memset(arg->mr_flattened, 0, mr_size); |
6ff26f0b | 96 | |
8defc06b | 97 | arg->mr_size = mr_size; |
6ff26f0b | 98 | |
99 | /* | |
100 | * This is gross. Any better suggestions, anyone? | |
101 | * It should work on the RT's, since malloc is guaranteed to | |
102 | * return a pointer which is aligned correctly for any data. | |
103 | */ | |
104 | ||
8defc06b | 105 | ((long *)buf)[0] = htonl(mr_size); |
106 | ((long *)buf)[1] = htonl(arg->mr_version_no); | |
107 | ((long *)buf)[2] = htonl(arg->mr_procno); | |
108 | ((long *)buf)[3] = htonl(arg->mr_argc); | |
6ff26f0b | 109 | |
110 | /* | |
111 | * bp is a pointer into the point in the buffer to put | |
112 | * the next argument. | |
113 | */ | |
114 | ||
115 | bp = (char *)(((long *)buf) + 4); | |
116 | ||
8defc06b | 117 | for (i = 0; i<arg->mr_argc; ++i) { |
6ff26f0b | 118 | len = argl[i]; |
119 | *((long *)bp) = htonl(len); | |
120 | bp += sizeof(long); | |
8fd777cf | 121 | memcpy(bp, arg->mr_argv[i], len); |
6ff26f0b | 122 | bp += sizeof(long) * howmany(len, sizeof(long)); |
123 | } | |
8defc06b | 124 | op->fcn.cont = mr_cont_send; |
125 | arg->mr_size = mr_size; | |
6ff26f0b | 126 | |
127 | free(argl); | |
128 | ||
8defc06b | 129 | if (gdb_send_data(hcon, arg->mr_flattened, mr_size) == OP_COMPLETE) |
130 | return mr_cont_send(op, hcon, arg); | |
6ff26f0b | 131 | else return OP_RUNNING; |
132 | } | |
133 | ||
24582af9 | 134 | /*ARGSUSED*/ |
8defc06b | 135 | mr_cont_recv(op, hcon, argp) |
6ff26f0b | 136 | OPERATION op; |
137 | HALF_CONNECTION hcon; | |
8defc06b | 138 | mr_params **argp; |
6ff26f0b | 139 | { |
140 | int done = FALSE; | |
141 | char *cp; | |
142 | int *ip; | |
143 | int i; | |
8defc06b | 144 | register mr_params *arg = *argp; |
6ff26f0b | 145 | |
146 | while (!done) { | |
8defc06b | 147 | switch (arg->mr_state) { |
6ff26f0b | 148 | case S_RECV_START: |
8defc06b | 149 | arg->mr_state = S_RECV_DATA; |
150 | if (gdb_receive_data(hcon, (caddr_t)&arg->mr_size, | |
6ff26f0b | 151 | sizeof(long)) == OP_COMPLETE) |
152 | continue; | |
153 | done = TRUE; | |
154 | break; | |
155 | case S_RECV_DATA: | |
156 | fflush(stdout); | |
157 | /* Should validate that length is reasonable */ | |
8defc06b | 158 | arg->mr_size = ntohl(arg->mr_size); |
159 | if (arg->mr_size > 65536) { | |
9f56cedf | 160 | return OP_CANCELLED; |
161 | } | |
8defc06b | 162 | arg->mr_flattened = malloc(arg->mr_size); |
163 | arg->mr_state = S_DECODE_DATA; | |
8fd777cf | 164 | memcpy(arg->mr_flattened, (caddr_t)&arg->mr_size, sizeof(long)); |
6ff26f0b | 165 | |
166 | if (gdb_receive_data(hcon, | |
8defc06b | 167 | arg->mr_flattened + sizeof(long), |
168 | arg->mr_size - sizeof(long)) | |
6ff26f0b | 169 | == OP_COMPLETE) |
170 | continue; | |
171 | done = TRUE; | |
172 | break; | |
173 | case S_DECODE_DATA: | |
8defc06b | 174 | cp = arg->mr_flattened; |
6ff26f0b | 175 | ip = (int *) cp; |
176 | /* we already got the overall length.. */ | |
177 | for(i=1; i <4; i++) ip[i] = ntohl(ip[i]); | |
8defc06b | 178 | arg->mr_version_no = ip[1]; |
179 | if (arg->mr_version_no != MR_VERSION_1 && | |
180 | arg->mr_version_no != MR_VERSION_2) | |
181 | arg->mr_status = MR_VERSION_MISMATCH; | |
182 | else arg->mr_status = ip[2]; | |
183 | arg->mr_argc = ip[3]; | |
6ff26f0b | 184 | cp += 4 * sizeof(int); |
8defc06b | 185 | arg->mr_argv=(char **)malloc(arg->mr_argc *sizeof(char **)); |
186 | arg->mr_argl=(int *)malloc(arg->mr_argc *sizeof(int *)); | |
6ff26f0b | 187 | |
8defc06b | 188 | for (i = 0; i<arg->mr_argc; ++i) { |
9f56cedf | 189 | u_short nlen = ntohl(* (int *) cp); |
6ff26f0b | 190 | cp += sizeof (long); |
8defc06b | 191 | if (cp + nlen > arg->mr_flattened + arg->mr_size) { |
192 | free(arg->mr_flattened); | |
193 | arg->mr_flattened = NULL; | |
9f56cedf | 194 | return OP_CANCELLED; |
195 | } | |
8defc06b | 196 | arg->mr_argv[i] = (char *)malloc(nlen); |
8fd777cf | 197 | memcpy(arg->mr_argv[i], cp, nlen); |
8defc06b | 198 | arg->mr_argl[i]=nlen; |
6ff26f0b | 199 | cp += sizeof(long) * howmany(nlen, sizeof(long)); |
200 | } | |
8defc06b | 201 | free(arg->mr_flattened); |
202 | arg->mr_flattened = NULL; | |
6ff26f0b | 203 | return OP_COMPLETE; |
204 | } | |
205 | } | |
206 | return OP_RUNNING; | |
207 | } | |
208 | ||
209 | ||
8defc06b | 210 | mr_start_recv(op, hcon, argp) |
6ff26f0b | 211 | OPERATION op; |
212 | HALF_CONNECTION hcon; | |
8defc06b | 213 | struct mr_params **argp; |
6ff26f0b | 214 | { |
8defc06b | 215 | register mr_params *arg = *argp; |
6ff26f0b | 216 | if (!arg) { |
8defc06b | 217 | *argp = arg = (mr_params *)malloc(sizeof(mr_params)); |
218 | arg->mr_argl = NULL; | |
219 | arg->mr_argv = NULL; | |
220 | arg->mr_flattened = NULL; | |
6ff26f0b | 221 | } |
8defc06b | 222 | arg->mr_state = S_RECV_START; |
223 | op->fcn.cont = mr_cont_recv; | |
224 | return mr_cont_recv(op, hcon, argp); | |
6ff26f0b | 225 | } |
226 | ||
8defc06b | 227 | mr_destroy_reply(reply) |
228 | mr_params *reply; | |
6ff26f0b | 229 | { |
230 | int i; | |
231 | if (reply) { | |
8defc06b | 232 | if (reply->mr_argl) |
233 | free(reply->mr_argl); | |
234 | reply->mr_argl = NULL; | |
235 | if (reply->mr_flattened) | |
236 | free(reply->mr_flattened); | |
237 | reply->mr_flattened = NULL; | |
238 | if (reply->mr_argv) { | |
239 | for (i=0; i<reply->mr_argc; i++) { | |
240 | if (reply->mr_argv[i]) | |
241 | free (reply->mr_argv[i]); | |
242 | reply->mr_argv[i] = NULL; | |
6ff26f0b | 243 | } |
8defc06b | 244 | free(reply->mr_argv); |
6ff26f0b | 245 | } |
8defc06b | 246 | reply->mr_argv = NULL; |
6ff26f0b | 247 | free(reply); |
248 | } | |
249 | } |