1 /*
2  *  tvheadend, TCP common functions
3  *  Copyright (C) 2007 Andreas Öman
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TCP_H_
20 #define TCP_H_
21 
22 #include "htsbuf.h"
23 #include "htsmsg.h"
24 
25 #if defined(PLATFORM_FREEBSD)
26 #include <sys/socket.h>
27 #endif
28 
29 #define IP_AS_V4(storage, f) ((struct sockaddr_in *)&(storage))->sin_##f
30 #define IP_AS_V6(storage, f) ((struct sockaddr_in6 *)&(storage))->sin6_##f
31 #define IP_IN_ADDR(storage) \
32   ((storage).ss_family == AF_INET6 ? \
33       &((struct sockaddr_in6 *)&(storage))->sin6_addr : \
34       (void *)&((struct sockaddr_in  *)&(storage))->sin_addr)
35 #define IP_IN_ADDRLEN(storage) \
36   ((storage).ss_family == AF_INET6 ? \
37       sizeof(struct sockaddr_in6) : \
38       sizeof(struct sockaddr_in))
39 #define IP_PORT(storage) \
40   ((storage).ss_family == AF_INET6 ? \
41       ((struct sockaddr_in6 *)&(storage))->sin6_port : \
42       ((struct sockaddr_in  *)&(storage))->sin_port)
43 #define IP_PORT_SET(storage, port) \
44   do { \
45     if ((storage).ss_family == AF_INET6) \
46       ((struct sockaddr_in6 *)&(storage))->sin6_port = (port); else \
47       ((struct sockaddr_in  *)&(storage))->sin_port  = (port); \
48   } while (0)
49 
50 typedef struct tcp_server_ops
51 {
52   void (*start)  (int fd, void **opaque,
53                      struct sockaddr_storage *peer,
54                      struct sockaddr_storage *self);
55   void (*stop)   (void *opaque);
56   void (*cancel) (void *opaque);
57 } tcp_server_ops_t;
58 
59 extern int tcp_preferred_address_family;
60 
61 void tcp_server_preinit(int opt_ipv6);
62 void tcp_server_init(void);
63 void tcp_server_done(void);
64 
65 int socket_set_dscp(int sockfd, uint32_t dscp, char *errbuf, size_t errbufsize);
66 
67 int tcp_connect(const char *hostname, int port, const char *bindaddr,
68                 char *errbuf, size_t errbufsize, int timeout);
69 
70 typedef void (tcp_server_callback_t)(int fd, void *opaque,
71 				     struct sockaddr_storage *peer,
72 				     struct sockaddr_storage *self);
73 
74 void *tcp_server_create(int subsystem, const char *name,
75                         const char *bindaddr, int port,
76                         tcp_server_ops_t *ops, void *opaque);
77 
78 void tcp_server_register(void *server);
79 
80 void tcp_server_delete(void *server);
81 
82 int tcp_default_ip_addr(struct sockaddr_storage *deflt, int family);
83 
84 int tcp_server_bound(void *server, struct sockaddr_storage *bound, int family);
85 
86 int tcp_server_onall(void *server);
87 
88 int tcp_read(int fd, void *buf, size_t len);
89 
90 char *tcp_read_line(int fd, htsbuf_queue_t *spill);
91 
92 int tcp_read_data(int fd, char *buf, const size_t bufsize,
93 		  htsbuf_queue_t *spill);
94 
95 int tcp_write_queue(int fd, htsbuf_queue_t *q);
96 
97 int tcp_read_timeout(int fd, void *buf, size_t len, int timeout);
98 
99 char *tcp_get_str_from_ip(const struct sockaddr_storage *sa, char *dst, size_t maxlen);
100 
101 struct sockaddr_storage *tcp_get_ip_from_str(const char *str, struct sockaddr_storage *sa);
102 
103 int tcp_socket_dead(int fd);
104 
105 struct access;
106 
107 uint32_t tcp_connection_count(struct access *aa);
108 void *tcp_connection_launch(int fd, void (*status) (void *opaque, htsmsg_t *m),
109                             struct access *aa);
110 void tcp_connection_land(void *tcp_id);
111 void tcp_connection_cancel(uint32_t id);
112 
113 htsmsg_t *tcp_server_connections ( void );
114 
115 #endif /* TCP_H_ */
116