1 /* Copyright ©2007 Kris Maglione <fbsdaemon@gmail.com>
2 * See LICENSE file for license details.
7 #include "mixp_local.h"
8 #include <9p-mixp/convert.h>
9 #include <9p-mixp/stat.h>
20 #define SString(s) (SWord + strlen(s))
22 SQid
= SByte
+ SDWord
+ SQWord
,
26 mixp_message(char *data
, size_t length
, unsigned int mode
) {
31 m
.end
= data
+ length
;
38 mixp_stat_free(MIXP_STAT
*s
) {
49 mixp_fcall_free(IxpFcall
*fcall
)
53 // fprintf(stderr,"mixp_fcall_free() got an NULL pointer\n");
60 MIXP_FREE(fcall
->Rstat
.stat
);
63 MIXP_FREE(fcall
->Rread
.data
);
66 MIXP_FREE(fcall
->Rversion
.version
);
69 MIXP_FREE(fcall
->Rerror
.ename
);
73 fprintf(stderr
,"WARN: incomplete mixp_fcall_free()\n");
79 mixp_stat_sizeof(MIXP_STAT
* stat
) {
80 return SWord
/* size */
84 + 3 * SDWord
/* mode, atime, mtime */
89 + SString(stat
->muid
);
92 const char* fcall_type2str(int type
)
96 case P9_TVersion
: return "[TVersion]";
97 case P9_RVersion
: return "[RVersion]";
98 case P9_TAuth
: return "[TAuth] ";
99 case P9_RAuth
: return "[RAuth] ";
100 case P9_TAttach
: return "[TAttach] ";
101 case P9_RAttach
: return "[RAttach] ";
102 case P9_TOpen
: return "[TOpen] ";
103 case P9_ROpen
: return "[ROpen] ";
104 case P9_TRead
: return "[TRead] ";
105 case P9_RRead
: return "[RRead] ";
106 case P9_TWrite
: return "[TWrite] ";
107 case P9_RWrite
: return "[RWrite] ";
108 case P9_TFlush
: return "[TFlush] ";
109 case P9_RError
: return "[RError] ";
110 case P9_TWalk
: return "[TWalk] ";
111 case P9_RWalk
: return "[RWalk] ";
112 case P9_TCreate
: return "[TCreate] ";
113 case P9_RCreate
: return "[RCreate] ";
114 case P9_TClunk
: return "[TClunk] ";
115 case P9_RClunk
: return "[RClunk] ";
116 case P9_TRemove
: return "[TRemove] ";
117 case P9_TStat
: return "[TStat] ";
118 case P9_RStat
: return "[RStat] ";
119 case P9_TWStat
: return "[TWStat] ";
120 default: return "(unknown) ";
124 #define _MSGDUMP(fmt...) \
127 fprintf(stderr,"%s %s %-5d tag=%-5d ", \
128 (msg->mode==MsgUnpack) ? ">>":"<<", \
129 fcall_type2str(fcall->type), \
130 (msg->end - msg->data), \
133 fprintf(stderr,fmt); \
134 fprintf(stderr,"\n"); \
138 ixp_pfcall(MIXP_MESSAGE
*msg
, IxpFcall
*fcall
)
140 mixp_pu8(msg
, &fcall
->type
);
141 mixp_pu16(msg
, &fcall
->tag
);
144 printf("ixp_pfcall() %s tag=%d type=%d %s\n",
145 ((msg->mode==MsgPack) ? "PACK" : ((msg->mode==MsgUnpack) ? "UNPACK" : "???")),
148 fcall_type2str(fcall->type));
150 switch (fcall
->type
) {
152 mixp_pu32(msg
, &fcall
->Tversion
.msize
);
153 mixp_pstring(msg
, &fcall
->Tversion
.version
);
154 _MSGDUMP("msize=%d version=\"%s\"", fcall
->Tversion
.msize
, fcall
->Tversion
.version
);
157 mixp_pu32(msg
, &fcall
->Rversion
.msize
);
158 mixp_pstring(msg
, &fcall
->Rversion
.version
);
159 _MSGDUMP("msize=%d version=\"%s\"", fcall
->Tversion
.msize
, fcall
->Tversion
.version
);
162 mixp_pu32(msg
, &fcall
->Tauth
.afid
);
163 mixp_pstring(msg
, &fcall
->Tauth
.uname
);
164 mixp_pstring(msg
, &fcall
->Tauth
.aname
);
165 _MSGDUMP("afid=%d uname=\"%s\" aname=\"%s\"", fcall
->Tauth
.afid
, fcall
->Tauth
.uname
, fcall
->Tauth
.aname
);
168 mixp_pqid(msg
, &fcall
->Rauth
.aqid
);
172 mixp_pqid(msg
, &fcall
->Rattach
.qid
);
176 mixp_pu32(msg
, &fcall
->fid
);
177 mixp_pu32(msg
, &fcall
->Tattach
.afid
);
178 mixp_pstring(msg
, &fcall
->Tattach
.uname
);
179 mixp_pstring(msg
, &fcall
->Tattach
.aname
);
180 _MSGDUMP("fid=%d afid=%d uname=\"%s\" aname=\"%s\"", fcall
->fid
, fcall
->Tattach
.afid
, fcall
->Tattach
.uname
, fcall
->Tattach
.aname
);
183 mixp_pstring(msg
, &fcall
->Rerror
.ename
);
184 _MSGDUMP("ename=\"%s\"", fcall
->Rerror
.ename
);
187 mixp_pu16(msg
, &fcall
->Tflush
.oldtag
);
188 _MSGDUMP("oldtag=%d", fcall
->Tflush
.oldtag
);
191 mixp_pu32(msg
, &fcall
->fid
);
192 mixp_pu32(msg
, &fcall
->Twalk
.newfid
);
193 mixp_pstrings(msg
, &fcall
->Twalk
.nwname
, fcall
->Twalk
.wname
);
195 char _dumpbuf
[65535] = {0};
197 for (_x
=0; _x
<fcall
->Twalk
.nwname
; _x
++)
199 if (_x
) strcat(_dumpbuf
,", ");
200 strcat(_dumpbuf
,"\"");
201 strcat(_dumpbuf
,fcall
->Twalk
.wname
[_x
]);
202 strcat(_dumpbuf
,"\"");
204 _MSGDUMP("fid=%d newfid=%d nwname[]={ %s }", fcall
->fid
, fcall
->Twalk
.newfid
, _dumpbuf
);
208 mixp_pqids(msg
, &fcall
->Rwalk
.nwqid
, fcall
->Rwalk
.wqid
);
209 _MSGDUMP("nwqid=%d", fcall
->Rwalk
.nwqid
);
212 mixp_pu32(msg
, &fcall
->fid
);
213 mixp_pu8(msg
, &fcall
->Topen
.mode
);
214 _MSGDUMP("fid=%d mode=%d", fcall
->fid
, fcall
->Topen
.mode
);
217 mixp_pqid(msg
, &fcall
->Ropen
.qid
);
218 mixp_pu32(msg
, &fcall
->Ropen
.iounit
);
219 _MSGDUMP("iounit=%d", fcall
->Ropen
.iounit
);
222 mixp_pqid(msg
, &fcall
->Rcreate
.qid
);
223 mixp_pu32(msg
, &fcall
->Rcreate
.iounit
);
224 _MSGDUMP("iounit=%d", fcall
->Rcreate
.iounit
);
227 mixp_pu32(msg
, &fcall
->fid
);
228 mixp_pstring(msg
, &fcall
->Tcreate
.name
);
229 mixp_pu32(msg
, &fcall
->Tcreate
.perm
);
230 mixp_pu8(msg
, &fcall
->Tcreate
.mode
);
231 _MSGDUMP("fid=%d name=\"%s\" perm=%d mode=%d", fcall
->fid
, fcall
->Tcreate
.name
, fcall
->Tcreate
.perm
, fcall
->Tcreate
.mode
);
234 mixp_pu32(msg
, &fcall
->fid
);
235 mixp_pu64(msg
, &fcall
->Tread
.offset
);
236 mixp_pu32(msg
, &fcall
->Tread
.count
);
237 _MSGDUMP("fid=%d offset=%llu count=%u", fcall
->fid
, fcall
->Tread
.offset
, fcall
->Tread
.count
);
240 mixp_pu32(msg
, &fcall
->Rread
.count
);
241 mixp_pdata(msg
, &fcall
->Rread
.data
, fcall
->Rread
.count
);
242 _MSGDUMP("count=%d", fcall
->Rread
.count
);
245 mixp_pu32(msg
, &fcall
->fid
);
246 mixp_pu64(msg
, &fcall
->Twrite
.offset
);
247 mixp_pu32(msg
, &fcall
->Twrite
.count
);
248 mixp_pdata(msg
, &fcall
->Twrite
.data
, fcall
->Twrite
.count
);
249 _MSGDUMP("fid=%u offset=%llu count=%u", fcall
->fid
, fcall
->Twrite
.offset
, fcall
->Twrite
.count
);
252 mixp_pu32(msg
, &fcall
->Rwrite
.count
);
253 _MSGDUMP("count=%d", fcall
->Rwrite
.count
);
256 mixp_pu32(msg
, &fcall
->fid
);
257 _MSGDUMP("fid=%d", fcall
->fid
);
260 mixp_pu32(msg
, &fcall
->fid
);
261 _MSGDUMP("fid=%d", fcall
->fid
);
264 mixp_pu32(msg
, &fcall
->fid
);
265 _MSGDUMP("fid=%d", fcall
->fid
);
268 mixp_pu32(msg
, &fcall
->fid
);
269 _MSGDUMP("fid=%d", fcall
->fid
);
272 mixp_pu16(msg
, &fcall
->Rstat
.nstat
);
273 mixp_pdata(msg
, (char**)&fcall
->Rstat
.stat
, fcall
->Rstat
.nstat
);
274 _MSGDUMP("nstat=%d", fcall
->Rstat
.nstat
);
277 mixp_pu32(msg
, &fcall
->fid
);
278 mixp_pu16(msg
, &fcall
->Twstat
.nstat
);
279 mixp_pdata(msg
, (char**)&fcall
->Twstat
.stat
, fcall
->Twstat
.nstat
);
280 _MSGDUMP("fid=%d nstat=%d", fcall
->fid
, fcall
->Twstat
.nstat
);
283 _MSGDUMP(" unhandled type %d", fcall
->type
);
288 ixp_fcall2msg(MIXP_MESSAGE
*msg
, IxpFcall
*fcall
)
292 msg
->end
= msg
->data
+ msg
->size
;
293 msg
->pos
= msg
->data
+ SDWord
;
295 ixp_pfcall(msg
, fcall
);
297 if(msg
->pos
> msg
->end
)
301 size
= msg
->end
- msg
->data
;
303 msg
->pos
= msg
->data
;
304 mixp_pu32(msg
, &size
);
306 msg
->pos
= msg
->data
;
311 ixp_msg2fcall(MIXP_MESSAGE
*msg
, IxpFcall
*fcall
)
313 msg
->pos
= msg
->data
+ SDWord
;
314 msg
->mode
= MsgUnpack
;
315 ixp_pfcall(msg
, fcall
);
317 if(msg
->pos
> msg
->end
)
320 return msg
->pos
- msg
->data
;