]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfcnb/rfcnb-util.c
3 /* UNIX RFCNB (RFC1001/RFC1002) NetBIOS implementation
6 * RFCNB Utility Routines ...
8 * Copyright (C) Richard Sharpe 1996
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.
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.
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.
28 #include "rfcnb/rfcnb.h"
29 #include "rfcnb/std-includes.h"
30 #include "rfcnb/rfcnb-priv.h"
31 #include "rfcnb/rfcnb-util.h"
32 #include "rfcnb/rfcnb-io.h"
35 #include <arpa/inet.h>
41 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 ..."
61 /* Convert name and pad to 16 chars as needed */
62 /* Name 1 is a C string with null termination, name 2 may not be */
63 /* If SysName is true, then put a <00> on end, else space> */
65 RFCNB_CvtPad_Name(char *name1
, char *name2
)
72 for (i
= 0; i
< 16; i
++) {
77 c2
= 'A'; /* CA is a space */
82 c1
= (char) ((int) c
/ 16 + (int) 'A');
83 c2
= (char) ((int) c
% 16 + (int) 'A');
87 name2
[i
* 2 + 1] = c2
;
91 name2
[32] = 0; /* Put in the nll ... */
95 /* Converts an Ascii NB Name (16 chars) to an RFCNB Name (32 chars)
96 * Uses the encoding in RFC1001. Each nibble of byte is added to 'A'
97 * to produce the next byte in the name.
99 * This routine assumes that AName is 16 bytes long and that NBName has
100 * space for 32 chars, so be careful ...
103 RFCNB_AName_To_NBName(char *AName
, char *NBName
)
108 for (i
= 0; i
< 16; i
++) {
112 c1
= (char) ((c
>> 4) + 'A');
113 c2
= (char) ((c
& 0xF) + 'A');
116 NBName
[i
* 2 + 1] = c2
;
119 NBName
[32] = 0; /* Put in a null */
122 /* Do the reverse of the above ... */
124 RFCNB_NBName_To_AName(char *NBName
, char *AName
)
129 for (i
= 0; i
< 16; i
++) {
132 c2
= NBName
[i
* 2 + 1];
134 c
= (char) (((int) c1
- (int) 'A') * 16 + ((int) c2
- (int) 'A'));
140 AName
[i
] = 0; /* Put a null on the end ... */
143 /* Print a string of bytes in HEX etc */
145 RFCNB_Print_Hex(FILE * fd
, struct RFCNB_Pkt
*pkt
, int Offset
, int Len
)
147 char c1
, c2
, outbuf1
[33];
150 struct RFCNB_Pkt
*pkt_ptr
= pkt
;
151 static char Hex_List
[17] = "0123456789ABCDEF";
155 /* We only want to print as much as sepcified in Len */
157 while (pkt_ptr
!= NULL
) {
160 i
< ((Len
> (pkt_ptr
->len
) ? pkt_ptr
->len
: Len
) - Offset
);
163 c
= pkt_ptr
->data
[i
+ Offset
];
164 c1
= Hex_List
[c
>> 4];
165 c2
= Hex_List
[c
& 0xF];
170 if (j
== 32) { /* Print and reset */
172 fprintf(fd
, " %s\n", outbuf1
);
178 Len
= Len
- pkt_ptr
->len
; /* Reduce amount by this much */
179 pkt_ptr
= pkt_ptr
->next
;
183 /* Print last lot in the buffer ... */
188 fprintf(fd
, " %s\n", outbuf1
);
194 /* Get a packet of size n */
196 RFCNB_Alloc_Pkt(int n
) {
199 if ((pkt
= (struct RFCNB_Pkt
*) malloc(sizeof(struct RFCNB_Pkt
))) == NULL
) {
201 RFCNB_errno
= RFCNBE_NoSpace
;
202 RFCNB_saved_errno
= errno
;
212 if ((pkt
->data
= (char *) malloc(n
)) == NULL
) {
214 RFCNB_errno
= RFCNBE_NoSpace
;
215 RFCNB_saved_errno
= errno
;
223 /* Free up a packet */
225 RFCNB_Free_Pkt(struct RFCNB_Pkt
*pkt
)
227 struct RFCNB_Pkt
*pkt_next
;
230 while (pkt
!= NULL
) {
232 pkt_next
= pkt
->next
;
234 data_ptr
= pkt
->data
;
236 if (data_ptr
!= NULL
)
246 /* Print an RFCNB packet */
248 RFCNB_Print_Pkt(FILE * fd
, char *dirn
, struct RFCNB_Pkt
*pkt
, int len
)
252 /* We assume that the first fragment is the RFCNB Header */
253 /* We should loop through the fragments printing them out */
255 fprintf(fd
, "RFCNB Pkt %s:", dirn
);
257 switch (RFCNB_Pkt_Type(pkt
->data
)) {
259 case RFCNB_SESSION_MESSAGE
:
261 fprintf(fd
, "SESSION MESSAGE: Length = %i\n", RFCNB_Pkt_Len(pkt
->data
));
262 RFCNB_Print_Hex(fd
, pkt
, RFCNB_Pkt_Hdr_Len
,
263 #ifdef RFCNB_PRINT_DATA
264 RFCNB_Pkt_Len(pkt
->data
) - RFCNB_Pkt_Hdr_Len
);
269 if (Prot_Print_Routine
) { /* Print the rest of the packet */
271 Prot_Print_Routine(fd
, strcmp(dirn
, "sent"), pkt
, RFCNB_Pkt_Hdr_Len
,
272 RFCNB_Pkt_Len(pkt
->data
) - RFCNB_Pkt_Hdr_Len
);
277 case RFCNB_SESSION_REQUEST
:
279 fprintf(fd
, "SESSION REQUEST: Length = %i\n",
280 RFCNB_Pkt_Len(pkt
->data
));
281 RFCNB_NBName_To_AName((char *) (pkt
->data
+ RFCNB_Pkt_Called_Offset
), lname
);
282 fprintf(fd
, " Called Name: %s\n", lname
);
283 RFCNB_NBName_To_AName((char *) (pkt
->data
+ RFCNB_Pkt_Calling_Offset
), lname
);
284 fprintf(fd
, " Calling Name: %s\n", lname
);
288 case RFCNB_SESSION_ACK
:
290 fprintf(fd
, "RFCNB SESSION ACK: Length = %i\n",
291 RFCNB_Pkt_Len(pkt
->data
));
295 case RFCNB_SESSION_REJ
:
296 fprintf(fd
, "RFCNB SESSION REJECT: Length = %i\n",
297 RFCNB_Pkt_Len(pkt
->data
));
299 if (RFCNB_Pkt_Len(pkt
->data
) < 1) {
300 fprintf(fd
, " Protocol Error, short Reject packet!\n");
302 fprintf(fd
, " Error = %x\n", CVAL(pkt
->data
, RFCNB_Pkt_Error_Offset
));
307 case RFCNB_SESSION_RETARGET
:
309 fprintf(fd
, "RFCNB SESSION RETARGET: Length = %i\n",
310 RFCNB_Pkt_Len(pkt
->data
));
312 /* Print out the IP address etc and the port? */
316 case RFCNB_SESSION_KEEP_ALIVE
:
318 fprintf(fd
, "RFCNB SESSION KEEP ALIVE: Length = %i\n",
319 RFCNB_Pkt_Len(pkt
->data
));
328 /* Resolve a name into an address */
330 RFCNB_Name_To_IP(char *host
, struct in_addr
*Dest_IP
)
332 int addr
; /* Assumes IP4, 32 bit network addresses */
335 /* Use inet_addr to try to convert the address */
337 if ((addr
= inet_addr(host
)) == INADDR_NONE
) { /* Oh well, a good try :-) */
339 /* Now try a name look up with gethostbyname */
341 if ((hp
= gethostbyname(host
)) == NULL
) { /* Not in DNS */
343 /* Try NetBIOS name lookup, how the hell do we do that? */
345 RFCNB_errno
= RFCNBE_BadName
; /* Is this right? */
346 RFCNB_saved_errno
= errno
;
349 } else { /* We got a name */
351 memcpy((void *) Dest_IP
, (void *) hp
->h_addr_list
[0], sizeof(struct in_addr
));
354 } else { /* It was an IP address */
356 memcpy((void *) Dest_IP
, (void *) &addr
, sizeof(struct in_addr
));
363 /* Disconnect the TCP connection to the server */
365 RFCNB_Close(int socket
)
370 /* If we want to do error recovery, here is where we put it */
375 /* Connect to the server specified in the IP address.
376 * Not sure how to handle socket options etc. */
378 RFCNB_IP_Connect(struct in_addr Dest_IP
, int port
)
380 struct sockaddr_in Socket
;
383 /* Create a socket */
385 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) { /* Handle the error */
387 RFCNB_errno
= RFCNBE_BadSocket
;
388 RFCNB_saved_errno
= errno
;
391 memset((char *) &Socket
, 0, sizeof(Socket
));
392 memcpy((char *) &Socket
.sin_addr
, (char *) &Dest_IP
, sizeof(Dest_IP
));
394 Socket
.sin_port
= htons(port
);
395 Socket
.sin_family
= PF_INET
;
397 /* Now connect to the destination */
399 if (connect(fd
, (struct sockaddr
*) &Socket
, sizeof(Socket
)) < 0) { /* Error */
402 RFCNB_errno
= RFCNBE_ConnectFailed
;
403 RFCNB_saved_errno
= errno
;
409 /* handle the details of establishing the RFCNB session with remote
413 RFCNB_Session_Req(struct RFCNB_Con
*con
,
417 struct in_addr
*Dest_IP
,
422 /* Response packet should be no more than 9 bytes, make 16 jic */
424 // char ln1[16], ln2[16], n1[32], n2[32];
427 struct RFCNB_Pkt
*pkt
, res_pkt
;
429 /* We build and send the session request, then read the response */
431 pkt
= RFCNB_Alloc_Pkt(RFCNB_Pkt_Sess_Len
);
434 return (RFCNBE_Bad
); /* Leave the error that RFCNB_Alloc_Pkt gives) */
436 sess_pkt
= pkt
->data
; /* Get pointer to packet proper */
438 sess_pkt
[RFCNB_Pkt_Type_Offset
] = RFCNB_SESSION_REQUEST
;
439 RFCNB_Put_Pkt_Len(sess_pkt
, RFCNB_Pkt_Sess_Len
- RFCNB_Pkt_Hdr_Len
);
440 sess_pkt
[RFCNB_Pkt_N1Len_Offset
] = 32;
441 sess_pkt
[RFCNB_Pkt_N2Len_Offset
] = 32;
443 RFCNB_CvtPad_Name(Called_Name
, (sess_pkt
+ RFCNB_Pkt_Called_Offset
));
444 RFCNB_CvtPad_Name(Calling_Name
, (sess_pkt
+ RFCNB_Pkt_Calling_Offset
));
446 /* Now send the packet */
449 fprintf(stderr
, "Sending packet: ");
452 if ((len
= RFCNB_Put_Pkt(con
, pkt
, RFCNB_Pkt_Sess_Len
)) < 0) {
454 return (RFCNBE_Bad
); /* Should be able to write that lot ... */
458 fprintf(stderr
, "Getting packet.\n");
462 res_pkt
.len
= sizeof(resp
);
465 if ((len
= RFCNB_Get_Pkt(con
, &res_pkt
, sizeof(resp
))) < 0) {
470 /* Now analyze the packet ... */
472 switch (RFCNB_Pkt_Type(resp
)) {
474 case RFCNB_SESSION_REJ
: /* Didnt like us ... too bad */
476 /* Why did we get rejected ? */
478 switch (CVAL(resp
, RFCNB_Pkt_Error_Offset
)) {
481 RFCNB_errno
= RFCNBE_CallRejNLOCN
;
484 RFCNB_errno
= RFCNBE_CallRejNLFCN
;
487 RFCNB_errno
= RFCNBE_CallRejCNNP
;
490 RFCNB_errno
= RFCNBE_CallRejInfRes
;
493 RFCNB_errno
= RFCNBE_CallRejUnSpec
;
496 RFCNB_errno
= RFCNBE_ProtErr
;
503 case RFCNB_SESSION_ACK
: /* Got what we wanted ... */
508 case RFCNB_SESSION_RETARGET
: /* Go elsewhere */
510 *redirect
= TRUE
; /* Copy port and ip addr */
512 memcpy(Dest_IP
, (resp
+ RFCNB_Pkt_IP_Offset
), sizeof(struct in_addr
));
513 *port
= SVAL(resp
, RFCNB_Pkt_Port_Offset
);
518 default: /* A protocol error */
520 RFCNB_errno
= RFCNBE_ProtErr
;