summaryrefslogtreecommitdiffstats
path: root/include/libtf/io.h
blob: d1098e30c7b47ea3b00d6e038fd31092c818baec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* tf.h - libtf io header
 *
 * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or later as
 * published by the Free Software Foundation.
 *
 * See http://www.gnu.org/ for details.
 */

#ifndef TF_IO_H
#define TF_IO_H

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in.h>

#include <libtf/defines.h>

/* Flags for tf_open_fd() */
#define TF_FD_READ			TF_BIT(0)
#define TF_FD_WRITE			TF_BIT(1)
#define TF_FD_AUTOCLOSE			TF_BIT(2)
#define TF_FD_STREAM_ORIENTED		TF_BIT(3)
#define TF_FD_SET_CLOEXEC		TF_BIT(4)
#define TF_FD_ALREADY_NONBLOCKING	TF_BIT(5)

struct tf_sockaddr {
	union {
		struct sockaddr		addr;
		struct sockaddr_in	in;
		struct sockaddr_in6	in6;
	} u;
};

struct tf_fd {
	int fd;
	unsigned int flags;
	struct tf_fiber *fiber;
};

struct tf_poll_hooks {
	void *	(*create)(void);
	int	(*fd_created)(void *fiber, struct tf_fd *fd);
	int	(*fd_destroyed)(void *fiber, struct tf_fd *fd);
	void	(*fd_rearm)(void *fiber, struct tf_fd *fd);
};

int tf_open_fd(struct tf_fd *fd, int kfd, int flags);
int tf_open(struct tf_fd *fd, const char *pathname, int flags);
int tf_close(struct tf_fd *fd);
ssize_t tf_read(struct tf_fd *fd, void *buf, size_t count);
ssize_t tf_write(struct tf_fd *fd, const void *buf, size_t count);
int tf_read_fully(struct tf_fd *fd, void *buf, size_t count);
int tf_write_fully(struct tf_fd *fd, const void *buf, size_t count);

int tf_socket(struct tf_fd *fd, int domain, int type, int protocol);
int tf_bind(struct tf_fd *fd, const struct tf_sockaddr *addr);
int tf_listen(struct tf_fd *fd, int backlog);

int tf_accept(struct tf_fd *listen_fd, struct tf_fd *child_fd,
	      struct tf_sockaddr *from);
int tf_connect(struct tf_fd *fd, const struct tf_sockaddr *addr);

ssize_t tf_recvmsg(struct tf_fd *fd,
		   struct tf_sockaddr *from, struct tf_sockaddr *to,
		   void *buf, size_t count);
ssize_t tf_sendmsg(struct tf_fd *fd,
		   struct tf_sockaddr *from, const struct tf_sockaddr *to,
		   const void *buf, size_t count);

int tf_query_dns(const char *name, int num_res, struct tf_sockaddr *res);

#endif