2005-11-18 18:54:58 +03:00
|
|
|
/*
|
2006-01-30 21:08:58 +03:00
|
|
|
*(C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
|
|
|
*See LICENSE file for license details.
|
2005-11-18 18:54:58 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2006-04-03 17:52:05 +04:00
|
|
|
#include <cext.h>
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2006-06-07 21:39:30 +04:00
|
|
|
#define IXP_VERSION "9P2000"
|
2006-04-06 09:55:33 +04:00
|
|
|
#define IXP_NOTAG (unsigned short)~0U /*Dummy tag */
|
|
|
|
#define IXP_NOFID (unsigned int)~0 /*No auth */
|
|
|
|
|
|
|
|
enum { IXP_MAX_VERSION = 32 };
|
|
|
|
enum { IXP_MAX_ERROR = 128 };
|
|
|
|
enum { IXP_MAX_CACHE = 32 };
|
|
|
|
enum { IXP_MAX_MSG = 8192 };
|
|
|
|
enum { IXP_MAX_FLEN = 128 };
|
|
|
|
enum { IXP_MAX_ULEN = 32 };
|
|
|
|
enum { IXP_MAX_WELEM = 16 };
|
2006-01-30 21:08:58 +03:00
|
|
|
|
2006-04-03 17:52:05 +04:00
|
|
|
/* 9P message types */
|
2005-11-18 18:54:58 +03:00
|
|
|
enum {
|
2006-03-23 14:53:41 +03:00
|
|
|
TVERSION = 100,
|
|
|
|
RVERSION,
|
|
|
|
TAUTH = 102,
|
|
|
|
RAUTH,
|
|
|
|
TATTACH = 104,
|
|
|
|
RATTACH,
|
|
|
|
TERROR = 106,
|
|
|
|
RERROR,
|
|
|
|
TFLUSH = 108,
|
|
|
|
RFLUSH,
|
|
|
|
TWALK = 110,
|
|
|
|
RWALK,
|
|
|
|
TOPEN = 112,
|
|
|
|
ROPEN,
|
|
|
|
TCREATE = 114,
|
|
|
|
RCREATE,
|
|
|
|
TREAD = 116,
|
|
|
|
RREAD,
|
|
|
|
TWRITE = 118,
|
|
|
|
RWRITE,
|
|
|
|
TCLUNK = 120,
|
|
|
|
RCLUNK,
|
|
|
|
TREMOVE = 122,
|
|
|
|
RREMOVE,
|
|
|
|
TSTAT = 124,
|
|
|
|
RSTAT,
|
|
|
|
TWSTAT = 126,
|
|
|
|
RWSTAT,
|
2005-11-18 18:54:58 +03:00
|
|
|
};
|
|
|
|
|
2006-03-10 18:49:13 +03:00
|
|
|
/* borrowed from libc.h of Plan 9 */
|
2006-04-06 09:55:33 +04:00
|
|
|
enum {
|
|
|
|
IXP_DMDIR = 0x80000000, /* mode bit for directories */
|
|
|
|
IXP_DMAPPEND = 0x40000000, /* mode bit for append only files */
|
|
|
|
IXP_DMEXCL = 0x20000000, /* mode bit for exclusive use files */
|
|
|
|
IXP_DMMOUNT = 0x10000000, /* mode bit for mounted channel */
|
|
|
|
IXP_DMAUTH = 0x08000000, /* mode bit for authentication file */
|
|
|
|
IXP_DMTMP = 0x04000000, /* mode bit for non-backed-up file */
|
|
|
|
IXP_DMREAD = 0x4<<6, /* mode bit for read permission */
|
|
|
|
IXP_DMWRITE = 0x2<<6, /* mode bit for write permission */
|
2006-06-08 12:54:19 +04:00
|
|
|
IXP_DMEXEC = 0x1<<6 /* mode bit for execute permission */
|
2006-04-06 09:55:33 +04:00
|
|
|
};
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2006-03-10 18:49:13 +03:00
|
|
|
/* modes */
|
2006-01-30 21:08:58 +03:00
|
|
|
enum {
|
2006-03-23 14:53:41 +03:00
|
|
|
IXP_OREAD = 0x00,
|
|
|
|
IXP_OWRITE = 0x01,
|
|
|
|
IXP_ORDWR = 0x02,
|
|
|
|
IXP_OEXEC = 0x03,
|
|
|
|
IXP_OEXCL = 0x04,
|
|
|
|
IXP_OTRUNC = 0x10,
|
|
|
|
IXP_OREXEC = 0x20,
|
|
|
|
IXP_ORCLOSE = 0x40,
|
|
|
|
IXP_OAPPEND = 0x80,
|
2005-11-18 18:54:58 +03:00
|
|
|
};
|
|
|
|
|
2006-03-10 18:49:13 +03:00
|
|
|
/* qid.types */
|
2006-01-30 21:08:58 +03:00
|
|
|
enum {
|
2006-03-23 14:53:41 +03:00
|
|
|
IXP_QTDIR = 0x80,
|
|
|
|
IXP_QTAPPEND = 0x40,
|
|
|
|
IXP_QTEXCL = 0x20,
|
|
|
|
IXP_QTMOUNT = 0x10,
|
|
|
|
IXP_QTAUTH = 0x08,
|
|
|
|
IXP_QTTMP = 0x04,
|
|
|
|
IXP_QTSYMLINK = 0x02,
|
|
|
|
IXP_QTLINK = 0x01,
|
|
|
|
IXP_QTFILE = 0x00,
|
2005-11-18 18:54:58 +03:00
|
|
|
};
|
|
|
|
|
2006-02-01 16:25:02 +03:00
|
|
|
typedef struct Qid Qid;
|
|
|
|
struct Qid {
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned char type;
|
|
|
|
unsigned int version;
|
|
|
|
unsigned long long path;
|
2006-02-01 16:25:02 +03:00
|
|
|
/* internal use only */
|
2006-02-10 17:59:30 +03:00
|
|
|
unsigned char dir_type;
|
2006-02-01 16:25:02 +03:00
|
|
|
};
|
2006-01-30 21:08:58 +03:00
|
|
|
|
2006-04-29 20:51:45 +04:00
|
|
|
/* stat structure */
|
2006-01-30 21:08:58 +03:00
|
|
|
typedef struct {
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned short type;
|
|
|
|
unsigned int dev;
|
|
|
|
Qid qid;
|
|
|
|
unsigned int mode;
|
|
|
|
unsigned int atime;
|
|
|
|
unsigned int mtime;
|
|
|
|
unsigned long long length;
|
|
|
|
char name[IXP_MAX_FLEN];
|
|
|
|
char uid[IXP_MAX_ULEN];
|
|
|
|
char gid[IXP_MAX_ULEN];
|
|
|
|
char muid[IXP_MAX_ULEN];
|
2006-01-30 21:08:58 +03:00
|
|
|
} Stat;
|
|
|
|
|
|
|
|
typedef struct {
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned char id;
|
|
|
|
unsigned short tag;
|
|
|
|
unsigned int fid;
|
2006-04-30 00:21:10 +04:00
|
|
|
unsigned int maxmsg; /* Tversion, Rversion */
|
|
|
|
char version[IXP_MAX_VERSION]; /* Tversion, Rversion */
|
|
|
|
unsigned short oldtag; /* Tflush */
|
|
|
|
char errstr[IXP_MAX_ERROR]; /* Rerror */
|
|
|
|
Qid qid; /* Rattach, Ropen, Rcreate */
|
|
|
|
unsigned int iounit; /* Ropen, Rcreate */
|
|
|
|
Qid aqid; /* Rauth */
|
|
|
|
unsigned int afid; /* Tauth, Tattach */
|
|
|
|
char uname[IXP_MAX_ULEN]; /* Tauth, Tattach */
|
|
|
|
char aname[IXP_MAX_FLEN]; /* Tauth, Tattach */
|
|
|
|
unsigned int perm; /* Tcreate */
|
|
|
|
char name[IXP_MAX_FLEN]; /* Tcreate */
|
|
|
|
unsigned char mode; /* Tcreate, Topen */
|
|
|
|
unsigned int newfid; /* Twalk */
|
|
|
|
unsigned short nwname; /* Twalk */
|
2006-06-15 03:26:35 +04:00
|
|
|
char *wname[IXP_MAX_WELEM];/* Twalk */
|
2006-04-30 00:21:10 +04:00
|
|
|
unsigned short nwqid; /* Rwalk */
|
|
|
|
Qid wqid[IXP_MAX_WELEM]; /* Rwalk */
|
|
|
|
unsigned long long offset; /* Tread, Twrite */
|
|
|
|
unsigned int count; /* Tread, Twrite, Rread */
|
|
|
|
Stat stat; /* Rstat */
|
|
|
|
unsigned short nstat; /* Twstat, Rstat */
|
|
|
|
unsigned char data[IXP_MAX_MSG]; /* Twrite, Rread, Twstat, Rstat */
|
2006-01-30 21:08:58 +03:00
|
|
|
} Fcall;
|
|
|
|
|
|
|
|
typedef struct IXPServer IXPServer;
|
|
|
|
typedef struct IXPConn IXPConn;
|
|
|
|
typedef struct IXPMap IXPMap;
|
2006-06-15 03:26:35 +04:00
|
|
|
typedef struct Intmap Intmap;
|
2006-01-30 21:08:58 +03:00
|
|
|
|
|
|
|
struct IXPMap {
|
2006-06-08 12:54:19 +04:00
|
|
|
IXPMap *next;
|
2006-01-30 21:08:58 +03:00
|
|
|
unsigned int fid;
|
2006-05-01 21:39:26 +04:00
|
|
|
unsigned short sel;
|
|
|
|
unsigned short nwqid;
|
|
|
|
Qid wqid[IXP_MAX_WELEM];
|
2005-11-18 18:54:58 +03:00
|
|
|
};
|
|
|
|
|
2006-06-15 03:26:35 +04:00
|
|
|
typedef struct Intlist Intlist;
|
|
|
|
struct Intlist
|
|
|
|
{
|
|
|
|
unsigned long id;
|
|
|
|
void* aux;
|
|
|
|
Intlist* link;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Intmap
|
|
|
|
{
|
|
|
|
unsigned long nhash;
|
|
|
|
Intlist **hash;
|
|
|
|
};
|
|
|
|
|
2006-01-30 21:08:58 +03:00
|
|
|
struct IXPConn {
|
2006-06-08 12:54:19 +04:00
|
|
|
IXPConn *next;
|
2006-03-23 14:53:41 +03:00
|
|
|
int fd;
|
2006-02-03 18:15:36 +03:00
|
|
|
IXPServer *srv;
|
2006-03-23 14:53:41 +03:00
|
|
|
void (*read) (IXPConn *);
|
|
|
|
void (*close) (IXPConn *);
|
2006-06-08 12:54:19 +04:00
|
|
|
IXPMap *map;
|
2006-02-12 02:49:38 +03:00
|
|
|
Fcall pending;
|
|
|
|
int is_pending;
|
2005-11-18 18:54:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IXPServer {
|
2006-03-23 14:53:41 +03:00
|
|
|
int running;
|
2006-06-08 12:54:19 +04:00
|
|
|
IXPConn *conn;
|
2006-03-23 14:53:41 +03:00
|
|
|
int maxfd;
|
|
|
|
fd_set rd;
|
2005-11-18 18:54:58 +03:00
|
|
|
};
|
|
|
|
|
2006-01-30 21:08:58 +03:00
|
|
|
typedef struct {
|
2006-03-23 14:53:41 +03:00
|
|
|
int fd;
|
|
|
|
unsigned int root_fid;
|
|
|
|
Qid root_qid;
|
|
|
|
Fcall fcall;
|
|
|
|
char *errstr;
|
2006-01-30 21:08:58 +03:00
|
|
|
} IXPClient;
|
|
|
|
|
|
|
|
/* client.c */
|
2006-02-12 03:49:33 +03:00
|
|
|
int ixp_client_dial(IXPClient *c, char *address, unsigned int rootfid);
|
|
|
|
void ixp_client_hangup(IXPClient *c);
|
2006-01-30 21:08:58 +03:00
|
|
|
int ixp_client_remove(IXPClient *c, unsigned int newfid, char *filepath);
|
|
|
|
int ixp_client_create(IXPClient *c, unsigned int dirfid, char *name,
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned int perm, unsigned char mode);
|
2006-01-30 21:08:58 +03:00
|
|
|
int ixp_client_walk(IXPClient *c, unsigned int newfid, char *filepath);
|
2006-05-23 10:17:42 +04:00
|
|
|
int ixp_client_stat(IXPClient *c, unsigned int newfid, char *filepath);
|
|
|
|
int ixp_client_walkopen(IXPClient *c, unsigned int newfid, char *filepath,
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned char mode);
|
2006-05-23 10:17:42 +04:00
|
|
|
int ixp_client_open(IXPClient *c, unsigned int newfid, unsigned char mode);
|
2006-01-30 21:08:58 +03:00
|
|
|
int ixp_client_read(IXPClient *c, unsigned int fid,
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned long long offset, void *result,
|
|
|
|
unsigned int res_len);
|
2006-01-30 21:08:58 +03:00
|
|
|
int ixp_client_write(IXPClient *c, unsigned int fid,
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned long long offset,
|
|
|
|
unsigned int count, unsigned char *data);
|
2006-01-30 21:08:58 +03:00
|
|
|
int ixp_client_close(IXPClient *c, unsigned int fid);
|
2006-02-07 02:32:57 +03:00
|
|
|
int ixp_client_do_fcall(IXPClient * c);
|
2006-01-30 21:08:58 +03:00
|
|
|
|
|
|
|
/* convert.c */
|
2006-06-10 10:18:32 +04:00
|
|
|
void ixp_pack_u8(unsigned char **msg, int *msize, unsigned char val);
|
2006-06-12 15:34:59 +04:00
|
|
|
void ixp_unpack_u8(unsigned char **msg, unsigned char *val);
|
2006-06-10 10:18:32 +04:00
|
|
|
void ixp_pack_u16(unsigned char **msg, int *msize, unsigned short val);
|
2006-06-12 15:34:59 +04:00
|
|
|
void ixp_unpack_u16(unsigned char **msg, unsigned short *val);
|
2006-06-10 10:18:32 +04:00
|
|
|
void ixp_pack_u32(unsigned char **msg, int *msize, unsigned int val);
|
2006-06-12 15:34:59 +04:00
|
|
|
void ixp_unpack_u32(unsigned char **msg, unsigned int *val);
|
2006-06-10 10:18:32 +04:00
|
|
|
void ixp_pack_u64(unsigned char **msg, int *msize, unsigned long long val);
|
2006-06-12 15:34:59 +04:00
|
|
|
void ixp_unpack_u64(unsigned char **msg, unsigned long long *val);
|
2006-06-10 10:18:32 +04:00
|
|
|
void ixp_pack_string(unsigned char **msg, int *msize, const char *s);
|
2006-06-15 03:26:35 +04:00
|
|
|
void ixp_unpack_strings(unsigned char **msg, unsigned short n, char **strings);
|
2006-06-12 15:34:59 +04:00
|
|
|
void ixp_unpack_string(unsigned char **msg, char *string,
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned short stringlen, unsigned short *len);
|
2006-06-10 10:18:32 +04:00
|
|
|
void ixp_pack_data(unsigned char **msg, int *msize, unsigned char *data,
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned int datalen);
|
2006-06-12 15:34:59 +04:00
|
|
|
void ixp_unpack_data(unsigned char **msg, unsigned char *data,
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned int datalen);
|
2006-06-10 10:18:32 +04:00
|
|
|
void ixp_pack_prefix(unsigned char *msg, unsigned int size,
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned char id, unsigned short tag);
|
2006-06-12 15:34:59 +04:00
|
|
|
void ixp_unpack_prefix(unsigned char **msg, unsigned int *size,
|
2006-03-23 14:53:41 +03:00
|
|
|
unsigned char *id, unsigned short *tag);
|
2006-06-10 10:18:32 +04:00
|
|
|
void ixp_pack_qid(unsigned char **msg, int *msize, Qid *qid);
|
2006-06-12 15:34:59 +04:00
|
|
|
void ixp_unpack_qid(unsigned char **msg, Qid *qid);
|
2006-06-10 10:18:32 +04:00
|
|
|
void ixp_pack_stat(unsigned char **msg, int *msize, Stat *stat);
|
2006-06-12 15:34:59 +04:00
|
|
|
void ixp_unpack_stat(unsigned char **msg, Stat *stat);
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2006-06-15 03:26:35 +04:00
|
|
|
/* intmap.c */
|
|
|
|
void initmap(Intmap *m, unsigned long nhash, void *hash);
|
|
|
|
void freemap(Intmap *map, void (*destroy)(void*));
|
|
|
|
void* lookupkey(Intmap *map, unsigned long id);
|
|
|
|
void* insertkey(Intmap *map, unsigned long id, void *v);
|
|
|
|
int caninsertkey(Intmap *map, unsigned long id, void *v);
|
|
|
|
void* deletekey(Intmap *map, unsigned long id);
|
|
|
|
|
2005-11-18 18:54:58 +03:00
|
|
|
/* message.c */
|
2006-01-30 21:08:58 +03:00
|
|
|
unsigned short ixp_sizeof_stat(Stat *stat);
|
2006-03-01 09:51:04 +03:00
|
|
|
unsigned int ixp_fcall2msg(void *msg, Fcall *fcall, unsigned int msglen);
|
|
|
|
unsigned int ixp_msg2fcall(Fcall *call, void *msg, unsigned int msglen);
|
2006-01-30 21:08:58 +03:00
|
|
|
|
|
|
|
/* server.c */
|
2006-02-03 18:15:36 +03:00
|
|
|
IXPConn *ixp_server_open_conn(IXPServer *s, int fd,
|
|
|
|
void (*read)(IXPConn *c), void (*close)(IXPConn *c));
|
|
|
|
void ixp_server_close_conn(IXPConn *c);
|
2006-01-30 21:08:58 +03:00
|
|
|
char *ixp_server_loop(IXPServer *s);
|
|
|
|
IXPMap *ixp_server_fid2map(IXPConn *c, unsigned int fid);
|
2006-02-03 18:15:36 +03:00
|
|
|
unsigned int ixp_server_receive_fcall(IXPConn *c, Fcall *fcall);
|
|
|
|
int ixp_server_respond_fcall(IXPConn *c, Fcall *fcall);
|
|
|
|
int ixp_server_respond_error(IXPConn *c, Fcall *fcall, char *errstr);
|
2006-02-03 18:42:59 +03:00
|
|
|
void ixp_server_close(IXPServer *s);
|
2006-01-30 21:08:58 +03:00
|
|
|
|
|
|
|
/* socket.c */
|
|
|
|
int ixp_connect_sock(char *address);
|
|
|
|
int ixp_create_sock(char *address, char **errstr);
|
|
|
|
|
|
|
|
/* transport.c */
|
|
|
|
unsigned int ixp_send_message(int fd, void *msg, unsigned int msize, char **errstr);
|
|
|
|
unsigned int ixp_recv_message(int fd, void *msg, unsigned int msglen, char **errstr);
|