]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfcnb/session.c
Merged from trunk (r12732, v3.3.3+).
[thirdparty/squid.git] / lib / rfcnb / session.c
1 #include "squid.h"
2
3 /* UNIX RFCNB (RFC1001/RFC1002) NetBIOS implementation
4 *
5 * Version 1.0
6 * Session Routines ...
7 *
8 * Copyright (C) Richard Sharpe 1996
9 *
10 */
11
12 /*
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28 int RFCNB_errno = 0;
29 int RFCNB_saved_errno = 0;
30 #define RFCNB_ERRNO
31
32 #include "rfcnb/std-includes.h"
33 #include <netinet/tcp.h>
34 #include "rfcnb/rfcnb.h"
35 #include "rfcnb/rfcnb-priv.h"
36 #include "rfcnb/rfcnb-io.h"
37 #include "rfcnb/rfcnb-util.h"
38
39 #if HAVE_STRING_H
40 #include <string.h>
41 #endif
42
43 int RFCNB_Stats[RFCNB_MAX_STATS];
44
45 RFCNB_Prot_Print_Routine *Prot_Print_Routine = NULL; /* Pointer to protocol print routine */
46
47 /* Set up a session with a remote name. We are passed Called_Name as a
48 * string which we convert to a NetBIOS name, ie space terminated, up to
49 * 16 characters only if we need to. If Called_Address is not empty, then
50 * we use it to connect to the remote end, but put in Called_Name ... Called
51 * Address can be a DNS based name, or a TCP/IP address ...
52 */
53
54 void *
55 RFCNB_Call(char *Called_Name, char *Calling_Name, char *Called_Address, int port)
56 {
57 struct RFCNB_Con *con;
58 struct in_addr Dest_IP;
59 int Client;
60 BOOL redirect;
61 struct redirect_addr *redir_addr;
62 char *Service_Address;
63
64 /* Now, we really should look up the port in /etc/services ... */
65
66 if (port == 0)
67 port = RFCNB_Default_Port;
68
69 /* Create a connection structure first */
70
71 if ((con = (struct RFCNB_Con *) malloc(sizeof(struct RFCNB_Con))) == NULL) { /* Error in size */
72
73 RFCNB_errno = RFCNBE_NoSpace;
74 RFCNB_saved_errno = errno;
75 return (NULL);
76
77 }
78 con->fd = -0; /* no descriptor yet */
79 con->errn = 0; /* no error yet */
80 con->timeout = 0; /* no timeout */
81 con->redirects = 0;
82 con->redirect_list = con->last_addr = NULL;
83
84 /* Resolve that name into an IP address */
85
86 Service_Address = Called_Name;
87 if (strcmp(Called_Address, "") != 0) { /* If the Called Address = "" */
88 Service_Address = Called_Address;
89 }
90 if ((errno = RFCNB_Name_To_IP(Service_Address, &Dest_IP)) < 0) { /* Error */
91
92 /* No need to modify RFCNB_errno as it was done by RFCNB_Name_To_IP */
93 free(con);
94 return (NULL);
95
96 }
97 /* Now connect to the remote end */
98
99 redirect = TRUE; /* Fudge this one so we go once through */
100
101 while (redirect) { /* Connect and get session info etc */
102
103 redirect = FALSE; /* Assume all OK */
104
105 /* Build the redirect info. First one is first addr called */
106 /* And tack it onto the list of addresses we called */
107
108 if ((redir_addr = (struct redirect_addr *) malloc(sizeof(struct redirect_addr))) == NULL) { /* Could not get space */
109
110 RFCNB_errno = RFCNBE_NoSpace;
111 RFCNB_saved_errno = errno;
112 free(con);
113 return (NULL);
114
115 }
116 memcpy((char *) &(redir_addr->ip_addr), (char *) &Dest_IP, sizeof(Dest_IP));
117 redir_addr->port = port;
118 redir_addr->next = NULL;
119
120 if (con->redirect_list == NULL) { /* Stick on head */
121
122 con->redirect_list = con->last_addr = redir_addr;
123
124 } else {
125
126 con->last_addr->next = redir_addr;
127 con->last_addr = redir_addr;
128
129 }
130
131 /* Now, make that connection */
132
133 if ((Client = RFCNB_IP_Connect(Dest_IP, port)) < 0) { /* Error */
134
135 /* No need to modify RFCNB_errno as it was done by RFCNB_IP_Connect */
136 free(con);
137 return (NULL);
138
139 }
140 con->fd = Client;
141
142 /* Now send and handle the RFCNB session request */
143 /* If we get a redirect, we will comeback with redirect true
144 * and a new IP address in DEST_IP */
145
146 if ((errno = RFCNB_Session_Req(con,
147 Called_Name,
148 Calling_Name,
149 &redirect, &Dest_IP, &port)) < 0) {
150
151 /* No need to modify RFCNB_errno as it was done by RFCNB_Session.. */
152
153 RFCNB_Close(con->fd); /* Close it */
154 free(con);
155 return (NULL);
156
157 }
158 if (redirect) {
159
160 /* We have to close the connection, and then try again */
161
162 (con->redirects)++;
163
164 RFCNB_Close(con->fd); /* Close it */
165
166 }
167 }
168
169 return (con);
170 }
171
172 /* We send a packet to the other end ... for the moment, we treat the
173 * data as a series of pointers to blocks of data ... we should check the
174 * length ... */
175 int
176 RFCNB_Send(struct RFCNB_Con *Con_Handle, struct RFCNB_Pkt *udata, int Length)
177 {
178 struct RFCNB_Pkt *pkt;
179 char *hdr;
180 int len;
181
182 /* Plug in the header and send the data */
183
184 pkt = RFCNB_Alloc_Pkt(RFCNB_Pkt_Hdr_Len);
185
186 if (pkt == NULL) {
187
188 RFCNB_errno = RFCNBE_NoSpace;
189 RFCNB_saved_errno = errno;
190 return (RFCNBE_Bad);
191
192 }
193 pkt->next = udata; /* The user data we want to send */
194
195 hdr = pkt->data;
196
197 /* Following crap is for portability across multiple UNIX machines */
198
199 *(hdr + RFCNB_Pkt_Type_Offset) = RFCNB_SESSION_MESSAGE;
200 RFCNB_Put_Pkt_Len(hdr, Length);
201
202 #ifdef RFCNB_DEBUG
203
204 fprintf(stderr, "Sending packet: ");
205
206 #endif
207
208 if ((len = RFCNB_Put_Pkt(Con_Handle, pkt, Length + RFCNB_Pkt_Hdr_Len)) < 0) {
209
210 /* No need to change RFCNB_errno as it was done by put_pkt ... */
211
212 RFCNB_Free_Pkt(pkt);
213 return (RFCNBE_Bad); /* Should be able to write that lot ... */
214
215 }
216 /* Now we have sent that lot, let's get rid of the RFCNB Header and return */
217
218 pkt->next = NULL;
219
220 RFCNB_Free_Pkt(pkt);
221
222 return (len);
223 }
224
225 /* We pick up a message from the internet ... We have to worry about
226 * non-message packets ... */
227 int
228 RFCNB_Recv(void *con_Handle, struct RFCNB_Pkt *Data, int Length)
229 {
230 struct RFCNB_Pkt *pkt;
231 // struct RFCNB_Hdr *hdr;
232 int ret_len;
233
234 if (con_Handle == NULL) {
235
236 RFCNB_errno = RFCNBE_BadHandle;
237 RFCNB_saved_errno = errno;
238 return (RFCNBE_Bad);
239
240 }
241 /* Now get a packet from below. We allocate a header first */
242
243 /* Plug in the header and send the data */
244
245 pkt = RFCNB_Alloc_Pkt(RFCNB_Pkt_Hdr_Len);
246
247 if (pkt == NULL) {
248
249 RFCNB_errno = RFCNBE_NoSpace;
250 RFCNB_saved_errno = errno;
251 return (RFCNBE_Bad);
252
253 }
254 pkt->next = Data; /* Plug in the data portion */
255
256 if ((ret_len = RFCNB_Get_Pkt(con_Handle, pkt, Length + RFCNB_Pkt_Hdr_Len)) < 0) {
257
258 #ifdef RFCNB_DEBUG
259 fprintf(stderr, "Bad packet return in RFCNB_Recv... \n");
260 #endif
261 RFCNB_Free_Pkt(pkt);
262 return (RFCNBE_Bad);
263
264 }
265 /* We should check that we go a message and not a keep alive */
266
267 pkt->next = NULL;
268
269 RFCNB_Free_Pkt(pkt);
270
271 return (ret_len);
272 }
273
274 /* We just disconnect from the other end, as there is nothing in the RFCNB */
275 /* protocol that specifies any exchange as far as I can see */
276 int
277 RFCNB_Hangup(struct RFCNB_Con *con_Handle)
278 {
279
280 if (con_Handle != NULL) {
281 RFCNB_Close(con_Handle->fd); /* Could this fail? */
282 free(con_Handle);
283 }
284 return 0;
285 }
286
287 /* Set TCP_NODELAY on the socket */
288 int
289 RFCNB_Set_Sock_NoDelay(struct RFCNB_Con *con_Handle, BOOL yn)
290 {
291
292 return (setsockopt(con_Handle->fd, IPPROTO_TCP, TCP_NODELAY,
293 (char *) &yn, sizeof(yn)));
294 }
295
296 #if NOT_IMPLEMENTED
297 /* Listen for a connection on a port???, when */
298 /* the connection comes in, we return with the connection */
299 void *
300 RFCNB_Listen()
301 {
302 }
303
304 #endif
305
306 /* Pick up the last error response as a string, hmmm, this routine should */
307 /* have been different ... */
308 void
309 RFCNB_Get_Error(char *buffer, int buf_len)
310 {
311
312 if (RFCNB_saved_errno <= 0) {
313 snprintf(buffer, (buf_len - 1), "%s", RFCNB_Error_Strings[RFCNB_errno]);
314 } else {
315 snprintf(buffer, (buf_len - 1), "%s\n\terrno:%s", RFCNB_Error_Strings[RFCNB_errno],
316 strerror(RFCNB_saved_errno));
317 }
318 }
319
320 /* Pick up the last error response and returns as a code */
321 int
322 RFCNB_Get_Last_Error()
323 {
324 return (RFCNB_errno);
325 }