summaryrefslogtreecommitdiffstats
path: root/include/libtf/io.h
blob: 1f37d8197f287bddc20a451059ef618e3032eb74 (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
/* 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_AUTOCLOSE			1
#define TF_FD_STREAM_ORIENTED		2
#define TF_FD_SET_CLOEXEC		4
#define TF_FD_ALREADY_NONBLOCKING	8

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

struct tf_fd {
	int fd;
	unsigned int flags;
	/* Single waiter -- would be relatively trivial to modify to allow
	 * multiple waiters, if someone actually needs it */
	unsigned int events;
	void *waiting_fiber;
};

void tf_poll_init(void);
int  tf_poll(tf_mtime_diff_t timeout);
void tf_poll_close(void);

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