]>
Commit | Line | Data |
---|---|---|
6ff26f0b | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
7 | * | |
8 | * $Log$ | |
3d074977 | 9 | * Revision 1.3 1988-04-19 12:45:20 mar |
10 | * new version number passing and checking | |
6ff26f0b | 11 | * |
3d074977 | 12 | * Revision 1.2 87/08/02 21:49:15 wesommer |
13 | * Clean up error handling, memory handling. | |
14 | * | |
9f56cedf | 15 | * Revision 1.1 87/06/16 17:48:21 wesommer |
16 | * Initial revision | |
17 | * | |
6ff26f0b | 18 | * Revision 1.4 87/06/04 01:32:18 wesommer |
19 | * Renamed gdb calls. | |
20 | * | |
21 | * Revision 1.3 87/06/01 03:33:54 wesommer | |
22 | * Added destroy_reply. | |
23 | * | |
24 | * Revision 1.2 87/05/31 22:03:37 wesommer | |
25 | * Fixed numerous bugs; still shaky. | |
26 | * | |
27 | */ | |
28 | ||
29 | #ifndef lint | |
30 | static char *rcsid_sms_param_c = "$Header$"; | |
31 | #endif lint | |
32 | ||
9f56cedf | 33 | #include <sys/types.h> |
34 | #include <netinet/in.h> | |
6ff26f0b | 35 | #include "sms_private.h" |
36 | ||
37 | /* | |
38 | * GDB operations to send and recieve RPC requests and replies. | |
39 | */ | |
40 | ||
41 | /* | |
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. | |
45 | */ | |
46 | ||
47 | sms_cont_send(op, hcon, arg) | |
48 | OPERATION op; | |
49 | HALF_CONNECTION hcon; | |
50 | struct sms_params *arg; | |
51 | { | |
52 | op->result = OP_SUCCESS; | |
53 | free(arg->sms_flattened); | |
9f56cedf | 54 | arg->sms_flattened = NULL; |
55 | ||
6ff26f0b | 56 | return OP_COMPLETE; |
57 | } | |
58 | ||
59 | sms_start_send(op, hcon, arg) | |
60 | OPERATION op; | |
61 | HALF_CONNECTION hcon; | |
62 | register struct sms_params *arg; | |
63 | { | |
64 | int i, len; | |
65 | unsigned int sms_size; | |
66 | int *argl; | |
67 | char *buf, *bp; | |
68 | ||
69 | /* | |
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? | |
74 | * | |
75 | * that would look a lot like the uio stuff in the kernel.. hmm. | |
76 | */ | |
77 | ||
78 | /* | |
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. | |
87 | */ | |
88 | ||
89 | sms_size = 4 * sizeof(long); | |
90 | ||
91 | argl = (int *)malloc((unsigned)(sizeof(int) * arg->sms_argc)); | |
92 | ||
93 | /* | |
94 | * For each argument, figure out how much space is needed. | |
95 | */ | |
96 | ||
97 | for (i = 0; i < arg->sms_argc; ++i) { | |
98 | if (arg->sms_argl) | |
99 | argl[i] = len = arg->sms_argl[i]; | |
100 | else | |
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)); | |
105 | } | |
106 | ||
107 | arg->sms_flattened = buf = malloc(sms_size); | |
108 | ||
109 | bzero(arg->sms_flattened, sms_size); | |
110 | ||
111 | arg->sms_size = sms_size; | |
112 | ||
113 | /* | |
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. | |
117 | */ | |
118 | ||
119 | ((long *)buf)[0] = htonl(sms_size); | |
3d074977 | 120 | ((long *)buf)[1] = htonl(arg->sms_version_no); |
6ff26f0b | 121 | ((long *)buf)[2] = htonl(arg->sms_procno); |
122 | ((long *)buf)[3] = htonl(arg->sms_argc); | |
123 | ||
124 | /* | |
125 | * bp is a pointer into the point in the buffer to put | |
126 | * the next argument. | |
127 | */ | |
128 | ||
129 | bp = (char *)(((long *)buf) + 4); | |
130 | ||
131 | for (i = 0; i<arg->sms_argc; ++i) { | |
132 | len = argl[i]; | |
133 | *((long *)bp) = htonl(len); | |
134 | bp += sizeof(long); | |
135 | bcopy(arg->sms_argv[i], bp, len); | |
136 | bp += sizeof(long) * howmany(len, sizeof(long)); | |
137 | } | |
138 | op->fcn.cont = sms_cont_send; | |
139 | arg->sms_size = sms_size; | |
140 | ||
141 | free(argl); | |
142 | ||
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; | |
146 | } | |
147 | ||
148 | sms_cont_recv(op, hcon, argp) | |
149 | OPERATION op; | |
150 | HALF_CONNECTION hcon; | |
151 | sms_params **argp; | |
152 | { | |
153 | int done = FALSE; | |
154 | char *cp; | |
155 | int *ip; | |
156 | int i; | |
157 | register sms_params *arg = *argp; | |
158 | ||
159 | while (!done) { | |
160 | switch (arg->sms_state) { | |
161 | case S_RECV_START: | |
162 | arg->sms_state = S_RECV_DATA; | |
163 | if (gdb_receive_data(hcon, (caddr_t)&arg->sms_size, | |
164 | sizeof(long)) == OP_COMPLETE) | |
165 | continue; | |
166 | done = TRUE; | |
167 | break; | |
168 | case S_RECV_DATA: | |
169 | fflush(stdout); | |
170 | /* Should validate that length is reasonable */ | |
171 | arg->sms_size = ntohl(arg->sms_size); | |
9f56cedf | 172 | if (arg->sms_size > 65536) { |
173 | return OP_CANCELLED; | |
174 | } | |
6ff26f0b | 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)); | |
178 | ||
179 | if (gdb_receive_data(hcon, | |
180 | arg->sms_flattened + sizeof(long), | |
181 | arg->sms_size - sizeof(long)) | |
182 | == OP_COMPLETE) | |
183 | continue; | |
184 | done = TRUE; | |
185 | break; | |
186 | case S_DECODE_DATA: | |
187 | cp = arg->sms_flattened; | |
188 | ip = (int *) cp; | |
189 | /* we already got the overall length.. */ | |
190 | for(i=1; i <4; i++) ip[i] = ntohl(ip[i]); | |
3d074977 | 191 | arg->sms_version_no = ip[1]; |
192 | if (arg->sms_version_no != SMS_VERSION_1 && | |
193 | arg->sms_version_no != SMS_VERSION_2) | |
6ff26f0b | 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 *)); | |
200 | ||
201 | for (i = 0; i<arg->sms_argc; ++i) { | |
9f56cedf | 202 | u_short nlen = ntohl(* (int *) cp); |
6ff26f0b | 203 | cp += sizeof (long); |
9f56cedf | 204 | if (cp + nlen > arg->sms_flattened + arg->sms_size) { |
205 | free(arg->sms_flattened); | |
206 | arg->sms_flattened = NULL; | |
207 | return OP_CANCELLED; | |
208 | } | |
6ff26f0b | 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)); | |
213 | } | |
214 | free(arg->sms_flattened); | |
215 | arg->sms_flattened = NULL; | |
216 | return OP_COMPLETE; | |
217 | } | |
218 | } | |
219 | return OP_RUNNING; | |
220 | } | |
221 | ||
222 | ||
223 | sms_start_recv(op, hcon, argp) | |
224 | OPERATION op; | |
225 | HALF_CONNECTION hcon; | |
226 | struct sms_params **argp; | |
227 | { | |
228 | register sms_params *arg = *argp; | |
229 | if (!arg) { | |
230 | *argp = arg = (sms_params *)malloc(sizeof(sms_params)); | |
231 | arg->sms_argl = NULL; | |
232 | arg->sms_argv = NULL; | |
233 | arg->sms_flattened = NULL; | |
234 | } | |
235 | arg->sms_state = S_RECV_START; | |
236 | op->fcn.cont = sms_cont_recv; | |
237 | return sms_cont_recv(op, hcon, argp); | |
238 | } | |
239 | ||
240 | sms_destroy_reply(reply) | |
241 | sms_params *reply; | |
242 | { | |
243 | int i; | |
244 | if (reply) { | |
245 | if (reply->sms_argl) | |
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; | |
256 | } | |
257 | free(reply->sms_argv); | |
258 | } | |
259 | reply->sms_argv = NULL; | |
260 | free(reply); | |
261 | } | |
262 | } | |
263 | ||
264 | /* | |
265 | * Local Variables: | |
266 | * mode: c | |
267 | * c-indent-level: 4 | |
268 | * c-continued-statement-offset: 4 | |
269 | * c-brace-offset: -4 | |
270 | * c-argdecl-indent: 4 | |
271 | * c-label-offset: -4 | |
272 | * End: | |
273 | */ |