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.
28
#include "std-includes.h"
29
#include "rfcnb-priv.h"
30
#include "rfcnb-util.h"
34
#include <arpa/inet.h>
38
typedef u_int16_t uint16;
40
typedef unsigned short uint16;
44
extern void (*Prot_Print_Routine) (); /* Pointer to protocol print routine */
46
/* Convert name and pad to 16 chars as needed */
47
/* Name 1 is a C string with null termination, name 2 may not be */
48
/* If SysName is true, then put a <00> on end, else space> */
51
RFCNB_CvtPad_Name(char *name1, char *name2)
58
for (i = 0; i < 16; i++) {
63
c2 = 'A'; /* CA is a space */
68
c1 = (char) ((int) c / 16 + (int) 'A');
69
c2 = (char) ((int) c % 16 + (int) 'A');
73
name2[i * 2 + 1] = c2;
77
name2[32] = 0; /* Put in the nll ... */
81
/* Converts an Ascii NB Name (16 chars) to an RFCNB Name (32 chars)
82
* Uses the encoding in RFC1001. Each nibble of byte is added to 'A'
83
* to produce the next byte in the name.
85
* This routine assumes that AName is 16 bytes long and that NBName has
86
* space for 32 chars, so be careful ...
91
RFCNB_AName_To_NBName(char *AName, char *NBName)
96
for (i = 0; i < 16; i++) {
100
c1 = (char) ((c >> 4) + 'A');
101
c2 = (char) ((c & 0xF) + 'A');
104
NBName[i * 2 + 1] = c2;
107
NBName[32] = 0; /* Put in a null */
111
/* Do the reverse of the above ... */
114
RFCNB_NBName_To_AName(char *NBName, char *AName)
119
for (i = 0; i < 16; i++) {
122
c2 = NBName[i * 2 + 1];
124
c = (char) (((int) c1 - (int) 'A') * 16 + ((int) c2 - (int) 'A'));
130
AName[i] = 0; /* Put a null on the end ... */
134
/* Print a string of bytes in HEX etc */
137
RFCNB_Print_Hex(FILE * fd, struct RFCNB_Pkt *pkt, int Offset, int Len)
139
char c1, c2, outbuf1[33];
142
struct RFCNB_Pkt *pkt_ptr = pkt;
143
static char Hex_List[17] = "0123456789ABCDEF";
147
/* We only want to print as much as sepcified in Len */
149
while (pkt_ptr != NULL) {
152
i < ((Len > (pkt_ptr->len) ? pkt_ptr->len : Len) - Offset);
155
c = pkt_ptr->data[i + Offset];
156
c1 = Hex_List[c >> 4];
157
c2 = Hex_List[c & 0xF];
162
if (j == 32) { /* Print and reset */
164
fprintf(fd, " %s\n", outbuf1);
170
Len = Len - pkt_ptr->len; /* Reduce amount by this much */
171
pkt_ptr = pkt_ptr->next;
175
/* Print last lot in the buffer ... */
180
fprintf(fd, " %s\n", outbuf1);
187
/* Get a packet of size n */
190
RFCNB_Alloc_Pkt(int n) {
193
if ((pkt = (struct RFCNB_Pkt *) malloc(sizeof(struct RFCNB_Pkt))) == NULL) {
195
RFCNB_errno = RFCNBE_NoSpace;
196
RFCNB_saved_errno = errno;
206
if ((pkt->data = (char *) malloc(n)) == NULL) {
208
RFCNB_errno = RFCNBE_NoSpace;
209
RFCNB_saved_errno = errno;
218
/* Free up a packet */
221
RFCNB_Free_Pkt(struct RFCNB_Pkt *pkt)
223
struct RFCNB_Pkt *pkt_next;
226
while (pkt != NULL) {
228
pkt_next = pkt->next;
230
data_ptr = pkt->data;
232
if (data_ptr != NULL)
243
/* Print an RFCNB packet */
246
RFCNB_Print_Pkt(FILE * fd, char *dirn, struct RFCNB_Pkt *pkt, int len)
250
/* We assume that the first fragment is the RFCNB Header */
251
/* We should loop through the fragments printing them out */
253
fprintf(fd, "RFCNB Pkt %s:", dirn);
255
switch (RFCNB_Pkt_Type(pkt->data)) {
257
case RFCNB_SESSION_MESSAGE:
259
fprintf(fd, "SESSION MESSAGE: Length = %i\n", RFCNB_Pkt_Len(pkt->data));
260
RFCNB_Print_Hex(fd, pkt, RFCNB_Pkt_Hdr_Len,
261
#ifdef RFCNB_PRINT_DATA
262
RFCNB_Pkt_Len(pkt->data) - RFCNB_Pkt_Hdr_Len);
267
if (Prot_Print_Routine != 0) { /* Print the rest of the packet */
269
Prot_Print_Routine(fd, strcmp(dirn, "sent"), pkt, RFCNB_Pkt_Hdr_Len,
270
RFCNB_Pkt_Len(pkt->data) - RFCNB_Pkt_Hdr_Len);
275
case RFCNB_SESSION_REQUEST:
277
fprintf(fd, "SESSION REQUEST: Length = %i\n",
278
RFCNB_Pkt_Len(pkt->data));
279
RFCNB_NBName_To_AName((char *) (pkt->data + RFCNB_Pkt_Called_Offset), lname);
280
fprintf(fd, " Called Name: %s\n", lname);
281
RFCNB_NBName_To_AName((char *) (pkt->data + RFCNB_Pkt_Calling_Offset), lname);
282
fprintf(fd, " Calling Name: %s\n", lname);
286
case RFCNB_SESSION_ACK:
288
fprintf(fd, "RFCNB SESSION ACK: Length = %i\n",
289
RFCNB_Pkt_Len(pkt->data));
293
case RFCNB_SESSION_REJ:
294
fprintf(fd, "RFCNB SESSION REJECT: Length = %i\n",
295
RFCNB_Pkt_Len(pkt->data));
297
if (RFCNB_Pkt_Len(pkt->data) < 1) {
298
fprintf(fd, " Protocol Error, short Reject packet!\n");
300
fprintf(fd, " Error = %x\n", CVAL(pkt->data, RFCNB_Pkt_Error_Offset));
305
case RFCNB_SESSION_RETARGET:
307
fprintf(fd, "RFCNB SESSION RETARGET: Length = %i\n",
308
RFCNB_Pkt_Len(pkt->data));
310
/* Print out the IP address etc and the port? */
314
case RFCNB_SESSION_KEEP_ALIVE:
316
fprintf(fd, "RFCNB SESSION KEEP ALIVE: Length = %i\n",
317
RFCNB_Pkt_Len(pkt->data));
327
/* 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 */
350
Dest_IP->s_addr = (*((struct in_addr*)hp->h_addr_list[0])).s_addr;
352
} else { /* It was an IP address */
353
Dest_IP->s_addr = addr;
360
/* Disconnect the TCP connection to the server */
363
RFCNB_Close(int aSocket)
368
/* If we want to do error recovery, here is where we put it */
374
/* Connect to the server specified in the IP address.
375
* 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;
410
/* handle the details of establishing the RFCNB session with remote
416
RFCNB_Session_Req(struct RFCNB_Con *con,
420
struct in_addr *Dest_IP,
425
/* Response packet should be no more than 9 bytes, make 16 jic */
429
struct RFCNB_Pkt *pkt, res_pkt;
431
/* We build and send the session request, then read the response */
433
pkt = RFCNB_Alloc_Pkt(RFCNB_Pkt_Sess_Len);
437
return (RFCNBE_Bad); /* Leave the error that RFCNB_Alloc_Pkt gives) */
440
sess_pkt = pkt->data; /* Get pointer to packet proper */
442
sess_pkt[RFCNB_Pkt_Type_Offset] = RFCNB_SESSION_REQUEST;
443
RFCNB_Put_Pkt_Len(sess_pkt, RFCNB_Pkt_Sess_Len - RFCNB_Pkt_Hdr_Len);
444
sess_pkt[RFCNB_Pkt_N1Len_Offset] = 32;
445
sess_pkt[RFCNB_Pkt_N2Len_Offset] = 32;
447
RFCNB_CvtPad_Name(Called_Name, (sess_pkt + RFCNB_Pkt_Called_Offset));
448
RFCNB_CvtPad_Name(Calling_Name, (sess_pkt + RFCNB_Pkt_Calling_Offset));
450
/* Now send the packet */
454
fprintf(stderr, "Sending packet: ");
458
if ((len = RFCNB_Put_Pkt(con, pkt, RFCNB_Pkt_Sess_Len)) < 0) {
460
return (RFCNBE_Bad); /* Should be able to write that lot ... */
465
fprintf(stderr, "Getting packet.\n");
470
res_pkt.len = sizeof(resp);
473
if ((len = RFCNB_Get_Pkt(con, &res_pkt, sizeof(resp))) < 0) {
478
/* Now analyze the packet ... */
480
switch (RFCNB_Pkt_Type(resp)) {
482
case RFCNB_SESSION_REJ: /* Didnt like us ... too bad */
484
/* Why did we get rejected ? */
486
switch (CVAL(resp, RFCNB_Pkt_Error_Offset)) {
489
RFCNB_errno = RFCNBE_CallRejNLOCN;
492
RFCNB_errno = RFCNBE_CallRejNLFCN;
495
RFCNB_errno = RFCNBE_CallRejCNNP;
498
RFCNB_errno = RFCNBE_CallRejInfRes;
501
RFCNB_errno = RFCNBE_CallRejUnSpec;
504
RFCNB_errno = RFCNBE_ProtErr;
511
case RFCNB_SESSION_ACK: /* Got what we wanted ... */
516
case RFCNB_SESSION_RETARGET: /* Go elsewhere */
518
*redirect = TRUE; /* Copy port and ip addr */
520
memcpy(Dest_IP, (resp + RFCNB_Pkt_IP_Offset), sizeof(struct in_addr));
521
*port = SVAL(resp, RFCNB_Pkt_Port_Offset);
526
default: /* A protocol error */
528
RFCNB_errno = RFCNBE_ProtErr;