r2343: - make socket_get_*_addr() return char * not const char *
[sfrench/samba-autobuild/.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 };
29
30 struct socket_ops {
31         const char *name;
32         enum socket_type type;
33
34         NTSTATUS (*init)(struct socket_context *sock);
35
36         /* client ops */
37         NTSTATUS (*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         /* server ops */
43         NTSTATUS (*listen)(struct socket_context *sock,
44                                 const char *my_address, int port, int queue_size, uint32_t flags);
45         NTSTATUS (*accept)(struct socket_context *sock,
46                                 struct socket_context **new_sock, uint32_t flags);
47
48         /* general ops */
49         NTSTATUS (*recv)(struct socket_context *sock, TALLOC_CTX *mem_ctx, 
50                                 DATA_BLOB *blob, size_t wantlen, uint32_t flags);
51         NTSTATUS (*send)(struct socket_context *sock, TALLOC_CTX *mem_ctx,
52                                 const DATA_BLOB *blob, size_t *sendlen, uint32_t flags);
53
54         void (*close)(struct socket_context *sock);
55
56         NTSTATUS (*set_option)(struct socket_context *sock, const char *option, const char *val);
57
58         char *(*get_peer_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
59         int (*get_peer_port)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
60         char *(*get_my_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
61         int (*get_my_port)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
62
63         int (*get_fd)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
64 };
65
66 enum socket_state {
67         SOCKET_STATE_UNDEFINED,
68
69         SOCKET_STATE_CLIENT_START,
70         SOCKET_STATE_CLIENT_CONNECTED,
71         SOCKET_STATE_CLIENT_STARTTLS,
72         SOCKET_STATE_CLIENT_ERROR,
73         
74         SOCKET_STATE_SERVER_LISTEN,
75         SOCKET_STATE_SERVER_CONNECTED,
76         SOCKET_STATE_SERVER_STARTTLS,
77         SOCKET_STATE_SERVER_ERROR
78 };
79
80 #define SOCKET_FLAG_BLOCK 0x00000001
81 #define SOCKET_FLAG_PEEK  0x00000002
82
83 struct socket_context {
84         enum socket_type type;
85         enum socket_state state;
86         uint32_t flags;
87
88         int fd;
89
90         void *private_data;
91         const struct socket_ops *ops;
92 };
93
94 #endif /* _SAMBA_SOCKET_H */