1/*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef _WEBDAV_NETWORK_H_INCLUDE
25#define _WEBDAV_NETWORK_H_INCLUDE
26
27#include "webdavd.h"
28
29#include <CoreServices/CoreServices.h>
30
31#define WEBDAV_MAX_REDIRECTS 5	/* avoids infinite 3xx redirection loops */
32
33enum
34{
35	kHttpDefaultPort = 80,	/* default port for HTTP */
36	kHttpsDefaultPort = 443	/* default port for HTTPS */
37};
38
39struct ReadStreamRec
40{
41	int inUse;						/* non-zero if this ReadStreamRec is in use */
42	CFReadStreamRef readStreamRef;	/* the read stream, or NULL */
43	CFStringRef uniqueValue;		/* CFString used to make stream unique */
44	int connectionClose;			/* if TRUE, readStreamRef should be closed when transaction is complete */
45};
46
47int network_init(
48	const UInt8 *uri,			/* -> bytes containing base URI to server */
49	CFIndex uriLength,			/* -> length of uri string */
50	int *store_notify_fd,		/* <-> pointer to int where the dynamic store notification fd is returned */
51	int add_mirror_comment);	/* -> if true, add the mirror comment to the WebDAVFS product in the User-Agent header */
52
53int network_get_proxy_settings(
54	int *httpProxyEnabled,		/* <- true if HTTP proxy is enabled */
55	char **httpProxyServer,		/* <- HTTP proxy server name */
56	int *httpProxyPort,			/* <- HTTP proxy port number */
57	int *httpsProxyEnabled,		/* <- true if HTTPS proxy is enabled */
58	char **httpsProxyServer,	/* <- HTTPS proxy server name */
59	int* httpsProxyPort);		/* <- HTTPS proxy port number */
60
61int network_update_proxy(void *arg);
62
63int network_lookup(
64	uid_t uid,					/* -> uid of the user making the request */
65	struct node_entry *node,	/* -> parent node */
66	char *name,					/* -> filename to find */
67	size_t name_length,			/* -> length of name */
68	struct webdav_stat_attr *statbuf);	/* <- stat information is returned in this buffer except for st_ino */
69
70int network_getattr(
71	uid_t uid,					/* -> uid of the user making the request */
72	struct node_entry *node,	/* -> parent node */
73	struct webdav_stat_attr *statbuf);	/* <- stat information is returned in this buffer */
74
75int network_mount(
76	uid_t uid,					/* -> uid of the user making the request */
77	int *server_mount_flags);	/* <- flags to OR in with mount flags (i.e., MNT_RDONLY) */
78								/* NOTE: if webdavfs is changed to support advlocks, then
79								 * server_mount_flags parameter is not needed.
80								 */
81
82int network_finish_download(
83	struct node_entry *node,	/* -> node to download to */
84	struct ReadStreamRec *readStreamRecPtr); /* -> the ReadStreamRec */
85
86/*
87 * Sends an "OPTIONS" request to the server after 'delay' seconds
88 * Returns 0 on success.
89 */
90int network_server_ping(u_int32_t delay);
91
92void network_seqwrite_manager(struct stream_put_ctx *ctx);
93
94// Note: ctx->lock must be held before calling queue_writemgr_request_locked()
95enum {WRITE_MGR_NEW_REQUEST_ID = 1};
96#define WRITE_MGR_MSG_PORTSEND_TIMEOUT 10.0
97#define WRITE_MGR_MSG_PORT_NAME_BASE_STRING "com.apple.webdavfs.msgport"
98#define WRITE_MGR_MSG_PORT_NAME_TEMPLATE  ("%s-0x%016u-%p")
99//
100// WRITE_MGR_MSG_PORT_NAME_BUFSIZE must be large enough
101// to hold strings like this:
102// com.apple.webdavfs.msgport-0x0000000000000594-0x000000064e689a32
103// |<------- 26 bytes------>|<------------ 38 bytes ------------->|
104//
105#define WRITE_MGR_MSG_PORT_NAME_BUFSIZE 128
106
107int queue_writemgr_request_locked(struct stream_put_ctx *ctx, struct seqwrite_mgr_req *req);
108struct seqwrite_mgr_req *dequeue_writemgr_request_locked(struct stream_put_ctx *ctx);
109void release_writemgr_request_locked(struct seqwrite_mgr_req *req);
110void release_writemgr_request(struct stream_put_ctx *ctx, struct seqwrite_mgr_req *req);
111
112void writeseqReadResponseCallback(CFReadStreamRef str,
113								  CFStreamEventType event,
114								  void* arg);
115
116void writeseqWriteCallback(CFWriteStreamRef str,
117						   CFStreamEventType event,
118						   void* arg);
119
120CFDataRef managerMessagePortCallback(CFMessagePortRef local,
121										SInt32 msgid,
122										CFDataRef data,
123										void *info);
124
125int cleanup_seq_write(struct stream_put_ctx *ctx);
126
127int network_open(
128	uid_t uid,					/* -> uid of the user making the request */
129	struct node_entry *node,	/* -> node to open */
130	int write_access);			/* -> open requires write access */
131
132int network_statfs(
133	uid_t uid,					/* -> uid of the user making the request */
134	struct node_entry *node,	/* -> root node */
135	struct statfs *fs_attr);	/* <- file system information */
136
137int network_create(
138	uid_t uid,					/* -> uid of the user making the request */
139	struct node_entry *node,	/* -> parent node */
140	char *name,					/* -> file name to create */
141	size_t name_length,			/* -> length of name */
142	time_t *creation_date);		/* <- date of the creation */
143
144int setup_seq_write(
145	uid_t uid,				  /* -> uid of the user making the request */
146	struct node_entry *node,  /* -> node we're writing  */
147	off_t file_length);       /* -> file length hint sent from the kernel */
148
149int network_fsync(
150	uid_t uid,					/* -> uid of the user making the request */
151	struct node_entry *node,	/* -> node to sync with server */
152	off_t *file_length,			/* <- length of file */
153	time_t *file_last_modified); /* <- date of last modification */
154
155int network_remove(
156	uid_t uid,					/* -> uid of the user making the request */
157	struct node_entry *node,	/* -> file node to remove on the server */
158	time_t *remove_date);		/* <- date of the removal */
159
160int network_rmdir(
161	uid_t uid,					/* -> uid of the user making the request */
162	struct node_entry *node,	/* -> directory node to remove on the server */
163	time_t *remove_date);		/* <- date of the removal */
164
165int network_rename(
166	uid_t uid,					/* -> uid of the user making the request */
167	struct node_entry *from_node, /* node to move */
168	struct node_entry *to_node,	/* node to move over (ignored if NULL) */
169	struct node_entry *to_dir_node, /* directory node move into (ignored if to_node != NULL) */
170	char *to_name,				/* new name for the object (ignored if to_node != NULL) */
171	size_t to_name_length,		/* length of to_name (ignored if to_node != NULL) */
172	time_t *rename_date);		/* <- date of the rename */
173								/* NOTE: the destination either is to_node, or is the combination
174								 * of to_dir_node and to_name.
175								 */
176
177int network_lock(
178	uid_t uid,					/* -> uid of the user making the request (ignored if refreshing) */
179	int refresh,				/* -> if FALSE, we're getting the lock (for uid); if TRUE, we're refreshing the lock */
180	struct node_entry *node);	/* -> node to get/renew server lock on */
181
182int network_unlock_with_nodecache_locked(
183	struct node_entry *node);	/* -> node to unlock on server */
184
185int network_unlock(
186	struct node_entry *node);	/* -> node to unlock on server */
187								/* NOTE: uid comes from node */
188
189int network_readdir(
190	uid_t uid,					/* -> uid of the user making the request */
191	int cache,					/* -> if TRUE, perform additional caching */
192	struct node_entry *node);	/* -> directory node to read */
193
194int network_mkdir(
195	uid_t uid,					/* -> uid of the user making the request */
196	struct node_entry *node,	/* -> parent node */
197	char *name,					/* -> directory name to create */
198	size_t name_length,			/* -> length of name */
199	time_t *creation_date);		/* <- date of the creation */
200
201int network_read(
202	uid_t uid,					/* -> uid of the user making the request */
203	struct node_entry *node,	/* -> node to read */
204	off_t offset,				/* -> position within the file at which the read is to begin */
205	size_t count,				/* -> number of bytes of data to be read */
206	char **buffer,				/* <- buffer data was read into (allocated by network_read) */
207	size_t *actual_count);		/* <- number of bytes actually read */
208
209/* Read the response CFReadStream of a sequential write */
210int network_read_seqwrite_rsp(
211	struct stream_put_ctx *ctx);	/* -> sequential write context */
212
213time_t DateBytesToTime(			/* <- time_t value; -1 if error */
214	const UInt8 *bytes,			/* -> pointer to bytes to parse */
215	CFIndex length);			/* -> number of bytes to parse */
216
217time_t ISO8601ToTime(			/* <- time_t value; -1 if error */
218	const UInt8 *bytes,			/* -> pointer to bytes to parse */
219	CFIndex length);			/* -> number of bytes to parse */
220
221#endif
222