Deleted Added
full compact
alias_nbt.c (97627) alias_nbt.c (99207)
1/*-
2 * Written by Atsushi Murai <amurai@spec.co.jp>
3 * Copyright (c) 1998, System Planning and Engineering Co.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 10 unchanged lines hidden (view full) ---

19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 * TODO:
1/*-
2 * Written by Atsushi Murai <amurai@spec.co.jp>
3 * Copyright (c) 1998, System Planning and Engineering Co.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 10 unchanged lines hidden (view full) ---

19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 * TODO:
27 * oClean up.
27 * oClean up.
28 * oConsidering for word alignment for other platform.
29 */
30
31#include <sys/cdefs.h>
28 * oConsidering for word alignment for other platform.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/netinet/libalias/alias_nbt.c 97627 2002-05-30 20:53:45Z wollman $");
32__FBSDID("$FreeBSD: head/sys/netinet/libalias/alias_nbt.c 99207 2002-07-01 11:19:40Z brian $");
33
34/*
35 alias_nbt.c performs special processing for NetBios over TCP/IP
36 sessions by UDP.
37
38 Initial version: May, 1998 (Atsushi Murai <amurai@spec.co.jp>)
39
40 See HISTORY file for record of revisions.
41*/
42
43/* Includes */
44#include <ctype.h>
33
34/*
35 alias_nbt.c performs special processing for NetBios over TCP/IP
36 sessions by UDP.
37
38 Initial version: May, 1998 (Atsushi Murai <amurai@spec.co.jp>)
39
40 See HISTORY file for record of revisions.
41*/
42
43/* Includes */
44#include <ctype.h>
45#include <stdio.h>
45#include
46#include <string.h>
47#include <sys/types.h>
48#include <netinet/in_systm.h>
49#include <netinet/in.h>
50#include <arpa/inet.h>
51#include <netinet/ip.h>
52#include <netinet/udp.h>
53#include <netinet/tcp.h>

--- 55 unchanged lines hidden (view full) ---

109 printf("\nRefused error.\n");
110 case ACT_ERR:
111 printf("\nActive error.\n");
112 case CFT_ERR:
113 printf("\nName in conflict error.\n");
114 default:
115 printf("\n?%c?=%0x\n", '?', rcode );
116
46#include <string.h>
47#include <sys/types.h>
48#include <netinet/in_systm.h>
49#include <netinet/in.h>
50#include <arpa/inet.h>
51#include <netinet/ip.h>
52#include <netinet/udp.h>
53#include <netinet/tcp.h>

--- 55 unchanged lines hidden (view full) ---

109 printf("\nRefused error.\n");
110 case ACT_ERR:
111 printf("\nActive error.\n");
112 case CFT_ERR:
113 printf("\nName in conflict error.\n");
114 default:
115 printf("\n?%c?=%0x\n", '?', rcode );
116
117 }
117 }
118}
119#endif
120
121
122/* Handling Name field */
123static u_char *AliasHandleName ( u_char *p, char *pmax ) {
124
125 u_char *s;

--- 12 unchanged lines hidden (view full) ---

138 return ((u_char *)p);
139 }
140 while ( ( *p & 0x3f) != 0x00 ) {
141 s = p + 1;
142 if ( *p == 0x20 )
143 compress = 1;
144 else
145 compress = 0;
118}
119#endif
120
121
122/* Handling Name field */
123static u_char *AliasHandleName ( u_char *p, char *pmax ) {
124
125 u_char *s;

--- 12 unchanged lines hidden (view full) ---

138 return ((u_char *)p);
139 }
140 while ( ( *p & 0x3f) != 0x00 ) {
141 s = p + 1;
142 if ( *p == 0x20 )
143 compress = 1;
144 else
145 compress = 0;
146
146
147 /* Get next length field */
148 p = (u_char *)(p + (*p & 0x3f) + 1);
149 if ((char *)p > pmax) {
150 p = NULL;
151 break;
152 }
153#ifdef DEBUG
154 printf(":");

--- 24 unchanged lines hidden (view full) ---

179 /* Set up to out of Name field */
180 if (p == NULL || (char *)p >= pmax)
181 p = NULL;
182 else
183 p++;
184 return ((u_char *)p);
185}
186
147 /* Get next length field */
148 p = (u_char *)(p + (*p & 0x3f) + 1);
149 if ((char *)p > pmax) {
150 p = NULL;
151 break;
152 }
153#ifdef DEBUG
154 printf(":");

--- 24 unchanged lines hidden (view full) ---

179 /* Set up to out of Name field */
180 if (p == NULL || (char *)p >= pmax)
181 p = NULL;
182 else
183 p++;
184 return ((u_char *)p);
185}
186
187/*
187/*
188 * NetBios Datagram Handler (IP/UDP)
189 */
190#define DGM_DIRECT_UNIQ 0x10
191#define DGM_DIRECT_GROUP 0x11
192#define DGM_BROADCAST 0x12
193#define DGM_ERROR 0x13
194#define DGM_QUERY 0x14
195#define DGM_POSITIVE_RES 0x15

--- 4 unchanged lines hidden (view full) ---

200 struct alias_link *link,
201 struct in_addr *alias_address,
202 u_short alias_port
203) {
204 struct udphdr * uh;
205 NbtDataHeader *ndh;
206 u_char *p = NULL;
207 char *pmax;
188 * NetBios Datagram Handler (IP/UDP)
189 */
190#define DGM_DIRECT_UNIQ 0x10
191#define DGM_DIRECT_GROUP 0x11
192#define DGM_BROADCAST 0x12
193#define DGM_ERROR 0x13
194#define DGM_QUERY 0x14
195#define DGM_POSITIVE_RES 0x15

--- 4 unchanged lines hidden (view full) ---

200 struct alias_link *link,
201 struct in_addr *alias_address,
202 u_short alias_port
203) {
204 struct udphdr * uh;
205 NbtDataHeader *ndh;
206 u_char *p = NULL;
207 char *pmax;
208
208
209 /* Calculate data length of UDP packet */
210 uh = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));
211 pmax = (char *)uh + ntohs( uh->uh_ulen );
212
213 ndh = (NbtDataHeader *)((char *)uh + (sizeof (struct udphdr)));
214 if ((char *)(ndh + 1) > pmax)
215 return(-1);
216#ifdef DEBUG

--- 106 unchanged lines hidden (view full) ---

323
324#define SizeOfNsRNB 6
325typedef struct {
326 u_short g:1, ont:2, resv:13;
327 struct in_addr addr;
328} NBTNsRNB;
329
330static u_char *
209 /* Calculate data length of UDP packet */
210 uh = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));
211 pmax = (char *)uh + ntohs( uh->uh_ulen );
212
213 ndh = (NbtDataHeader *)((char *)uh + (sizeof (struct udphdr)));
214 if ((char *)(ndh + 1) > pmax)
215 return(-1);
216#ifdef DEBUG

--- 106 unchanged lines hidden (view full) ---

323
324#define SizeOfNsRNB 6
325typedef struct {
326 u_short g:1, ont:2, resv:13;
327 struct in_addr addr;
328} NBTNsRNB;
329
330static u_char *
331AliasHandleResourceNB(
331AliasHandleResourceNB(
332 NBTNsResource *q,
332 NBTNsResource *q,
333 char *pmax,
333 char *pmax,
334 NBTArguments *nbtarg)
335{
336 NBTNsRNB *nb;
337 u_short bcount;
338
339 if (q == NULL || (char *)(q + 1) > pmax)
340 return(NULL);
341 /* Check out a length */

--- 50 unchanged lines hidden (view full) ---

392}
393
394#define SizeOfResourceA 6
395typedef struct {
396 struct in_addr addr;
397} NBTNsResourceA;
398
399static u_char *
334 NBTArguments *nbtarg)
335{
336 NBTNsRNB *nb;
337 u_short bcount;
338
339 if (q == NULL || (char *)(q + 1) > pmax)
340 return(NULL);
341 /* Check out a length */

--- 50 unchanged lines hidden (view full) ---

392}
393
394#define SizeOfResourceA 6
395typedef struct {
396 struct in_addr addr;
397} NBTNsResourceA;
398
399static u_char *
400AliasHandleResourceA(
400AliasHandleResourceA(
401 NBTNsResource *q,
402 char *pmax,
403 NBTArguments *nbtarg)
404{
405 NBTNsResourceA *a;
406 u_short bcount;
407
408 if (q == NULL || (char *)(q + 1) > pmax)

--- 40 unchanged lines hidden (view full) ---

449 return ((u_char *)a);
450}
451
452typedef struct {
453 u_short opcode:4, flags:8, resv:4;
454} NBTNsResourceNULL;
455
456static u_char *
401 NBTNsResource *q,
402 char *pmax,
403 NBTArguments *nbtarg)
404{
405 NBTNsResourceA *a;
406 u_short bcount;
407
408 if (q == NULL || (char *)(q + 1) > pmax)

--- 40 unchanged lines hidden (view full) ---

449 return ((u_char *)a);
450}
451
452typedef struct {
453 u_short opcode:4, flags:8, resv:4;
454} NBTNsResourceNULL;
455
456static u_char *
457AliasHandleResourceNULL(
458 NBTNsResource *q,
457AliasHandleResourceNULL(
458 NBTNsResource *q,
459 char *pmax,
460 NBTArguments *nbtarg)
461{
462 NBTNsResourceNULL *n;
463 u_short bcount;
464
465 if (q == NULL || (char *)(q + 1) > pmax)
466 return(NULL);

--- 15 unchanged lines hidden (view full) ---

482 }
483 if ((char *)(n + 1) > pmax)
484 n = NULL;
485
486 return ((u_char *)n);
487}
488
489static u_char *
459 char *pmax,
460 NBTArguments *nbtarg)
461{
462 NBTNsResourceNULL *n;
463 u_short bcount;
464
465 if (q == NULL || (char *)(q + 1) > pmax)
466 return(NULL);

--- 15 unchanged lines hidden (view full) ---

482 }
483 if ((char *)(n + 1) > pmax)
484 n = NULL;
485
486 return ((u_char *)n);
487}
488
489static u_char *
490AliasHandleResourceNS(
490AliasHandleResourceNS(
491 NBTNsResource *q,
492 char *pmax,
493 NBTArguments *nbtarg)
494{
495 NBTNsResourceNULL *n;
496 u_short bcount;
497
498 if (q == NULL || (char *)(q + 1) > pmax)

--- 39 unchanged lines hidden (view full) ---

538 if (q == NULL || (char *)((u_char *)n + bcount) > pmax)
539 return(NULL);
540 else
541 return ((u_char *)n + bcount);
542}
543
544static u_char *
545AliasHandleResource(
491 NBTNsResource *q,
492 char *pmax,
493 NBTArguments *nbtarg)
494{
495 NBTNsResourceNULL *n;
496 u_short bcount;
497
498 if (q == NULL || (char *)(q + 1) > pmax)

--- 39 unchanged lines hidden (view full) ---

538 if (q == NULL || (char *)((u_char *)n + bcount) > pmax)
539 return(NULL);
540 else
541 return ((u_char *)n + bcount);
542}
543
544static u_char *
545AliasHandleResource(
546 u_short count,
546 u_short count,
547 NBTNsResource *q,
548 char *pmax,
547 NBTNsResource *q,
548 char *pmax,
549 NBTArguments
549 NBTArguments
550 *nbtarg)
551{
552 while ( count != 0 ) {
553 /* Resource Record Name Filed */
554 q = (NBTNsResource *)AliasHandleName( (u_char *)q, pmax );
555
556 if (q == NULL || (char *)(q + 1) > pmax)
557 break;
558#ifdef DEBUG
559 printf("type=%02x, count=%d\n", ntohs(q->type), count );
560#endif
561
562 /* Type and Class filed */
563 switch ( ntohs(q->type) ) {
564 case RR_TYPE_NB:
550 *nbtarg)
551{
552 while ( count != 0 ) {
553 /* Resource Record Name Filed */
554 q = (NBTNsResource *)AliasHandleName( (u_char *)q, pmax );
555
556 if (q == NULL || (char *)(q + 1) > pmax)
557 break;
558#ifdef DEBUG
559 printf("type=%02x, count=%d\n", ntohs(q->type), count );
560#endif
561
562 /* Type and Class filed */
563 switch ( ntohs(q->type) ) {
564 case RR_TYPE_NB:
565 q = (NBTNsResource *)AliasHandleResourceNB(
565 q = (NBTNsResource *)AliasHandleResourceNB(
566 q,
567 pmax,
566 q,
567 pmax,
568 nbtarg
568 nbtarg
569 );
570 break;
569 );
570 break;
571 case RR_TYPE_A:
572 q = (NBTNsResource *)AliasHandleResourceA(
573 q,
574 pmax,
571 case RR_TYPE_A:
572 q = (NBTNsResource *)AliasHandleResourceA(
573 q,
574 pmax,
575 nbtarg
576 );
577 break;
578 case RR_TYPE_NS:
575 nbtarg
576 );
577 break;
578 case RR_TYPE_NS:
579 q = (NBTNsResource *)AliasHandleResourceNS(
579 q = (NBTNsResource *)AliasHandleResourceNS(
580 q,
580 q,
581 pmax,
582 nbtarg
581 pmax,
582 nbtarg
583 );
584 break;
585 case RR_TYPE_NULL:
583 );
584 break;
585 case RR_TYPE_NULL:
586 q = (NBTNsResource *)AliasHandleResourceNULL(
587 q,
588 pmax,
589 nbtarg
586 q = (NBTNsResource *)AliasHandleResourceNULL(
587 q,
588 pmax,
589 nbtarg
590 );
591 break;
592 case RR_TYPE_NBSTAT:
593 q = (NBTNsResource *)AliasHandleResourceNBSTAT(
594 q,
590 );
591 break;
592 case RR_TYPE_NBSTAT:
593 q = (NBTNsResource *)AliasHandleResourceNBSTAT(
594 q,
595 pmax,
595 pmax,
596 nbtarg
597 );
598 break;
596 nbtarg
597 );
598 break;
599 default:
599 default:
600#ifdef DEBUG
601 printf(
600#ifdef DEBUG
601 printf(
602 "\nUnknown Type of Resource %0x\n",
603 ntohs(q->type)
602 "\nUnknown Type of Resource %0x\n",
603 ntohs(q->type)
604 );
605#endif
606 break;
607 }
608 count--;
609 }
610 fflush(stdout);
611 return ((u_char *)q);

--- 8 unchanged lines hidden (view full) ---

620 u_short *original_port )
621{
622 struct udphdr * uh;
623 NbtNSHeader * nsh;
624 u_char * p;
625 char *pmax;
626 NBTArguments nbtarg;
627
604 );
605#endif
606 break;
607 }
608 count--;
609 }
610 fflush(stdout);
611 return ((u_char *)q);

--- 8 unchanged lines hidden (view full) ---

620 u_short *original_port )
621{
622 struct udphdr * uh;
623 NbtNSHeader * nsh;
624 u_char * p;
625 char *pmax;
626 NBTArguments nbtarg;
627
628 /* Set up Common Parameter */
628 /* Set up Common Parameter */
629 nbtarg.oldaddr = *alias_address;
630 nbtarg.oldport = *alias_port;
631 nbtarg.newaddr = *original_address;
632 nbtarg.newport = *original_port;
633
634 /* Calculate data length of UDP packet */
635 uh = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));
636 nbtarg.uh_sum = &(uh->uh_sum);
637 nsh = (NbtNSHeader *)((char *)uh + (sizeof(struct udphdr)));
638 p = (u_char *)(nsh + 1);
639 pmax = (char *)uh + ntohs( uh->uh_ulen );
640
641 if ((char *)(nsh + 1) > pmax)
642 return(-1);
643
644#ifdef DEBUG
645 printf(" [%s] ID=%02x, op=%01x, flag=%02x, rcode=%01x, qd=%04x"
629 nbtarg.oldaddr = *alias_address;
630 nbtarg.oldport = *alias_port;
631 nbtarg.newaddr = *original_address;
632 nbtarg.newport = *original_port;
633
634 /* Calculate data length of UDP packet */
635 uh = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));
636 nbtarg.uh_sum = &(uh->uh_sum);
637 nsh = (NbtNSHeader *)((char *)uh + (sizeof(struct udphdr)));
638 p = (u_char *)(nsh + 1);
639 pmax = (char *)uh + ntohs( uh->uh_ulen );
640
641 if ((char *)(nsh + 1) > pmax)
642 return(-1);
643
644#ifdef DEBUG
645 printf(" [%s] ID=%02x, op=%01x, flag=%02x, rcode=%01x, qd=%04x"
646 ", an=%04x, ns=%04x, ar=%04x, [%d]-->",
646 ", an=%04x, ns=%04x, ar=%04x, [%d]-->",
647 nsh->dir ? "Response": "Request",
648 nsh->nametrid,
649 nsh->opcode,
650 nsh->nmflags,
651 nsh->rcode,
652 ntohs(nsh->qdcount),
653 ntohs(nsh->ancount),
654 ntohs(nsh->nscount),
655 ntohs(nsh->arcount),
656 (u_char *)p -(u_char *)nsh
657 );
658#endif
659
660 /* Question Entries */
661 if (ntohs(nsh->qdcount) !=0 ) {
662 p = AliasHandleQuestion(
663 ntohs(nsh->qdcount),
664 (NBTNsQuestion *)p,
647 nsh->dir ? "Response": "Request",
648 nsh->nametrid,
649 nsh->opcode,
650 nsh->nmflags,
651 nsh->rcode,
652 ntohs(nsh->qdcount),
653 ntohs(nsh->ancount),
654 ntohs(nsh->nscount),
655 ntohs(nsh->arcount),
656 (u_char *)p -(u_char *)nsh
657 );
658#endif
659
660 /* Question Entries */
661 if (ntohs(nsh->qdcount) !=0 ) {
662 p = AliasHandleQuestion(
663 ntohs(nsh->qdcount),
664 (NBTNsQuestion *)p,
665 pmax,
666 &nbtarg
665 pmax,
666 &nbtarg
667 );
668 }
669
670 /* Answer Resource Records */
671 if (ntohs(nsh->ancount) !=0 ) {
672 p = AliasHandleResource(
673 ntohs(nsh->ancount),
674 (NBTNsResource *)p,
667 );
668 }
669
670 /* Answer Resource Records */
671 if (ntohs(nsh->ancount) !=0 ) {
672 p = AliasHandleResource(
673 ntohs(nsh->ancount),
674 (NBTNsResource *)p,
675 pmax,
676 &nbtarg
675 pmax,
676 &nbtarg
677 );
678 }
679
680 /* Authority Resource Recodrs */
681 if (ntohs(nsh->nscount) !=0 ) {
682 p = AliasHandleResource(
677 );
678 }
679
680 /* Authority Resource Recodrs */
681 if (ntohs(nsh->nscount) !=0 ) {
682 p = AliasHandleResource(
683 ntohs(nsh->nscount),
683 ntohs(nsh->nscount),
684 (NBTNsResource *)p,
684 (NBTNsResource *)p,
685 pmax,
686 &nbtarg
685 pmax,
686 &nbtarg
687 );
688 }
689
690 /* Additional Resource Recodrs */
691 if (ntohs(nsh->arcount) !=0 ) {
692 p = AliasHandleResource(
693 ntohs(nsh->arcount),
694 (NBTNsResource *)p,
687 );
688 }
689
690 /* Additional Resource Recodrs */
691 if (ntohs(nsh->arcount) !=0 ) {
692 p = AliasHandleResource(
693 ntohs(nsh->arcount),
694 (NBTNsResource *)p,
695 pmax,
696 &nbtarg
695 pmax,
696 &nbtarg
697 );
698 }
699
700#ifdef DEBUG
701 PrintRcode(nsh->rcode);
702#endif
703 return ((p == NULL) ? -1 : 0);
704}
697 );
698 }
699
700#ifdef DEBUG
701 PrintRcode(nsh->rcode);
702#endif
703 return ((p == NULL) ? -1 : 0);
704}