comparison lib/fsplib/fsplib.h @ 662:be9663e0be00

2005-1-16 Brian Masney <masneyb@gftp.org> * lib/fsplib/COPYING lib/fsplib/Makefile.am lib/fsplib/fsplib.c lib/fsplib/fsplib.h lib/fsplib/lock.c lib/fsplib/lock.h - added FSPLIB This library is written by Radim Kolar <hsn@netmag.cz> and was included with his permission
author masneyb
date Sun, 16 Jan 2005 16:12:13 +0000
parents
children 6b6fbcf76d00
comparison
equal deleted inserted replaced
661:2e718fba351e 662:be9663e0be00
1 #ifndef _FSPLIB_H
2 #define _FSPLIB_H 1
3 #include <time.h>
4 /* The FSP v2 protocol support library - public interface */
5
6 /*
7 This file is part of fsplib - FSP protocol stack implemented in C
8 language. See http://fsp.sourceforge.net for more information.
9
10 Copyright (c) 2003-2004 by Radim HSN Kolar (hsn@netmag.cz)
11
12 You may copy or modify this file in any manner you wish, provided
13 that this notice is always included, and that you hold the author
14 harmless for any loss or damage resulting from the installation or
15 use of this software.
16
17 This is a free software. Be creative.
18 Let me know of any bugs and suggestions.
19 */
20
21 /* definition of FSP protocol v2 commands */
22
23 #define FSP_CC_VERSION 0x10 /* return server's version string. */
24 #define FSP_CC_INFO 0x11 /* return server's extended info block */
25 #define FSP_CC_ERR 0x40 /* error response from server. */
26 #define FSP_CC_GET_DIR 0x41 /* get a directory listing. */
27 #define FSP_CC_GET_FILE 0x42 /* get a file. */
28 #define FSP_CC_UP_LOAD 0x43 /* open a file for writing. */
29 #define FSP_CC_INSTALL 0x44 /* close a file opened for writing. */
30 #define FSP_CC_DEL_FILE 0x45 /* delete a file. */
31 #define FSP_CC_DEL_DIR 0x46 /* delete a directory. */
32 #define FSP_CC_GET_PRO 0x47 /* get directory protection. */
33 #define FSP_CC_SET_PRO 0x48 /* set directory protection. */
34 #define FSP_CC_MAKE_DIR 0x49 /* create a directory. */
35 #define FSP_CC_BYE 0x4A /* finish a session. */
36 #define FSP_CC_GRAB_FILE 0x4B /* atomic get+delete a file. */
37 #define FSP_CC_GRAB_DONE 0x4C /* atomic get+delete a file done. */
38 #define FSP_CC_STAT 0x4D /* get information about file. */
39 #define FSP_CC_RENAME 0x4E /* rename file or directory. */
40 #define FSP_CC_CH_PASSWD 0x4F /* change password */
41 #define FSP_CC_LIMIT 0x80 /* # > 0x7f for future cntrl blk ext. */
42 #define FSP_CC_TEST 0x81 /* reserved for testing */
43
44 /* FSP v2 packet size */
45 #define FSP_HSIZE 12 /* 12 bytes for v2 header */
46 #define FSP_SPACE 1024 /* maximum payload. */
47 #define FSP_MAXPACKET FSP_HSIZE+FSP_SPACE /* maximum packet size. */
48
49 /* byte offsets of fields in the FSP v2 header */
50 #define FSP_OFFSET_CMD 0
51 #define FSP_OFFSET_SUM 1
52 #define FSP_OFFSET_KEY 2
53 #define FSP_OFFSET_SEQ 4
54 #define FSP_OFFSET_LEN 6
55 #define FSP_OFFSET_POS 8
56
57 /* types of directory entry */
58 #define FSP_RDTYPE_END 0x00
59 #define FSP_RDTYPE_FILE 0x01
60 #define FSP_RDTYPE_DIR 0x02
61 #define FSP_RDTYPE_LINK 0x03
62 #define FSP_RDTYPE_SKIP 0x2A
63
64 /* definition of directory bitfield for directory information */
65 /* directory information is just going to be a bitfield encoding
66 * of which protection bits are set/unset
67 */
68
69 #define FSP_PRO_BYTES 1 /* currently only 8 bits or less of info */
70 #define FSP_DIR_OWNER 0x01 /* does caller own directory */
71 #define FSP_DIR_DEL 0x02 /* can files be deleted from this dir */
72 #define FSP_DIR_ADD 0x04 /* can files be added to this dir */
73 #define FSP_DIR_MKDIR 0x08 /* can new subdirectories be created */
74 #define FSP_DIR_GET 0x10 /* are files readable by non-owners? */
75 #define FSP_DIR_README 0x20 /* does this dir contain an readme file? */
76 #define FSP_DIR_LIST 0x40 /* public can list directory */
77 #define FSP_DIR_RENAME 0x80 /* can files be renamed in this dir */
78
79 /* decoded FSP packet */
80 typedef struct FSP_PKT {
81 unsigned char cmd; /* message code. */
82 unsigned char sum; /* message checksum. */
83 unsigned short key; /* message key. */
84 unsigned short seq; /* message sequence number. */
85 unsigned short len; /* number of bytes in buf 1. */
86 unsigned int pos; /* location in the file. */ unsigned short xlen; /* number of bytes in buf 2 */
87
88 unsigned char buf[FSP_SPACE]; /* packet payload */
89 } FSP_PKT;
90
91 /* FSP host:port */
92 typedef struct FSP_SESSION {
93 void * lock; /* key locking */
94 unsigned int timeout; /* send timeout 1/1000s*/
95 unsigned int maxdelay; /* maximum recv. delay */
96 unsigned short seq; /* sequence number */
97 unsigned int dupes; /* total pkt. dupes */
98 unsigned int resends; /* total pkt. sends */
99 unsigned int trips; /* total pkt trips */
100 unsigned long rtts; /* cumul. rtt */
101 unsigned int last_rtt; /* last rtt */
102 unsigned int last_delay; /* last delay time */
103 unsigned int last_dupes; /* last dupes */
104 unsigned int last_resends;/* last resends */
105 int fd; /* i/o descriptor */
106 char *password; /* host acccess password */
107 } FSP_SESSION;
108
109 /* fsp directory handle */
110 typedef struct FSP_DIR {
111 char *dirname; /* directory name */
112 short inuse; /* in use counter */
113 int dirpos; /* current directory pos. */
114 unsigned short blocksize; /* size of directory block */
115 unsigned char *data; /* raw directory data */
116 unsigned int datasize; /* size of raw dir. data */
117 } FSP_DIR;
118
119 /* fsp directory entry */
120 typedef struct FSP_RDENTRY {
121 char name[255 + 1]; /* entry name */
122 unsigned short namlen; /* length */
123 unsigned char type; /* field type */
124 unsigned short reclen; /* directory record length */
125 unsigned int size;
126 unsigned int lastmod;
127 } FSP_RDENTRY;
128
129 /* fsp file handle */
130 typedef struct FSP_FILE {
131 FSP_PKT in,out; /* io packets */
132 FSP_SESSION *s; /* master session */
133 char *name; /* filename for upload */
134 unsigned char writing; /* opened for writing */
135 unsigned char eof; /* EOF reached? */
136 unsigned char err; /* i/o error? */
137 int bufpos; /* position in buffer */
138 unsigned int pos; /* position of next packet */
139 } FSP_FILE;
140
141 /* function prototypes */
142
143 /* session management */
144 FSP_SESSION * fsp_open_session(const char *host,unsigned short port, const char *password);
145 void fsp_close_session(FSP_SESSION *s);
146
147 /* packet encoding/decoding */
148 size_t fsp_pkt_write(const FSP_PKT *p,void *space);
149 int fsp_pkt_read(FSP_PKT *p,const void *space,size_t recv_len);
150
151 /* send/receive round-trip */
152 int fsp_transaction(FSP_SESSION *s,FSP_PKT *p,FSP_PKT *rpkt);
153
154 /* directory listing commands */
155 FSP_DIR * fsp_opendir(FSP_SESSION *s,const char *dirname);
156 int fsp_readdir_r(FSP_DIR *dir,struct dirent *entry, struct dirent **result);
157 long fsp_telldir(FSP_DIR *dirp);
158 void fsp_seekdir(FSP_DIR *dirp, long loc);
159 void fsp_rewinddir(FSP_DIR *dirp);
160 struct dirent * fsp_readdir(FSP_DIR *dirp);
161 int fsp_readdir_native(FSP_DIR *dir,FSP_RDENTRY *entry, FSP_RDENTRY **result);
162 int fsp_closedir(FSP_DIR *dirp);
163 /* high level file i/o */
164 FSP_FILE * fsp_fopen(FSP_SESSION *session, const char *path,const char *modeflags);
165 size_t fsp_fread(void *ptr,size_t size,size_t nmemb,FSP_FILE *file);
166 size_t fsp_fwrite(const void * source, size_t size, size_t count, FSP_FILE * file);
167 int fsp_fclose(FSP_FILE *file);
168 int fsp_fpurge(FSP_FILE *file);
169 int fsp_fflush(FSP_FILE *file);
170 int fsp_fseek(FSP_FILE *stream, long offset, int whence);
171 long fsp_ftell(FSP_FILE *f);
172 void fsp_rewind(FSP_FILE *f);
173 /* misc. functions */
174 int fsp_stat(FSP_SESSION *s,const char *path,struct stat *sb);
175 int fsp_mkdir(FSP_SESSION *s,const char *directory);
176 int fsp_rmdir(FSP_SESSION *s,const char *directory);
177 int fsp_unlink(FSP_SESSION *s,const char *directory);
178 int fsp_rename(FSP_SESSION *s,const char *from, const char *to);
179 int fsp_access(FSP_SESSION *s,const char *path, int mode);
180 /* fsp protocol specific functions */
181 int fsp_getpro(FSP_SESSION *s,const char *directory,unsigned char *result);
182 int fsp_install(FSP_SESSION *s,const char *fname,time_t timestamp);
183 int fsp_canupload(FSP_SESSION *s,const char *fname);
184 #endif