fcall 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. .TH FCALL 2
  2. .SH NAME
  3. Fcall, convS2M, convD2M, convM2S, convM2D, fcallfmt, dirfmt, dirmodefmt, read9pmsg, statcheck, sizeS2M, sizeD2M \- interface to Plan 9 File protocol
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .br
  9. .br
  10. .B #include <fcall.h>
  11. .PP
  12. .B
  13. uint convS2M(Fcall *f, uchar *ap, uint nap)
  14. .PP
  15. .B
  16. uint convD2M(Dir *d, uchar *ap, uint nap)
  17. .PP
  18. .B
  19. uint convM2S(uchar *ap, uint nap, Fcall *f)
  20. .PP
  21. .B
  22. uint convM2D(uchar *ap, uint nap, Dir *d, char *strs)
  23. .PP
  24. .B
  25. int dirfmt(Fmt*)
  26. .PP
  27. .B
  28. int fcallfmt(Fmt*)
  29. .PP
  30. .B
  31. int dirmodefmt(Fmt*)
  32. .PP
  33. .B
  34. int read9pmsg(int fd, uchar *buf, uint nbuf)
  35. .PP
  36. .B
  37. int statcheck(uchar *buf, uint nbuf)
  38. .PP
  39. .B
  40. uint sizeS2M(Fcall *f)
  41. .PP
  42. .B
  43. uint sizeD2M(Dir *d)
  44. .SH DESCRIPTION
  45. These
  46. routines convert messages in the machine-independent format of
  47. the Plan 9 file protocol,
  48. 9P, to and from a more convenient form,
  49. an
  50. .B Fcall
  51. structure:
  52. .PP
  53. .EX
  54. .if n .ta 4n +6n +5n +6n +18n +4n
  55. .if t .ta \w'xxxx'u +\w'short 'u +\w'xxxx'u +\w'ushort 'u +\w'ticket[TICKETLEN]; 'u +\w'/* 'u
  56. #define MAXWELEM 16
  57. typedef
  58. struct Fcall
  59. {
  60. uchar type;
  61. u32int fid;
  62. ushort tag;
  63. union {
  64. struct {
  65. u32int msize; /* Tversion, Rversion */
  66. char *version; /* Tversion, Rversion */
  67. };
  68. struct {
  69. u32int oldtag; /* Tflush */
  70. };
  71. struct {
  72. char *ename; /* Rerror */
  73. };
  74. struct {
  75. Qid qid; /* Rattach, Ropen, Rcreate */
  76. u32int iounit; /* Ropen, Rcreate */
  77. ushort nrauth; /* Rattach */
  78. uchar *rauth; /* Rattach */
  79. };
  80. struct {
  81. char *uname; /* Tattach */
  82. char *aname; /* Tattach */
  83. ushort nauth; /* Tattach */
  84. uchar *auth; /* Tattach */
  85. };
  86. struct {
  87. char *authid; /* Rsession */
  88. char *authdom; /* Rsession */
  89. ushort nchal; /* Tsession/Rsession */
  90. uchar *chal; /* Tsession/Rsession */
  91. };
  92. struct {
  93. u32int perm; /* Tcreate */
  94. char *name; /* Tcreate */
  95. uchar mode; /* Tcreate, Topen */
  96. };
  97. struct {
  98. u32int newfid; /* Twalk */
  99. ushort nwname; /* Twalk */
  100. char *wname[MAXWELEM]; /* Twalk */
  101. };
  102. struct {
  103. ushort nwqid; /* Rwalk */
  104. Qid wqid[MAXWELEM]; /* Rwalk */
  105. };
  106. struct {
  107. vlong offset; /* Tread, Twrite */
  108. u32int count; /* Tread, Twrite, Rread */
  109. char *data; /* Twrite, Rread */
  110. };
  111. struct {
  112. ushort nstat; /* Twstat, Rstat */
  113. uchar *stat; /* Twstat, Rstat */
  114. };
  115. };
  116. } Fcall;
  117. .EE
  118. .EX
  119. /* these are implemented as macros */
  120. uchar GBIT8(uchar*)
  121. ushort GBIT16(uchar*)
  122. ulong GBIT32(uchar*)
  123. vlong GBIT64(uchar*)
  124. void PBIT8(uchar*, uchar)
  125. void PBIT16(uchar*, ushort)
  126. void PBIT32(uchar*, ulong)
  127. void PBIT64(uchar*, vlong)
  128. #define BIT8SZ 1
  129. #define BIT16SZ 2
  130. #define BIT32SZ 4
  131. #define BIT64SZ 8
  132. .EE
  133. .PP
  134. This structure is defined in
  135. .BR <fcall.h> .
  136. See section 5
  137. for a full description of 9P messages and their encoding.
  138. For all message types, the
  139. .B type
  140. field of an
  141. .B Fcall
  142. holds one of
  143. .BR Tnop ,
  144. .BR Rnop ,
  145. .BR Tsession ,
  146. .BR Rsession ,
  147. etc. (defined in an enumerated type in
  148. .BR <fcall.h> ).
  149. .B Fid
  150. is used by most messages, and
  151. .B tag
  152. is used by all messages.
  153. The other fields are used selectively by the message types
  154. given in comments.
  155. .PP
  156. .I ConvM2S
  157. takes a 9P message at
  158. .I ap
  159. of length
  160. .IR nap ,
  161. and uses it to fill in
  162. .B Fcall
  163. structure
  164. .IR f .
  165. If the passed message
  166. including any data for
  167. .B Twrite
  168. and
  169. .B Rread
  170. messages
  171. is formatted properly,
  172. the return value is the number of bytes the message occupied in the buffer
  173. .IR ap ,
  174. which will always be less than or equal to
  175. .IR nap ;
  176. otherwise it is 0.
  177. For
  178. .B Twrite
  179. and
  180. .B Tread
  181. messages,
  182. .B data
  183. is set to a pointer into the argument message,
  184. not a copy.
  185. .PP
  186. .I ConvS2M
  187. does the reverse conversion, turning
  188. .I f
  189. into a message starting at
  190. .IR ap .
  191. The length of the resulting message is returned.
  192. For
  193. .B Twrite
  194. and
  195. .B Rread
  196. messages,
  197. .B count
  198. bytes starting at
  199. .B data
  200. are copied into the message.
  201. .PP
  202. The constant
  203. .B IOHDRSZ
  204. is a suitable amount of buffer to reserve for storing
  205. the 9P header;
  206. the data portion of a
  207. .B Twrite
  208. or
  209. .B Rread
  210. will be no more than the buffer size negotiated in the
  211. .BR Tversion/Rversion
  212. exchange, minus
  213. .BR IOHDRSZ .
  214. .PP
  215. The routine
  216. .I sizeS2M
  217. returns the number of bytes required to store the machine-independent representation of the
  218. .B Fcall
  219. structure
  220. .IR f ,
  221. including its initial 32-bit size field.
  222. In other words, it reports the number of bytes produced
  223. by a successful call to
  224. .IR convS2M .
  225. .PP
  226. Another structure is
  227. .BR Dir ,
  228. used by the routines described in
  229. .IR stat (2).
  230. .I ConvM2D
  231. converts the machine-independent form starting at
  232. .I ap
  233. into
  234. .IR d
  235. and returns the length of the machine-independent encoding.
  236. The strings in the returned
  237. .B Dir
  238. structure are stored at successive locations starting at
  239. .BR strs .
  240. Usually
  241. .B strs
  242. will point to storage immediately after the
  243. .B Dir
  244. itself.
  245. It can also be a
  246. .B nil
  247. pointer, in which case the string pointers in the returned
  248. .B Dir
  249. are all
  250. .BR nil ;
  251. however, the return value still includes their length.
  252. .PP
  253. .I ConvD2M
  254. does the reverse translation,
  255. also returning the length of the encoding.
  256. If the buffer is too short, the return value will be
  257. .B BIT16SZ
  258. and the correct size will be returned in the first
  259. .B BIT16SZ
  260. bytes.
  261. (If the buffer is less that
  262. .BR BIT16SZ ,
  263. the return value is zero; therefore a correct test for
  264. complete packing of the message is that the return value is
  265. greater than
  266. .BR BIT16SZ ).
  267. The macro
  268. .B GBIT16
  269. can be used to extract the correct value.
  270. The related macros with different sizes retrieve the corresponding-sized quantities.
  271. .B PBIT16
  272. and its brethren place values in messages.
  273. With the exception of handling short buffers in
  274. .IR convD2M ,
  275. these macros are not usually needed except by internal routines.
  276. .PP
  277. Analogous to
  278. .IR sizeS2M ,
  279. .I sizeD2M
  280. returns the number of bytes required to store the machine-independent representation of the
  281. .B Dir
  282. structure
  283. .IR d ,
  284. including its initial 16-bit size field.
  285. .PP
  286. The routine
  287. .B statcheck
  288. checks whether the
  289. .I nbuf
  290. bytes of
  291. .I buf
  292. contain a validly formatted machine-independent
  293. .B Dir
  294. entry suitable as an argument, for example, for the
  295. .B wstat
  296. (see
  297. .IR stat (2))
  298. system call.
  299. It checks that the sizes of all the elements of the the entry sum to exactly
  300. .IR nbuf ,
  301. which is a simple but effective test of validity.
  302. .I Nbuf
  303. and
  304. .I buf
  305. should include the second two-byte (16-bit) length field that precedes the entry when
  306. formatted in a 9P message (see
  307. .IR stat (5));
  308. in other words,
  309. .I nbuf
  310. is 2 plus the sum of the sizes of the entry itself.
  311. .I Statcheck
  312. also verifies that the length field has the correct value (that is,
  313. .IB nbuf -2\r1).
  314. It returns
  315. .B 0
  316. for a valid entry and
  317. .B -1
  318. for an incorrectly formatted entry.
  319. .PP
  320. .IR Dirfmt ,
  321. .IR fcallfmt ,
  322. and
  323. .I dirmodefmt
  324. are formatting routines, suitable for
  325. .IR fmtinstall (2).
  326. They convert
  327. .BR Dir* ,
  328. .BR Fcall* ,
  329. and
  330. .BR long
  331. values into string representations of the directory buffer,
  332. .B Fcall
  333. buffer,
  334. or file mode value.
  335. .I Fcallfmt
  336. assumes that
  337. .I dirfmt
  338. has been installed with format letter
  339. .L D
  340. and
  341. .I dirmodefmt
  342. with format letter
  343. .LR M .
  344. .PP
  345. .I Read9pmsg
  346. calls
  347. .IR read (2)
  348. multiple times, if necessary, to read an entire 9P message into
  349. .BR buf .
  350. The return value is 0 for end of file, or -1 for error; it does not return
  351. partial messages.
  352. .SH SOURCE
  353. .B /sys/src/libc/9sys
  354. .SH SEE ALSO
  355. .IR intro (2),
  356. .IR 9p (2),
  357. .IR stat (2),
  358. .IR intro (5)