wmii/libixp/message.c

312 lines
7.6 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
2006-01-20 17:20:24 +03:00
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2005-11-18 18:54:58 +03:00
* See LICENSE file for license details.
*/
#include <stdio.h>
2005-11-18 18:54:58 +03:00
#include <stdlib.h>
#include <string.h>
#include "ixp.h"
#define IXP_QIDSZ (sizeof(unsigned char) + sizeof(unsigned int)\
2006-03-23 14:53:41 +03:00
+ sizeof(unsigned long long))
2005-11-18 18:54:58 +03:00
static unsigned short
sizeof_string(const char *s)
2005-11-18 18:54:58 +03:00
{
2006-03-23 14:53:41 +03:00
return sizeof(unsigned short) + strlen(s);
2005-11-18 18:54:58 +03:00
}
unsigned short
ixp_sizeof_stat(Stat * stat)
2005-11-18 18:54:58 +03:00
{
2006-03-23 14:53:41 +03:00
return IXP_QIDSZ
+ 2 * sizeof(unsigned short)
+ 4 * sizeof(unsigned int)
+ sizeof(unsigned long long)
+ sizeof_string(stat->name)
+ sizeof_string(stat->uid)
+ sizeof_string(stat->gid)
+ sizeof_string(stat->muid);
2005-11-18 18:54:58 +03:00
}
unsigned int
ixp_fcall2msg(void *msg, Fcall *fcall, unsigned int msglen)
2005-11-18 18:54:58 +03:00
{
2006-03-23 14:53:41 +03:00
unsigned int i, msize =
sizeof(unsigned char) + sizeof(unsigned short) +
sizeof(unsigned int);
void *p = msg;
2005-11-18 18:54:58 +03:00
2006-03-23 14:53:41 +03:00
switch (fcall->id) {
case TVERSION:
case RVERSION:
msize += sizeof(unsigned int) + sizeof_string(fcall->version);
break;
case TAUTH:
msize += sizeof(unsigned int) + sizeof_string(fcall->uname)
+ sizeof_string(fcall->aname);
break;
case RAUTH:
case RATTACH:
msize += IXP_QIDSZ;
break;
case TATTACH:
msize += 2 * sizeof(unsigned int) + sizeof_string(fcall->uname)
+ sizeof_string(fcall->aname);
break;
case RERROR:
msize += sizeof_string(fcall->errstr);
break;
case RWRITE:
case TCLUNK:
case TREMOVE:
case TSTAT:
msize += sizeof(unsigned int);
break;
case TWALK:
msize += sizeof(unsigned short) + 2 * sizeof(unsigned int);
for(i = 0; i < fcall->nwname; i++)
msize += sizeof_string(fcall->wname[i]);
break;
case TFLUSH:
msize += sizeof(unsigned short);
break;
case RWALK:
msize += sizeof(unsigned short) + fcall->nwqid * IXP_QIDSZ;
break;
case TOPEN:
msize += sizeof(unsigned int) + sizeof(unsigned char);
break;
case ROPEN:
case RCREATE:
msize += IXP_QIDSZ + sizeof(unsigned int);
break;
case TCREATE:
msize += sizeof(unsigned char) + 2 * sizeof(unsigned int)
+ sizeof_string(fcall->name);
break;
case TREAD:
msize += 2 * sizeof(unsigned int) + sizeof(unsigned long long);
break;
case RREAD:
msize += sizeof(unsigned int) + fcall->count;
break;
case TWRITE:
msize += 2 * sizeof(unsigned int) + sizeof(unsigned long long)
+ fcall->count;
break;
case RSTAT:
msize += sizeof(unsigned short) + ixp_sizeof_stat(&fcall->stat);
break;
case TWSTAT:
msize += sizeof(unsigned int) + sizeof(unsigned short)
+ ixp_sizeof_stat(&fcall->stat);
break;
default:
break;
}
2005-11-18 18:54:58 +03:00
2006-03-23 14:53:41 +03:00
if(msize > msglen)
return 0;
2006-05-01 21:39:26 +04:00
p = ixp_pack_prefix(p, msize, fcall->id, fcall->tag);
2005-11-18 18:54:58 +03:00
2006-03-23 14:53:41 +03:00
switch (fcall->id) {
case TVERSION:
case RVERSION:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->maxmsg);
p = ixp_pack_string(p, fcall->version);
2006-03-23 14:53:41 +03:00
break;
case TAUTH:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->afid);
p = ixp_pack_string(p, fcall->uname);
p = ixp_pack_string(p, fcall->aname);
2006-03-23 14:53:41 +03:00
break;
case RAUTH:
2006-05-01 21:39:26 +04:00
p = ixp_pack_qid(p, &fcall->aqid);
2006-03-23 14:53:41 +03:00
break;
case RATTACH:
2006-05-01 21:39:26 +04:00
p = ixp_pack_qid(p, &fcall->qid);
2006-03-23 14:53:41 +03:00
break;
case TATTACH:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->fid);
p = ixp_pack_u32(p, fcall->afid);
p = ixp_pack_string(p, fcall->uname);
p = ixp_pack_string(p, fcall->aname);
2006-03-23 14:53:41 +03:00
break;
case RERROR:
2006-05-01 21:39:26 +04:00
p = ixp_pack_string(p, fcall->errstr);
2006-03-23 14:53:41 +03:00
break;
case TFLUSH:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u16(p, fcall->oldtag);
2006-03-23 14:53:41 +03:00
break;
case TWALK:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->fid);
p = ixp_pack_u32(p, fcall->newfid);
p = ixp_pack_u16(p, fcall->nwname);
2006-03-23 14:53:41 +03:00
for(i = 0; i < fcall->nwname; i++)
2006-05-01 21:39:26 +04:00
p = ixp_pack_string(p, fcall->wname[i]);
2006-03-23 14:53:41 +03:00
break;
case RWALK:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u16(p, fcall->nwqid);
2006-03-23 14:53:41 +03:00
for(i = 0; i < fcall->nwqid; i++)
2006-05-01 21:39:26 +04:00
p = ixp_pack_qid(p, &fcall->wqid[i]);
2006-03-23 14:53:41 +03:00
break;
case TOPEN:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->fid);
p = ixp_pack_u8(p, fcall->mode);
2006-03-23 14:53:41 +03:00
break;
case ROPEN:
case RCREATE:
2006-05-01 21:39:26 +04:00
p = ixp_pack_qid(p, &fcall->qid);
p = ixp_pack_u32(p, fcall->iounit);
2006-03-23 14:53:41 +03:00
break;
case TCREATE:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->fid);
p = ixp_pack_string(p, fcall->name);
p = ixp_pack_u32(p, fcall->perm);
p = ixp_pack_u8(p, fcall->mode);
2006-03-23 14:53:41 +03:00
break;
case TREAD:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->fid);
p = ixp_pack_u64(p, fcall->offset);
p = ixp_pack_u32(p, fcall->count);
2006-03-23 14:53:41 +03:00
break;
case RREAD:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->count);
p = ixp_pack_data(p, fcall->data, fcall->count);
2006-03-23 14:53:41 +03:00
break;
case TWRITE:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->fid);
p = ixp_pack_u64(p, fcall->offset);
p = ixp_pack_u32(p, fcall->count);
p = ixp_pack_data(p, fcall->data, fcall->count);
2006-03-23 14:53:41 +03:00
break;
case RWRITE:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->count);
2006-03-23 14:53:41 +03:00
break;
case TCLUNK:
case TREMOVE:
case TSTAT:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->fid);
2006-03-23 14:53:41 +03:00
break;
case RSTAT:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u16(p, ixp_sizeof_stat(&fcall->stat));
p = ixp_pack_stat(p, &fcall->stat);
2006-03-23 14:53:41 +03:00
break;
case TWSTAT:
2006-05-01 21:39:26 +04:00
p = ixp_pack_u32(p, fcall->fid);
p = ixp_pack_u16(p, ixp_sizeof_stat(&fcall->stat));
p = ixp_pack_stat(p, &fcall->stat);
2006-03-23 14:53:41 +03:00
break;
}
2005-11-18 18:54:58 +03:00
if(msg + msize == p)
2006-03-23 14:53:41 +03:00
return msize;
return 0;
2005-11-18 18:54:58 +03:00
}
unsigned int
ixp_msg2fcall(Fcall *fcall, void *msg, unsigned int msglen)
2005-11-18 18:54:58 +03:00
{
2006-03-23 14:53:41 +03:00
unsigned int i, msize;
unsigned short len;
2006-05-01 21:39:26 +04:00
void *p = ixp_unpack_prefix(msg, &msize, &fcall->id, &fcall->tag);
2006-03-23 14:53:41 +03:00
if(msize > msglen) /* bad message */
return 0;
switch (fcall->id) {
case TVERSION:
case RVERSION:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->maxmsg);
p = ixp_unpack_string(p, fcall->version, sizeof(fcall->version), &len);
2006-03-23 14:53:41 +03:00
break;
case TAUTH:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->afid);
p = ixp_unpack_string(p, fcall->uname, sizeof(fcall->uname), &len);
p = ixp_unpack_string(p, fcall->aname, sizeof(fcall->aname), &len);
2006-03-23 14:53:41 +03:00
break;
case RAUTH:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_qid(p, &fcall->aqid);
2006-03-23 14:53:41 +03:00
break;
case RATTACH:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_qid(p, &fcall->qid);
2006-03-23 14:53:41 +03:00
break;
case TATTACH:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u32(p, &fcall->afid);
p = ixp_unpack_string(p, fcall->uname, sizeof(fcall->uname), &len);
p = ixp_unpack_string(p, fcall->aname, sizeof(fcall->aname), &len);
2006-03-23 14:53:41 +03:00
break;
case RERROR:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_string(p, fcall->errstr, sizeof(fcall->errstr), &len);
2006-03-23 14:53:41 +03:00
break;
case TFLUSH:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u16(p, &fcall->oldtag);
2006-03-23 14:53:41 +03:00
break;
case TWALK:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u32(p, &fcall->newfid);
p = ixp_unpack_u16(p, &fcall->nwname);
2006-03-23 14:53:41 +03:00
for(i = 0; i < fcall->nwname; i++)
2006-05-01 21:39:26 +04:00
p = ixp_unpack_string(p, fcall->wname[i], IXP_MAX_FLEN, &len);
2006-03-23 14:53:41 +03:00
break;
case RWALK:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u16(p, &fcall->nwqid);
2006-03-23 14:53:41 +03:00
for(i = 0; i < fcall->nwqid; i++)
2006-05-01 21:39:26 +04:00
p = ixp_unpack_qid(p, &fcall->wqid[i]);
2006-03-23 14:53:41 +03:00
break;
case TOPEN:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u8(p, &fcall->mode);
2006-03-23 14:53:41 +03:00
break;
case ROPEN:
case RCREATE:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_qid(p, &fcall->qid);
p = ixp_unpack_u32(p, &fcall->iounit);
2006-03-23 14:53:41 +03:00
break;
case TCREATE:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_string(p, fcall->name, sizeof(fcall->name), &len);
p = ixp_unpack_u32(p, &fcall->perm);
p = ixp_unpack_u8(p, &fcall->mode);
2006-03-23 14:53:41 +03:00
break;
case TREAD:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u64(p, &fcall->offset);
p = ixp_unpack_u32(p, &fcall->count);
2006-03-23 14:53:41 +03:00
break;
case RREAD:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->count);
p = ixp_unpack_data(p, fcall->data, fcall->count);
2006-03-23 14:53:41 +03:00
break;
case TWRITE:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u64(p, &fcall->offset);
p = ixp_unpack_u32(p, &fcall->count);
p = ixp_unpack_data(p, fcall->data, fcall->count);
2006-03-23 14:53:41 +03:00
break;
case RWRITE:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->count);
2006-03-23 14:53:41 +03:00
break;
case TCLUNK:
case TREMOVE:
case TSTAT:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->fid);
2006-03-23 14:53:41 +03:00
break;
case RSTAT:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u16(p, &len);
p = ixp_unpack_stat(p, &fcall->stat);
2006-03-23 14:53:41 +03:00
break;
case TWSTAT:
2006-05-01 21:39:26 +04:00
p = ixp_unpack_u32(p, &fcall->fid);
p = ixp_unpack_u16(p, &len);
p = ixp_unpack_stat(p, &fcall->stat);
2006-03-23 14:53:41 +03:00
break;
}
2005-11-18 18:54:58 +03:00
if(msg + msize == p)
2006-03-23 14:53:41 +03:00
return msize;
return 0;
2005-11-18 18:54:58 +03:00
}