Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
knetfile.h
Go to the documentation of this file.
1 /* The MIT License
2 
3  Copyright (c) 2008 by Genome Research Ltd (GRL).
4  2010 by Attractive Chaos <attractor@live.co.uk>
5 
6  Permission is hereby granted, free of charge, to any person obtaining
7  a copy of this software and associated documentation files (the
8  "Software"), to deal in the Software without restriction, including
9  without limitation the rights to use, copy, modify, merge, publish,
10  distribute, sublicense, and/or sell copies of the Software, and to
11  permit persons to whom the Software is furnished to do so, subject to
12  the following conditions:
13 
14  The above copyright notice and this permission notice shall be
15  included in all copies or substantial portions of the Software.
16 
17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  SOFTWARE.
25 */
26 
27 #ifndef KNETFILE_H
28 #define KNETFILE_H
29 
30 #include <stdint.h>
31 #include <fcntl.h>
32 
33 #ifndef _WIN32
34 #define netread(fd, ptr, len) read(fd, ptr, len)
35 #define netwrite(fd, ptr, len) write(fd, ptr, len)
36 #define netclose(fd) close(fd)
37 #else
38 #include <winsock2.h>
39 #define netread(fd, ptr, len) recv(fd, ptr, len, 0)
40 #define netwrite(fd, ptr, len) send(fd, ptr, len, 0)
41 #define netclose(fd) closesocket(fd)
42 #endif
43 
44 // FIXME: currently I/O is unbuffered
45 
46 #define KNF_TYPE_LOCAL 1
47 #define KNF_TYPE_FTP 2
48 #define KNF_TYPE_HTTP 3
49 
50 typedef struct knetFile_s {
51  int type, fd;
52  int64_t offset;
53  char *host, *port;
54 
55  // the following are for FTP only
57  char *response, *retr, *size_cmd;
58  int64_t seek_offset; // for lazy seek
59  int64_t file_size;
60 
61  // the following are for HTTP only
62  char *path, *http_host;
63 } knetFile;
64 
65 #define knet_tell(fp) ((fp)->offset)
66 #define knet_fileno(fp) ((fp)->fd)
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
72 #ifdef _WIN32
73  int knet_win32_init();
74  void knet_win32_destroy();
75 #endif
76 
77  knetFile *knet_open(const char *fn, const char *mode);
78 
79  /*
80  This only works with local files.
81  */
82  knetFile *knet_dopen(int fd, const char *mode);
83 
84  /*
85  If ->is_ready==0, this routine updates ->fd; otherwise, it simply
86  reads from ->fd.
87  */
88  ssize_t knet_read(knetFile *fp, void *buf, size_t len);
89 
90  /*
91  This routine only sets ->offset and ->is_ready=0. It does not
92  communicate with the FTP server.
93  */
94  off_t knet_seek(knetFile *fp, off_t off, int whence);
95  int knet_close(knetFile *fp);
96 
97 #ifdef __cplusplus
98 }
99 #endif
100 
101 #endif
int is_ready
Definition: knetfile.h:56
char * retr
Definition: knetfile.h:57
char * port
Definition: knetfile.h:53
struct knetFile_s knetFile
int no_reconnect
Definition: knetfile.h:56
int pasv_ip[4]
Definition: knetfile.h:56
off_t knet_seek(knetFile *fp, off_t off, int whence)
Definition: knetfile.c:542
int pasv_port
Definition: knetfile.h:56
knetFile * knet_open(const char *fn, const char *mode)
Definition: knetfile.c:461
int ctrl_fd
Definition: knetfile.h:56
enum @17 mode
int max_response
Definition: knetfile.h:56
int64_t offset
Definition: knetfile.h:52
char * size_cmd
Definition: knetfile.h:57
int fd
Definition: knetfile.h:51
char * response
Definition: knetfile.h:57
int64_t file_size
Definition: knetfile.h:59
int64_t seek_offset
Definition: knetfile.h:58
int type
Definition: knetfile.h:51
char * path
Definition: knetfile.h:62
ssize_t knet_read(knetFile *fp, void *buf, size_t len)
Definition: knetfile.c:513
knetFile * knet_dopen(int fd, const char *mode)
Definition: knetfile.c:505
char * host
Definition: knetfile.h:53
Definition: knetfile.h:50
char * http_host
Definition: knetfile.h:62
int knet_close(knetFile *fp)
Definition: knetfile.c:575