22 Maxpath = 2+2+Segsize-8,
25 typedef struct Tfile Tfile;
29 uchar addr[IPaddrlen];
36 uchar ipaddr[IPaddrlen];
42 tfileget(uchar *addr, char *path)
47 for(f = files; f; f = f->next){
48 if(memcmp(addr, f->addr, IPaddrlen) == 0 && strcmp(path, f->path) == 0){
53 f = emalloc9p(sizeof *f);
54 memset(f, 0, sizeof(*f));
55 ipmove(f->addr, addr);
56 strncpy(f->path, path, sizeof(f->path));
71 if(f==nil || decref(f))
77 for(pp = &files; *pp; pp = &(*pp)->next){
98 tfilestat(Req *r, char *path, vlong length)
100 memset(&r->d, 0, sizeof(r->d));
101 r->d.uid = estrdup9p("tftp");
102 r->d.gid = estrdup9p("tftp");
103 r->d.name = estrdup9p(basename(path));
104 r->d.atime = r->d.mtime = time0;
105 r->d.length = length;
106 r->d.qid.path = r->fid->qid.path;
107 if(r->fid->qid.path & Qdata){
111 r->d.qid.type = QTDIR;
112 r->d.mode = DMDIR|0555;
118 catch(void *, char *msg)
120 if(strstr(msg, "alarm"))
129 return 5000+(port++)%64;
133 filereq(uchar *buf, char *path)
138 hnputs(buf, Tftp_READ);
142 /* hack: remove the trailing dot */
149 memcpy(p, "octet", 6);
157 int fd, cfd, last, block, n, ndata;
158 char *err, addr[40], adir[40];
166 uchar buf[2+2+Segsize+1];
178 if((c = f->c) == nil)
181 threadsetname(f->path);
184 snprint(addr, sizeof(addr), "udp!*!%d", newport());
185 if((cfd = announce(addr, adir)) >= 0)
189 err = "announce: %r";
192 if(write(cfd, "headers", 7) < 0){
193 err = "write ctl: %r";
196 strcat(adir, "/data");
197 if((fd = open(adir, ORDWR)) < 0){
202 n = filereq(msg.buf, f->path);
203 ipmove(msg.raddr, f->addr);
204 hnputs(msg.rport, TftpPort);
205 if(write(fd, &msg, sizeof(Udphdr) + n) < 0){
206 err = "send read request: %r";
215 if((n = read(fd, &msg, sizeof(Udphdr) + sizeof(msg.buf)-1)) < 0){
216 err = "receive response: %r";
223 switch(nhgets(msg.buf)){
225 werrstr((char*)msg.buf+4);
232 block = nhgets(msg.buf+2);
234 data = erealloc9p(data, ndata + n);
235 memcpy(data + ndata, msg.buf+4, n);
238 rloop: /* hanlde read request while downloading */
239 if((r != nil) && (r->ifcall.type == Tread) && (r->ifcall.offset < ndata)){
240 readbuf(r, data, ndata);
244 if((r == nil) && (nbrecv(c, &r) == 1)){
255 hnputs(msg.buf, Tftp_ACK);
256 hnputs(msg.buf+2, block);
257 if(write(fd, &msg, sizeof(Udphdr) + 4) < 0){
258 err = "send acknowledge: %r";
273 while((r != nil) || (r = recvp(c))){
277 snprint(buf, sizeof(buf), err);
280 switch(r->ifcall.type){
282 readbuf(r, data, ndata);
286 tfilestat(r, f->path, ndata);
289 respond(r, "bug in fs");
304 if(r->ifcall.aname && r->ifcall.aname[0]){
305 uchar addr[IPaddrlen];
307 if(parseip(addr, r->ifcall.aname) < 0){
308 respond(r, "bad ip specified");
311 f = tfileget(addr, "/");
313 if(ipcmp(ipaddr, IPnoaddr) == 0){
314 respond(r, "no ipaddr specified");
317 f = tfileget(ipaddr, "/");
320 r->fid->qid.type = QTDIR;
321 r->fid->qid.path = f->id<<1;
322 r->fid->qid.vers = 0;
323 r->ofcall.qid = r->fid->qid;
328 fswalk1(Fid *fid, char *name, Qid *qid)
334 t = smprint("%s/%s", f->path, name);
335 f = tfileget(f->addr, cleanname(t));
337 tfileput(fid->aux); fid->aux = f;
338 fid->qid.type = QTDIR;
339 fid->qid.path = f->id<<1;
342 * a dot in the path means the path element is not
343 * a directory. to force download of files containing
344 * no dot, a trailing dot can be appended that will
345 * be stripped out in the tftp read request.
347 if(strchr(f->path, '.') != nil){
349 fid->qid.path |= Qdata;
358 fsclone(Fid *oldfid, Fid *newfid)
369 fsdestroyfid(Fid *fid)
380 m = r->ifcall.mode & 3;
381 if(m != OREAD && m != OEXEC){
382 respond(r, "permission denied");
395 f->c = chancreate(sizeof(r), 0);
396 proccreate(download, f, 16*1024);
404 if(r->fid->qid.path & Qdata){
414 if(r->fid->qid.path & Qdata){
417 tfilestat(r, ((Tfile*)r->fid->aux)->path, 0);
424 .destroyfid= fsdestroyfid,
435 fprint(2, "usage: tftpfs [-D] [-s srvname] [-m mtpt] [ipaddr]\n");
436 threadexitsall("usage");
440 threadmain(int argc, char **argv)
446 ipmove(ipaddr, IPnoaddr);
453 srvname = EARGF(usage());
456 mtpt = EARGF(usage());
466 if(parseip(ipaddr, *argv) < 0)
473 if(srvname==nil && mtpt==nil)
476 threadpostmountsrv(&fs, srvname, mtpt, MREPL|MCREATE);