r4831: added udp support to our generic sockets library.
[samba.git] / source4 / 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
64         void (*fn_close)(struct socket_context *sock);
65
66         NTSTATUS (*fn_set_option)(struct socket_context *sock, const char *option, const char *val);
67
68         char *(*fn_get_peer_name)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
69         char *(*fn_get_peer_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
70         int (*fn_get_peer_port)(struct socket_context *sock);
71         char *(*fn_get_my_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
72         int (*fn_get_my_port)(struct socket_context *sock);
73
74         int (*fn_get_fd)(struct socket_context *sock);
75 };
76
77 enum socket_state {
78         SOCKET_STATE_UNDEFINED,
79
80         SOCKET_STATE_CLIENT_START,
81         SOCKET_STATE_CLIENT_CONNECTED,
82         SOCKET_STATE_CLIENT_STARTTLS,
83         SOCKET_STATE_CLIENT_ERROR,
84         
85         SOCKET_STATE_SERVER_LISTEN,
86         SOCKET_STATE_SERVER_CONNECTED,
87         SOCKET_STATE_SERVER_STARTTLS,
88         SOCKET_STATE_SERVER_ERROR
89 };
90
91 #define SOCKET_FLAG_BLOCK        0x00000001
92 #define SOCKET_FLAG_PEEK         0x00000002
93 #define SOCKET_FLAG_TESTNONBLOCK 0x00000004
94
95 struct socket_context {
96         enum socket_type type;
97         enum socket_state state;
98         uint32_t flags;
99
100         int fd;
101
102         void *private_data;
103         const struct socket_ops *ops;
104 };
105
106 #endif /* _SAMBA_SOCKET_H */