]>
git.ipfire.org Git - thirdparty/squid.git/blob - helpers/basic_auth/MSNT/rfcnb-util.c
1 /* UNIX RFCNB (RFC1001/RFC1002) NetBIOS implementation
4 * RFCNB Utility Routines ...
6 * Copyright (C) Richard Sharpe 1996
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "std-includes.h"
27 #include "rfcnb-priv.h"
28 #include "rfcnb-util.h"
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
39 const char *RFCNB_Error_Strings
[] =
42 "RFCNBE_OK: Routine completed successfully.",
43 "RFCNBE_NoSpace: No space available for a malloc call.",
44 "RFCNBE_BadName: NetBIOS name could not be translated to IP address.",
45 "RFCNBE_BadRead: Read system call returned an error. Check errno.",
46 "RFCNBE_BadWrite: Write system call returned an error. Check errno.",
47 "RFCNBE_ProtErr: A protocol error has occurred.",
48 "RFCNBE_ConGone: Connection dropped during a read or write system call.",
49 "RFCNBE_BadHandle: Bad connection handle passed.",
50 "RFCNBE_BadSocket: Problems creating socket.",
51 "RFCNBE_ConnectFailed: Connection failed. See errno.",
52 "RFCNBE_CallRejNLOCN: Call rejected. Not listening on called name.",
53 "RFCNBE_CallRejNLFCN: Call rejected. Not listening for called name.",
54 "RFCNBE_CallRejCNNP: Call rejected. Called name not present.",
55 "RFCNBE_CallRejInfRes: Call rejected. Name present, but insufficient resources.",
56 "RFCNBE_CallRejUnSpec: Call rejected. Unspecified error.",
57 "RFCNBE_BadParam: Bad parameters passed to a routine.",
58 "RFCNBE_Timeout: IO Operation timed out ..."
63 extern void (*Prot_Print_Routine
) (); /* Pointer to protocol print routine */
66 /* Convert name and pad to 16 chars as needed */
67 /* Name 1 is a C string with null termination, name 2 may not be */
68 /* If SysName is true, then put a <00> on end, else space> */
71 RFCNB_CvtPad_Name(char *name1
, char *name2
)
78 for (i
= 0; i
< 16; i
++) {
83 c2
= 'A'; /* CA is a space */
88 c1
= (char) ((int) c
/ 16 + (int) 'A');
89 c2
= (char) ((int) c
% 16 + (int) 'A');
93 name2
[i
* 2 + 1] = c2
;
97 name2
[32] = 0; /* Put in the nll ... */
101 /* Converts an Ascii NB Name (16 chars) to an RFCNB Name (32 chars)
102 * Uses the encoding in RFC1001. Each nibble of byte is added to 'A'
103 * to produce the next byte in the name.
105 * This routine assumes that AName is 16 bytes long and that NBName has
106 * space for 32 chars, so be careful ...
111 RFCNB_AName_To_NBName(char *AName
, char *NBName
)
116 for (i
= 0; i
< 16; i
++) {
120 c1
= (char) ((c
>> 4) + 'A');
121 c2
= (char) ((c
& 0xF) + 'A');
124 NBName
[i
* 2 + 1] = c2
;
127 NBName
[32] = 0; /* Put in a null */
131 /* Do the reverse of the above ... */
134 RFCNB_NBName_To_AName(char *NBName
, char *AName
)
139 for (i
= 0; i
< 16; i
++) {
142 c2
= NBName
[i
* 2 + 1];
144 c
= (char) (((int) c1
- (int) 'A') * 16 + ((int) c2
- (int) 'A'));
150 AName
[i
] = 0; /* Put a null on the end ... */
155 /* Print a string of bytes in HEX etc */
158 RFCNB_Print_Hex(FILE * fd
, struct RFCNB_Pkt
*pkt
, int Offset
, int Len
)
160 char c1
, c2
, outbuf1
[33];
163 struct RFCNB_Pkt
*pkt_ptr
= pkt
;
164 static char Hex_List
[17] = "0123456789ABCDEF";
168 /* We only want to print as much as sepcified in Len */
170 while (pkt_ptr
!= NULL
) {
173 i
< ((Len
> (pkt_ptr
->len
) ? pkt_ptr
->len
: Len
) - Offset
);
176 c
= pkt_ptr
->data
[i
+ Offset
];
177 c1
= Hex_List
[c
>> 4];
178 c2
= Hex_List
[c
& 0xF];
183 if (j
== 32) { /* Print and reset */
185 fprintf(fd
, " %s\n", outbuf1
);
191 Len
= Len
- pkt_ptr
->len
; /* Reduce amount by this much */
192 pkt_ptr
= pkt_ptr
->next
;
196 /* Print last lot in the buffer ... */
201 fprintf(fd
, " %s\n", outbuf1
);
209 /* Get a packet of size n */
212 RFCNB_Alloc_Pkt(int n
)
216 if ((pkt
= malloc(sizeof(struct RFCNB_Pkt
))) == NULL
) {
217 RFCNB_errno
= RFCNBE_NoSpace
;
218 RFCNB_saved_errno
= errno
;
227 if ((pkt
->data
= malloc(n
)) == NULL
) {
228 RFCNB_errno
= RFCNBE_NoSpace
;
229 RFCNB_saved_errno
= errno
;
237 /* Free up a packet */
240 RFCNB_Free_Pkt(struct RFCNB_Pkt
*pkt
)
242 struct RFCNB_Pkt
*pkt_next
;
244 while (pkt
!= NULL
) {
246 pkt_next
= pkt
->next
;
248 if (pkt
->data
!= NULL
)
260 /* Print an RFCNB packet */
263 RFCNB_Print_Pkt(FILE * fd
, char *dirn
, struct RFCNB_Pkt
*pkt
, int len
)
267 /* We assume that the first fragment is the RFCNB Header */
268 /* We should loop through the fragments printing them out */
270 fprintf(fd
, "RFCNB Pkt %s:", dirn
);
272 switch (RFCNB_Pkt_Type(pkt
->data
)) {
274 case RFCNB_SESSION_MESSAGE
:
276 fprintf(fd
, "SESSION MESSAGE: Length = %i\n", RFCNB_Pkt_Len(pkt
->data
));
277 RFCNB_Print_Hex(fd
, pkt
, RFCNB_Pkt_Hdr_Len
,
278 #ifdef RFCNB_PRINT_DATA
279 RFCNB_Pkt_Len(pkt
->data
) - RFCNB_Pkt_Hdr_Len
);
284 if (Prot_Print_Routine
!= 0) { /* Print the rest of the packet */
286 Prot_Print_Routine(fd
, strcmp(dirn
, "sent"), pkt
, RFCNB_Pkt_Hdr_Len
,
287 RFCNB_Pkt_Len(pkt
->data
) - RFCNB_Pkt_Hdr_Len
);
292 case RFCNB_SESSION_REQUEST
:
294 fprintf(fd
, "SESSION REQUEST: Length = %i\n",
295 RFCNB_Pkt_Len(pkt
->data
));
296 RFCNB_NBName_To_AName((char *) (pkt
->data
+ RFCNB_Pkt_Called_Offset
), lname
);
297 fprintf(fd
, " Called Name: %s\n", lname
);
298 RFCNB_NBName_To_AName((char *) (pkt
->data
+ RFCNB_Pkt_Calling_Offset
), lname
);
299 fprintf(fd
, " Calling Name: %s\n", lname
);
303 case RFCNB_SESSION_ACK
:
305 fprintf(fd
, "RFCNB SESSION ACK: Length = %i\n",
306 RFCNB_Pkt_Len(pkt
->data
));
310 case RFCNB_SESSION_REJ
:
311 fprintf(fd
, "RFCNB SESSION REJECT: Length = %i\n",
312 RFCNB_Pkt_Len(pkt
->data
));
314 if (RFCNB_Pkt_Len(pkt
->data
) < 1) {
315 fprintf(fd
, " Protocol Error, short Reject packet!\n");
317 fprintf(fd
, " Error = %x\n", CVAL(pkt
->data
, RFCNB_Pkt_Error_Offset
));
322 case RFCNB_SESSION_RETARGET
:
324 fprintf(fd
, "RFCNB SESSION RETARGET: Length = %i\n",
325 RFCNB_Pkt_Len(pkt
->data
));
327 /* Print out the IP address etc and the port? */
331 case RFCNB_SESSION_KEEP_ALIVE
:
333 fprintf(fd
, "RFCNB SESSION KEEP ALIVE: Length = %i\n",
334 RFCNB_Pkt_Len(pkt
->data
));
345 /* Resolve a name into an address */
348 RFCNB_Name_To_IP(char *host
, struct in_addr
*Dest_IP
)
350 int addr
; /* Assumes IP4, 32 bit network addresses */
353 /* Use inet_addr to try to convert the address */
355 if ((addr
= inet_addr(host
)) == INADDR_NONE
) { /* Oh well, a good try :-) */
357 /* Now try a name look up with gethostbyname */
359 if ((hp
= gethostbyname(host
)) == NULL
) { /* Not in DNS */
361 /* Try NetBIOS name lookup, how the hell do we do that? */
362 RFCNB_errno
= RFCNBE_BadName
; /* Is this right? */
363 RFCNB_saved_errno
= errno
;
366 } else { /* We got a name */
367 Dest_IP
->s_addr
= (*((struct in_addr
*)hp
->h_addr_list
[0])).s_addr
;
369 } else { /* It was an IP address */
370 Dest_IP
->s_addr
= addr
;
377 /* Disconnect the TCP connection to the server */
380 RFCNB_Close(int socket
)
385 /* If we want to do error recovery, here is where we put it */
391 /* Connect to the server specified in the IP address.
392 * Not sure how to handle socket options etc. */
395 RFCNB_IP_Connect(struct in_addr Dest_IP
, int port
)
397 struct sockaddr_in Socket
;
400 /* Create a socket */
402 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) { /* Handle the error */
404 RFCNB_errno
= RFCNBE_BadSocket
;
405 RFCNB_saved_errno
= errno
;
408 memset((char *) &Socket
, 0, sizeof(Socket
));
409 memcpy((char *) &Socket
.sin_addr
, (char *) &Dest_IP
, sizeof(Dest_IP
));
411 Socket
.sin_port
= htons(port
);
412 Socket
.sin_family
= PF_INET
;
414 /* Now connect to the destination */
416 if (connect(fd
, (struct sockaddr
*) &Socket
, sizeof(Socket
)) < 0) { /* Error */
419 RFCNB_errno
= RFCNBE_ConnectFailed
;
420 RFCNB_saved_errno
= errno
;
427 /* handle the details of establishing the RFCNB session with remote
433 RFCNB_Session_Req(struct RFCNB_Con
*con
,
437 struct in_addr
*Dest_IP
,
442 /* Response packet should be no more than 9 bytes, make 16 jic */
446 struct RFCNB_Pkt
*pkt
, res_pkt
;
448 /* We build and send the session request, then read the response */
450 pkt
= RFCNB_Alloc_Pkt(RFCNB_Pkt_Sess_Len
);
454 return (RFCNBE_Bad
); /* Leave the error that RFCNB_Alloc_Pkt gives) */
457 sess_pkt
= pkt
->data
; /* Get pointer to packet proper */
459 sess_pkt
[RFCNB_Pkt_Type_Offset
] = RFCNB_SESSION_REQUEST
;
460 RFCNB_Put_Pkt_Len(sess_pkt
, (RFCNB_Pkt_Sess_Len
- RFCNB_Pkt_Hdr_Len
));
461 sess_pkt
[RFCNB_Pkt_N1Len_Offset
] = 32;
462 sess_pkt
[RFCNB_Pkt_N2Len_Offset
] = 32;
464 RFCNB_CvtPad_Name(Called_Name
, (sess_pkt
+ RFCNB_Pkt_Called_Offset
));
465 RFCNB_CvtPad_Name(Calling_Name
, (sess_pkt
+ RFCNB_Pkt_Calling_Offset
));
467 /* Now send the packet */
471 fprintf(stderr
, "Sending packet: ");
475 if ((len
= RFCNB_Put_Pkt(con
, pkt
, RFCNB_Pkt_Sess_Len
)) < 0) {
477 return (RFCNBE_Bad
); /* Should be able to write that lot ... */
482 fprintf(stderr
, "Getting packet.\n");
487 res_pkt
.len
= sizeof(resp
);
490 if ((len
= RFCNB_Get_Pkt(con
, &res_pkt
, sizeof(resp
))) < 0) {
495 /* Now analyze the packet ... */
497 switch (RFCNB_Pkt_Type(resp
)) {
499 case RFCNB_SESSION_REJ
: /* Didnt like us ... too bad */
501 /* Why did we get rejected ? */
503 switch (CVAL(resp
, RFCNB_Pkt_Error_Offset
)) {
506 RFCNB_errno
= RFCNBE_CallRejNLOCN
;
509 RFCNB_errno
= RFCNBE_CallRejNLFCN
;
512 RFCNB_errno
= RFCNBE_CallRejCNNP
;
515 RFCNB_errno
= RFCNBE_CallRejInfRes
;
518 RFCNB_errno
= RFCNBE_CallRejUnSpec
;
521 RFCNB_errno
= RFCNBE_ProtErr
;
528 case RFCNB_SESSION_ACK
: /* Got what we wanted ... */
533 case RFCNB_SESSION_RETARGET
: /* Go elsewhere */
535 *redirect
= TRUE
; /* Copy port and ip addr */
537 memcpy(Dest_IP
, (resp
+ RFCNB_Pkt_IP_Offset
), sizeof(struct in_addr
));
538 *port
= SVAL(resp
, RFCNB_Pkt_Port_Offset
);
543 default: /* A protocol error */
545 RFCNB_errno
= RFCNBE_ProtErr
;