1 /* distcache, Distributed Session Caching technology
2  * Copyright (C) 2000-2003  Geoff Thorpe, and Cryptographic Appliances, Inc.
3  * Copyright (C) 2004       The Distcache.org project
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License as published by the Free
7  * Software Foundation; using version 2.1 of the License. The copyright holders
8  * may elect to allow the application of later versions of the License to this
9  * software, please contact the author (geoff@distcache.org) if you wish us to
10  * review any later version released by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #ifndef HEADER_PRIVATE_NAL_INTERNAL_H
22 #define HEADER_PRIVATE_NAL_INTERNAL_H
23 
24 #ifndef HEADER_LIBSYS_PRE_H
25 	#error "Must include libsys/pre.h prior to libnal/nal.h"
26 #endif
27 
28 /* All our internal code will require the nal_devel.h header anyway, so include
29  * it here. */
30 #include <libnal/nal_devel.h>
31 
32 /* Utility functions and types used inside libnal. Eventually these should be
33  * hidden from API functions (only protocol implementations should require
34  * them) but until that's organised, I'm putting everything here. */
35 
36 /* Some platforms don't get socklen_t ... use int */
37 #ifndef socklen_t
38 #define socklen_t int
39 #endif
40 
41 /* Flags used in determining what "kind" of address has been created */
42 #define NAL_ADDRESS_CAN_LISTEN	(unsigned char)0x01
43 #define NAL_ADDRESS_CAN_CONNECT	(unsigned char)0x02
44 
45 /* nal_sock code will use this as a default in its call to listen(2) */
46 #define NAL_LISTENER_BACKLOG	511
47 
48 /* An upper limit on the size of address strings that will be allowable */
49 #define NAL_ADDRESS_MAX_STR_LEN	255
50 
51 /* This nal_sockaddr stuff is to encapsulate unix domain and ipv4 socket
52  * code. */
53 typedef enum {
54 	nal_sockaddr_type_ip,
55 	nal_sockaddr_type_unix
56 } nal_sockaddr_type;
57 typedef struct st_nal_sockaddr {
58 	union {
59 		struct sockaddr_in val_in;
60 #ifndef WIN32
61 		struct sockaddr_un val_un;
62 #endif
63 	} val;
64 	nal_sockaddr_type type;
65 	unsigned char caps;
66 } nal_sockaddr;
67 
68 /***********/
69 /* util_fd */
70 /***********/
71 
72 int nal_fd_make_non_blocking(int fd, int non_blocking);
73 int nal_fd_buffer_to_fd(NAL_BUFFER *buf, int fd, unsigned int max_send);
74 int nal_fd_buffer_from_fd(NAL_BUFFER *buf, int fd, unsigned int max_read);
75 void nal_fd_close(int *fd);
76 
77 /***************/
78 /* util_socket */
79 /***************/
80 
81 int nal_sock_set_nagle(int fd, int use_nagle, nal_sockaddr_type type);
82 int nal_sock_sockaddr_from_ipv4(nal_sockaddr *addr, const char *start_ptr);
83 int nal_sock_sockaddr_from_unix(nal_sockaddr *addr, const char *start_ptr);
84 int nal_sock_create_socket(int *fd, const nal_sockaddr *addr);
85 int nal_sock_create_unix_pair(int sv[2]);
86 int nal_sock_connect(int fd, const nal_sockaddr *addr, int *established);
87 int nal_sock_listen(int fd, const nal_sockaddr *addr);
88 int nal_sock_accept(int listen_fd, int *conn);
89 int nal_sock_is_connected(int fd);
90 int nal_sockaddr_get(nal_sockaddr *addr, int fd);
91 int nal_sockaddr_chown(const nal_sockaddr *addr, const char *username,
92 			const char *groupname);
93 int nal_sockaddr_chmod(const nal_sockaddr *addr, const char *octal_string);
94 
95 /****************/
96 /* NAL_SELECTOR */
97 /****************/
98 
99 /* This symbol controls what implementation NAL_SELECTOR_new() uses. Note, that
100  * NAL_SELECTOR_reset() will revert to the vtable it was initially created with
101  * (this makes more sense when alternative constructors are made for other
102  * vtables). */
103 #ifdef HAVE_SELECT
104 #ifdef HAVE_POLL
105 /* Decide between the two */
106 #ifdef PREFER_POLL
107 #define NAL_SELECTOR_VT_DEFAULT		sel_fdpoll
108 #else
109 #define NAL_SELECTOR_VT_DEFAULT		sel_fdselect
110 #endif
111 #else
112 /* Only select() */
113 #define NAL_SELECTOR_VT_DEFAULT		sel_fdselect
114 #endif
115 #else
116 /* No select() */
117 #ifdef HAVE_POLL
118 #define NAL_SELECTOR_VT_DEFAULT		sel_fdpoll
119 #else
120 #error "Neither HAVE_SELECT nor HAVE_POLL are defined"
121 #endif
122 #endif
123 
124 NAL_SELECTOR_TOKEN nal_selector_add_listener(NAL_SELECTOR *, NAL_LISTENER *);
125 NAL_SELECTOR_TOKEN nal_selector_add_connection(NAL_SELECTOR *, NAL_CONNECTION *);
126 void nal_selector_del_listener(NAL_SELECTOR *, NAL_LISTENER *, NAL_SELECTOR_TOKEN);
127 void nal_selector_del_connection(NAL_SELECTOR *, NAL_CONNECTION *, NAL_SELECTOR_TOKEN);
128 
129 /****************/
130 /* NAL_LISTENER */
131 /****************/
132 
133 unsigned int nal_listener_get_def_buffer_size(const NAL_LISTENER *l);
134 int nal_listener_set_def_buffer_size(NAL_LISTENER *l, unsigned int def_buffer_size);
135 
136 /**************/
137 /* NAL_BUFFER */
138 /**************/
139 
140 /* There's little point making data buffers bigger than this, but it can be
141  * changed later if desired. To cut down latencies, buffers used for network IO
142  * should only be big enough to ensure (a) "things fit", and (b) we can
143  * aggregate as much data as possible into single "read"s and "sends" (rather
144  * than sending multitudes of fragments. I strongly doubt it's possible to send
145  * a IP packet as big as 32K, so why bother adding further to latency by letting
146  * data-loops build up more than that before going back to the network code?
147  * Anyway - it'll get changed if it's a problem ... for now it's a good check to
148  * make sure the other code isn't too loose. */
149 #define NAL_BUFFER_MAX_SIZE  32768
150 #define nal_check_buffer_size(sz) (((sz) > NAL_BUFFER_MAX_SIZE) ? 0 : 1)
151 
152 #endif /* !defined(HEADER_PRIVATE_NAL_INTERNAL_H) */
153