]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfcnb/session.c
Portability fix: remove unneeded functions in lib/rfcnb
[thirdparty/squid.git] / lib / rfcnb / session.c
1 #include "config.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
83 /* Resolve that name into an IP address */
84
85 Service_Address = Called_Name;
86 if (strcmp(Called_Address, "") != 0) { /* If the Called Address = "" */
87 Service_Address = Called_Address;
88 }
89 if ((errno = RFCNB_Name_To_IP(Service_Address, &Dest_IP)) < 0) { /* Error */
90
91 /* No need to modify RFCNB_errno as it was done by RFCNB_Name_To_IP */
92 free(con);
93 return (NULL);
94
95 }
96 /* Now connect to the remote end */
97
98 redirect = TRUE; /* Fudge this one so we go once through */
99
100 while (redirect) { /* Connect and get session info etc */
101
102 redirect = FALSE; /* Assume all OK */
103
104 /* Build the redirect info. First one is first addr called */
105 /* And tack it onto the list of addresses we called */
106
107 if ((redir_addr = (struct redirect_addr *) malloc(sizeof(struct redirect_addr))) == NULL) { /* Could not get space */
108
109 RFCNB_errno = RFCNBE_NoSpace;
110 RFCNB_saved_errno = errno;
111 free(con);
112 return (NULL);
113
114 }
115 memcpy((char *) &(redir_addr->ip_addr), (char *) &Dest_IP, sizeof(Dest_IP));
116 redir_addr->port = port;
117 redir_addr->next = NULL;
118
119 if (con->redirect_list == NULL) { /* Stick on head */
120
121 con->redirect_list = con->last_addr = redir_addr;
122
123 } else {
124
125 con->last_addr->next = redir_addr;
126 con->last_addr = redir_addr;
127
128 }
129
130 /* Now, make that connection */
131
132 if ((Client = RFCNB_IP_Connect(Dest_IP, port)) < 0) { /* Error */
133
134 /* No need to modify RFCNB_errno as it was done by RFCNB_IP_Connect */
135 free(con);
136 return (NULL);
137
138 }
139 con->fd = Client;
140
141 /* Now send and handle the RFCNB session request */
142 /* If we get a redirect, we will comeback with redirect true
143 * and a new IP address in DEST_IP */
144
145 if ((errno = RFCNB_Session_Req(con,
146 Called_Name,
147 Calling_Name,
148 &redirect, &Dest_IP, &port)) < 0) {
149
150 /* No need to modify RFCNB_errno as it was done by RFCNB_Session.. */
151
152 RFCNB_Close(con->fd); /* Close it */
153 free(con);
154 return (NULL);
155
156 }
157 if (redirect) {
158
159 /* We have to close the connection, and then try again */
160
161 (con->redirects)++;
162
163 RFCNB_Close(con->fd); /* Close it */
164
165 }
166 }
167
168 return (con);
169 }
170
171 /* We send a packet to the other end ... for the moment, we treat the
172 * data as a series of pointers to blocks of data ... we should check the
173 * length ... */
174 int
175 RFCNB_Send(struct RFCNB_Con *Con_Handle, struct RFCNB_Pkt *udata, int Length)
176 {
177 struct RFCNB_Pkt *pkt;
178 char *hdr;
179 int len;
180
181 /* Plug in the header and send the data */
182
183 pkt = RFCNB_Alloc_Pkt(RFCNB_Pkt_Hdr_Len);
184
185 if (pkt == NULL) {
186
187 RFCNB_errno = RFCNBE_NoSpace;
188 RFCNB_saved_errno = errno;
189 return (RFCNBE_Bad);
190
191 }
192 pkt->next = udata; /* The user data we want to send */
193
194 hdr = pkt->data;
195
196 /* Following crap is for portability across multiple UNIX machines */
197
198 *(hdr + RFCNB_Pkt_Type_Offset) = RFCNB_SESSION_MESSAGE;
199 RFCNB_Put_Pkt_Len(hdr, Length);
200
201 #ifdef RFCNB_DEBUG
202
203 fprintf(stderr, "Sending packet: ");
204
205 #endif
206
207 if ((len = RFCNB_Put_Pkt(Con_Handle, pkt, Length + RFCNB_Pkt_Hdr_Len)) < 0) {
208
209 /* No need to change RFCNB_errno as it was done by put_pkt ... */
210
211 return (RFCNBE_Bad); /* Should be able to write that lot ... */
212
213 }
214 /* Now we have sent that lot, let's get rid of the RFCNB Header and return */
215
216 pkt->next = NULL;
217
218 RFCNB_Free_Pkt(pkt);
219
220 return (len);
221 }
222
223 /* We pick up a message from the internet ... We have to worry about
224 * non-message packets ... */
225 int
226 RFCNB_Recv(void *con_Handle, struct RFCNB_Pkt *Data, int Length)
227 {
228 struct RFCNB_Pkt *pkt;
229 // struct RFCNB_Hdr *hdr;
230 int ret_len;
231
232 if (con_Handle == NULL) {
233
234 RFCNB_errno = RFCNBE_BadHandle;
235 RFCNB_saved_errno = errno;
236 return (RFCNBE_Bad);
237
238 }
239 /* Now get a packet from below. We allocate a header first */
240
241 /* Plug in the header and send the data */
242
243 pkt = RFCNB_Alloc_Pkt(RFCNB_Pkt_Hdr_Len);
244
245 if (pkt == NULL) {
246
247 RFCNB_errno = RFCNBE_NoSpace;
248 RFCNB_saved_errno = errno;
249 return (RFCNBE_Bad);
250
251 }
252 pkt->next = Data; /* Plug in the data portion */
253
254 if ((ret_len = RFCNB_Get_Pkt(con_Handle, pkt, Length + RFCNB_Pkt_Hdr_Len)) < 0) {
255
256 #ifdef RFCNB_DEBUG
257 fprintf(stderr, "Bad packet return in RFCNB_Recv... \n");
258 #endif
259
260 return (RFCNBE_Bad);
261
262 }
263 /* We should check that we go a message and not a keep alive */
264
265 pkt->next = NULL;
266
267 RFCNB_Free_Pkt(pkt);
268
269 return (ret_len);
270 }
271
272 /* We just disconnect from the other end, as there is nothing in the RFCNB */
273 /* protocol that specifies any exchange as far as I can see */
274 int
275 RFCNB_Hangup(struct RFCNB_Con *con_Handle)
276 {
277
278 if (con_Handle != NULL) {
279 RFCNB_Close(con_Handle->fd); /* Could this fail? */
280 free(con_Handle);
281 }
282 return 0;
283 }
284
285 /* Set TCP_NODELAY on the socket */
286 int
287 RFCNB_Set_Sock_NoDelay(struct RFCNB_Con *con_Handle, BOOL yn)
288 {
289
290 return (setsockopt(con_Handle->fd, IPPROTO_TCP, TCP_NODELAY,
291 (char *) &yn, sizeof(yn)));
292 }
293
294 #if NOT_IMPLEMENTED
295 /* Listen for a connection on a port???, when */
296 /* the connection comes in, we return with the connection */
297 void *
298 RFCNB_Listen()
299 {
300 }
301
302 #endif
303
304 /* Pick up the last error response as a string, hmmm, this routine should */
305 /* have been different ... */
306 void
307 RFCNB_Get_Error(char *buffer, int buf_len)
308 {
309
310 if (RFCNB_saved_errno <= 0) {
311 snprintf(buffer, (buf_len - 1), "%s", RFCNB_Error_Strings[RFCNB_errno]);
312 } else {
313 snprintf(buffer, (buf_len - 1), "%s\n\terrno:%s", RFCNB_Error_Strings[RFCNB_errno],
314 strerror(RFCNB_saved_errno));
315 }
316 }
317
318 /* Pick up the last error response and returns as a code */
319 int
320 RFCNB_Get_Last_Error()
321 {
322 return (RFCNB_errno);
323 }