1 /*
2  * $Id: com-socket.h,v 1.5.2.1 2003/05/07 11:13:34 mt Exp $
3  *
4  * Header for common functions for TCP/IP sockets
5  *
6  * Author(s): Jens-Gero Boehm <jens-gero.boehm@suse.de>
7  *            Pieter Hollants <pieter.hollants@suse.de>
8  *            Marius Tomaschewski <mt@suse.de>
9  *            Volker Wiegand <volker.wiegand@suse.de>
10  *
11  * This file is part of the SuSE Proxy Suite
12  *            See also  http://proxy-suite.suse.de/
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version
17  * 2 of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the
26  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27  * Boston, MA 02111-1307, USA.
28  *
29  * A history log can be found at the end of this file.
30  */
31 
32 #if !defined(_COM_SOCKET_H_)
33 #define _COM_SOCKET_H_
34 
35 /* ------------------------------------------------------------ */
36 
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 
40 #if !defined(INADDR_NONE)
41 #  if defined(INADDR_BROADCAST)
42 #    define INADDR_NONE INADDR_BROADCAST
43 #  else
44 #    define INADDR_NONE ((uint32_t) 0xffffffffU)
45 #  endif
46 #endif
47 
48 #if !defined(INPORT_ANY)
49 #  define   INPORT_ANY   0
50 #endif
51 
52 #if !defined(MAXHOSTNAMELEN)
53 #  define MAXHOSTNAMELEN 256
54 #endif
55 
56 /* ------------------------------------------------------------ */
57 
58 #define SK_LISTEN	1	/* Kind: listening socket	*/
59 #define SK_CONTROL	2	/* Kind: control connection	*/
60 #define SK_DATA		3	/* Kind: data transfer		*/
61 
62 #define LOC_END		1	/* Local end of connection	*/
63 #define REM_END		2	/* Remote end of connection	*/
64 
65 #define PEER_LEN	32	/* Storage for dotted decimal	*/
66 
67 #define MAX_RETRIES	6	/* bind retries on EADDRINUSE	*/
68 
69 typedef void (*ACPT_CB)(int);	/* Accept callback function	*/
70 
71 
72 /* ------------------------------------------------------------ */
73 
74 /*
75 ** Definition of a "High Level Socket"
76 */
77 
78 typedef struct buf_t {
79 	struct buf_t *next;	/* Next one in the chain	*/
80 	size_t  len;		/* Number of bytes at ptr	*/
81 	size_t  cur;		/* Currently used offset	*/
82 	int     flg;		/* Flag for send() (e.g. OOB)	*/
83 	char    dat[8];		/* Beginning of data (Guard)	*/
84 } BUF;
85 
86 typedef struct hls_t {
87 	struct hls_t *next;	/* Next one in the chain	*/
88 	int       sock;		/* The corresponding socket	*/
89 	int       kill;		/* 1=kill socket after send	*/
90 	int       ernr;		/* socket i/o error number	*/
91 	int       retr;		/* recv i/o retry counter	*/
92 	int       flag;		/* Flag for send() (e.g. OOB)	*/
93 	int       more;		/* 1=read more to complete line	*/
94 	u_int32_t addr;		/* Peer's address (host order)	*/
95 	u_int16_t port;		/* Peer's port (host order)	*/
96 	char      peer[PEER_LEN]; /* Peer's readable address	*/
97 	char     *ctyp;		/* Connection type identifier	*/
98 	BUF      *wbuf;		/* Write buffer chain		*/
99 	BUF      *rbuf;		/* Read buffer chain		*/
100 	size_t    wcnt;		/* write bytes counter		*/
101 	size_t    rcnt;		/* read bytes counter		*/
102 } HLS;
103 
104 
105 /* ------------------------------------------------------------ */
106 
107 int  socket_listen (u_int32_t addr, u_int16_t port, ACPT_CB func);
108 void socket_lclose (int shut);
109 
110 HLS  *socket_init  (int sock);
111 void  socket_opts  (int sock, int kind);
112 void  socket_kill  (HLS *hls);
113 char *socket_gets  (HLS *hls, char *ptr, int len);
114 void  socket_flag  (HLS *hls, int flag);
115 int   socket_write (HLS *hls, char *ptr, int len);
116 int   socket_printf(HLS *hls, char *fmt, ...);
117 int   socket_file  (HLS *hls, char *file, int crlf);
118 
119 int   socket_exec  (int timeout, int *close_flag);
120 
121 char *socket_msgline(char *fmt);
122 
123 u_int16_t socket_d_bind   (int sock, u_int32_t addr,
124 			   u_int16_t lrng, u_int16_t urng,
125 			   int incr);
126 
127 u_int16_t socket_d_listen (u_int32_t addr,
128 			   u_int16_t lrng, u_int16_t urng,
129 			   HLS **phls, char *ctyp,
130 			   int incr);
131 
132 u_int16_t socket_d_connect(u_int32_t addr, u_int16_t port,
133 			   u_int32_t ladr,
134 			   u_int16_t lrng, u_int16_t urng,
135 			   HLS **phls, char *ctyp,
136 			   int incr);
137 
138 u_int32_t  socket_str2addr(char *name, u_int32_t dflt);
139 u_int16_t  socket_str2port(char *name, u_int16_t dflt);
140 char      *socket_addr2str(u_int32_t addr);
141 u_int32_t  socket_sck2addr(int sock, int peer, u_int16_t *port);
142 
143 int        socket_chkladdr(u_int32_t addr);
144 int        socket_orgdst(HLS *phls, u_int32_t *addr, u_int16_t *port);
145 
146 int        getfqhostname(char *fqhost, size_t n);
147 int        getfqdomainname(char *fqdomain, size_t n);
148 
149 /* ------------------------------------------------------------ */
150 
151 #endif /* defined(_COM_SOCKET_H_) */
152 
153 /* ------------------------------------------------------------
154  * $Log: com-socket.h,v $
155  * Revision 1.5.2.1  2003/05/07 11:13:34  mt
156  * added hls->retr -- recv i/o retry counter
157  *
158  * Revision 1.5  2002/05/02 13:01:32  mt
159  * merged with v1.8.2.2
160  *
161  * Revision 1.4.2.1  2002/04/04 14:33:17  mt
162  * added ernr flag in hls needed to remember failures
163  *
164  * Revision 1.4  2002/01/14 18:26:55  mt
165  * implemented socket_orgdst to read transparent proxying destinations
166  * implemented a MaxRecvBufSize option limiting max recv buffer size
167  * implemented workarround for Netscape (4.x) directory symlink handling
168  * extended log messages to provide basic transfer statistics data
169  * fixed socket_gets to wait for a complete line if no EOL found
170  * added snprintf usage, replaced strcpy/strncpy with misc_strncpy
171  *
172  * Revision 1.3  2001/11/06 23:04:43  mt
173  * applied / merged with transparent proxy patches v8
174  * see ftp-proxy/NEWS for more detailed release news
175  *
176  * Revision 1.2  1999/09/17 06:32:28  wiegand
177  * buffer length and overflow protection review
178  *
179  * Revision 1.1  1999/09/15 14:05:38  wiegand
180  * initial checkin
181  *
182  * ------------------------------------------------------------ */
183 
184