r9704: r9684@blu: tridge | 2005-08-27 19:38:31 +1000
[jra/samba/.git] / source / lib / socket / socket.h
1 /* 
2    Unix SMB/CIFS implementation.
3    Socket functions
4    Copyright (C) Stefan Metzmacher 2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #ifndef _SAMBA_SOCKET_H
22 #define _SAMBA_SOCKET_H
23
24 struct socket_context;
25
26 enum socket_type {
27         SOCKET_TYPE_STREAM,
28         SOCKET_TYPE_DGRAM
29 };
30
31 struct socket_ops {
32         const char *name;
33
34         NTSTATUS (*fn_init)(struct socket_context *sock);
35
36         /* client ops */
37         NTSTATUS (*fn_connect)(struct socket_context *sock,
38                                 const char *my_address, int my_port,
39                                 const char *server_address, int server_port,
40                                 uint32_t flags);
41
42         /* complete a non-blocking connect */
43         NTSTATUS (*fn_connect_complete)(struct socket_context *sock,
44                                         uint32_t flags);
45
46         /* server ops */
47         NTSTATUS (*fn_listen)(struct socket_context *sock,
48                                 const char *my_address, int port, int queue_size, uint32_t flags);
49         NTSTATUS (*fn_accept)(struct socket_context *sock,      struct socket_context **new_sock);
50
51         /* general ops */
52         NTSTATUS (*fn_recv)(struct socket_context *sock, void *buf,
53                             size_t wantlen, size_t *nread, uint32_t flags);
54         NTSTATUS (*fn_send)(struct socket_context *sock, 
55                             const DATA_BLOB *blob, size_t *sendlen, uint32_t flags);
56
57         NTSTATUS (*fn_sendto)(struct socket_context *sock, 
58                               const DATA_BLOB *blob, size_t *sendlen, uint32_t flags,
59                               const char *dest_addr, int dest_port);
60         NTSTATUS (*fn_recvfrom)(struct socket_context *sock, 
61                                 void *buf, size_t wantlen, size_t *nread, uint32_t flags,
62                                 const char **src_addr, int *src_port);
63         NTSTATUS (*fn_pending)(struct socket_context *sock, size_t *npending);      
64
65         void (*fn_close)(struct socket_context *sock);
66
67         NTSTATUS (*fn_set_option)(struct socket_context *sock, const char *option, const char *val);
68
69         char *(*fn_get_peer_name)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
70         char *(*fn_get_peer_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
71         int (*fn_get_peer_port)(struct socket_context *sock);
72         char *(*fn_get_my_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
73         int (*fn_get_my_port)(struct socket_context *sock);
74
75         int (*fn_get_fd)(struct socket_context *sock);
76 };
77
78 enum socket_state {
79         SOCKET_STATE_UNDEFINED,
80
81         SOCKET_STATE_CLIENT_START,
82         SOCKET_STATE_CLIENT_CONNECTED,
83         SOCKET_STATE_CLIENT_STARTTLS,
84         SOCKET_STATE_CLIENT_ERROR,
85         
86         SOCKET_STATE_SERVER_LISTEN,
87         SOCKET_STATE_SERVER_CONNECTED,
88         SOCKET_STATE_SERVER_STARTTLS,
89         SOCKET_STATE_SERVER_ERROR
90 };
91
92 #define SOCKET_FLAG_BLOCK        0x00000001
93 #define SOCKET_FLAG_PEEK         0x00000002
94 #define SOCKET_FLAG_TESTNONBLOCK 0x00000004
95
96 struct socket_context {
97         enum socket_type type;
98         enum socket_state state;
99         uint32_t flags;
100
101         int fd;
102
103         void *private_data;
104         const struct socket_ops *ops;
105         const char *backend_name;
106 };
107
108
109 /* prototypes */
110 NTSTATUS socket_create(const char *name, enum socket_type type, 
111                        struct socket_context **new_sock, uint32_t flags);
112 NTSTATUS socket_connect(struct socket_context *sock,
113                         const char *my_address, int my_port,
114                         const char *server_address, int server_port,
115                         uint32_t flags);
116 NTSTATUS socket_connect_complete(struct socket_context *sock, uint32_t flags);
117 NTSTATUS socket_listen(struct socket_context *sock, const char *my_address, int port, int queue_size, uint32_t flags);
118 NTSTATUS socket_accept(struct socket_context *sock, struct socket_context **new_sock);
119 NTSTATUS socket_recv(struct socket_context *sock, void *buf, 
120                      size_t wantlen, size_t *nread, uint32_t flags);
121 NTSTATUS socket_recvfrom(struct socket_context *sock, void *buf, 
122                          size_t wantlen, size_t *nread, uint32_t flags,
123                          const char **src_addr, int *src_port);
124 NTSTATUS socket_send(struct socket_context *sock, 
125                      const DATA_BLOB *blob, size_t *sendlen, uint32_t flags);
126 NTSTATUS socket_sendto(struct socket_context *sock, 
127                        const DATA_BLOB *blob, size_t *sendlen, uint32_t flags,
128                        const char *dest_addr, int dest_port);
129 NTSTATUS socket_pending(struct socket_context *sock, size_t *npending);
130 NTSTATUS socket_set_option(struct socket_context *sock, const char *option, const char *val);
131 char *socket_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx);
132 char *socket_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
133 int socket_get_peer_port(struct socket_context *sock);
134 char *socket_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
135 int socket_get_my_port(struct socket_context *sock);
136 int socket_get_fd(struct socket_context *sock);
137 NTSTATUS socket_dup(struct socket_context *sock);
138 const struct socket_ops *socket_getops_byname(const char *name, enum socket_type type);
139 BOOL socket_check_access(struct socket_context *sock, 
140                          const char *service_name,
141                          const char **allow_list, const char **deny_list);
142
143 NTSTATUS socket_connect_ev(struct socket_context *sock,
144                            const char *my_address, int my_port,
145                            const char *server_address, int server_port,
146                            uint32_t flags, struct event_context *ev);
147
148 #endif /* _SAMBA_SOCKET_H */