VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/libalias/alias_nbt.c@ 22451

最後變更 在這個檔案從22451是 22451,由 vboxsync 提交於 16 年 前

NAT/libalias: no-static

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 22.0 KB
 
1/*-
2 * Written by Atsushi Murai <[email protected]>
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:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
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.
28 * oConsidering for word alignment for other platform.
29 */
30
31#ifndef VBOX
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: src/sys/netinet/libalias/alias_nbt.c,v 1.20.8.1 2009/04/15 03:14:26 kensmith Exp $");
34
35/*
36 alias_nbt.c performs special processing for NetBios over TCP/IP
37 sessions by UDP.
38
39 Initial version: May, 1998 (Atsushi Murai <[email protected]>)
40
41 See HISTORY file for record of revisions.
42*/
43
44/* Includes */
45#ifdef _KERNEL
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/module.h>
50#else
51#include <errno.h>
52#include <sys/types.h>
53#include <stdio.h>
54#endif
55
56#include <netinet/in_systm.h>
57#include <netinet/in.h>
58#include <netinet/ip.h>
59#include <netinet/udp.h>
60
61#ifdef _KERNEL
62#include <netinet/libalias/alias_local.h>
63#include <netinet/libalias/alias_mod.h>
64#else
65#include "alias_local.h"
66#include "alias_mod.h"
67#endif
68#else /*VBOX*/
69# include <iprt/ctype.h>
70# include <slirp.h>
71# include "alias_local.h"
72# include "alias_mod.h"
73#endif /*VBOX*/
74
75#define NETBIOS_NS_PORT_NUMBER 137
76#define NETBIOS_DGM_PORT_NUMBER 138
77
78static int
79AliasHandleUdpNbt(struct libalias *, struct ip *, struct alias_link *,
80 struct in_addr *, u_short);
81
82static int
83AliasHandleUdpNbtNS(struct libalias *, struct ip *, struct alias_link *,
84 struct in_addr *, u_short *, struct in_addr *, u_short *);
85static int
86fingerprint1(struct libalias *la, struct ip *pip, struct alias_data *ah)
87{
88
89 if (ah->dport == NULL || ah->sport == NULL || ah->lnk == NULL ||
90 ah->aaddr == NULL || ah->aport == NULL)
91 return (-1);
92 if (ntohs(*ah->dport) == NETBIOS_DGM_PORT_NUMBER
93 || ntohs(*ah->sport) == NETBIOS_DGM_PORT_NUMBER)
94 return (0);
95 return (-1);
96}
97
98static int
99protohandler1(struct libalias *la, struct ip *pip, struct alias_data *ah)
100{
101
102 AliasHandleUdpNbt(la, pip, ah->lnk, ah->aaddr, *ah->aport);
103 return (0);
104}
105
106static int
107fingerprint2(struct libalias *la, struct ip *pip, struct alias_data *ah)
108{
109
110 if (ah->dport == NULL || ah->sport == NULL || ah->lnk == NULL ||
111 ah->aaddr == NULL || ah->aport == NULL)
112 return (-1);
113 if (ntohs(*ah->dport) == NETBIOS_NS_PORT_NUMBER
114 || ntohs(*ah->sport) == NETBIOS_NS_PORT_NUMBER)
115 return (0);
116 return (-1);
117}
118
119static int
120protohandler2in(struct libalias *la, struct ip *pip, struct alias_data *ah)
121{
122
123 AliasHandleUdpNbtNS(la, pip, ah->lnk, ah->aaddr, ah->aport,
124 ah->oaddr, ah->dport);
125 return (0);
126}
127
128static int
129protohandler2out(struct libalias *la, struct ip *pip, struct alias_data *ah)
130{
131
132 AliasHandleUdpNbtNS(la, pip, ah->lnk, &pip->ip_src, ah->sport,
133 ah->aaddr, ah->aport);
134 return (0);
135}
136
137/* Kernel module definition. */
138#ifndef VBOX
139struct proto_handler handlers[] = {
140 {
141 .pri = 130,
142 .dir = IN|OUT,
143 .proto = UDP,
144 .fingerprint = &fingerprint1,
145 .protohandler = &protohandler1
146 },
147 {
148 .pri = 140,
149 .dir = IN,
150 .proto = UDP,
151 .fingerprint = &fingerprint2,
152 .protohandler = &protohandler2in
153 },
154 {
155 .pri = 140,
156 .dir = OUT,
157 .proto = UDP,
158 .fingerprint = &fingerprint2,
159 .protohandler = &protohandler2out
160 },
161 { EOH }
162};
163#else /* !VBOX */
164#define handlers pData->nbt_module
165#endif /*VBOX*/
166
167#ifndef VBOX
168static int
169mod_handler(module_t mod, int type, void *data)
170#else /*!VBOX*/
171static int nbt_alias_handler(PNATState pData, int type);
172
173int
174nbt_alias_load(PNATState pData)
175{
176 return nbt_alias_handler(pData, MOD_LOAD);
177}
178
179int
180nbt_alias_unload(PNATState pData)
181{
182 return nbt_alias_handler(pData, MOD_UNLOAD);
183}
184static int
185nbt_alias_handler(PNATState pData, int type)
186#endif /*VBOX*/
187{
188 int error;
189#ifdef VBOX
190 handlers = RTMemAllocZ(4 * sizeof(struct proto_handler));
191 handlers[0].pri = 130;
192 handlers[0].dir = IN|OUT;
193 handlers[0].proto = UDP;
194 handlers[0].fingerprint = &fingerprint1;
195 handlers[0].protohandler = &protohandler1;
196
197
198 handlers[1].pri = 140;
199 handlers[1].dir = IN;
200 handlers[1].proto = UDP;
201 handlers[1].fingerprint = &fingerprint2;
202 handlers[1].protohandler = &protohandler2in;
203
204
205 handlers[2].pri = 140;
206 handlers[2].dir = OUT;
207 handlers[2].proto = UDP;
208 handlers[2].fingerprint = &fingerprint2;
209 handlers[2].protohandler = &protohandler2out;
210
211 handlers[3].pri = EOH;
212#endif /*VBOX*/
213
214 switch (type) {
215 case MOD_LOAD:
216 error = 0;
217#ifdef VBOX
218 LibAliasAttachHandlers(pData, handlers);
219#else
220 LibAliasAttachHandlers(handlers);
221#endif
222 break;
223 case MOD_UNLOAD:
224 error = 0;
225#ifdef VBOX
226 LibAliasDetachHandlers(pData, handlers);
227 RTMemFree(handlers);
228 handlers = NULL;
229#else
230 LibAliasDetachHandlers(handlers);
231#endif
232 break;
233 default:
234 error = EINVAL;
235 }
236 return (error);
237}
238
239#ifndef VBOX
240#ifdef _KERNEL
241static
242#endif
243moduledata_t alias_mod = {
244 "alias_nbt", mod_handler, NULL
245};
246#endif /*!VBOX*/
247
248#ifdef _KERNEL
249DECLARE_MODULE(alias_nbt, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
250MODULE_VERSION(alias_nbt, 1);
251MODULE_DEPEND(alias_nbt, libalias, 1, 1, 1);
252#endif
253
254typedef struct {
255 struct in_addr oldaddr;
256 u_short oldport;
257 struct in_addr newaddr;
258 u_short newport;
259 u_short *uh_sum;
260} NBTArguments;
261
262typedef struct {
263 unsigned char type;
264 unsigned char flags;
265 u_short id;
266 struct in_addr source_ip;
267 u_short source_port;
268 u_short len;
269 u_short offset;
270} NbtDataHeader;
271
272#define OpQuery 0
273#define OpUnknown 4
274#define OpRegist 5
275#define OpRelease 6
276#define OpWACK 7
277#define OpRefresh 8
278typedef struct {
279 u_short nametrid;
280 u_short dir: 1, opcode:4, nmflags:7, rcode:4;
281 u_short qdcount;
282 u_short ancount;
283 u_short nscount;
284 u_short arcount;
285} NbtNSHeader;
286
287#define FMT_ERR 0x1
288#define SRV_ERR 0x2
289#define IMP_ERR 0x4
290#define RFS_ERR 0x5
291#define ACT_ERR 0x6
292#define CFT_ERR 0x7
293
294
295#ifdef LIBALIAS_DEBUG
296static void
297PrintRcode(u_char rcode)
298{
299
300 switch (rcode) {
301 case FMT_ERR:
302 printf("\nFormat Error.");
303 case SRV_ERR:
304 printf("\nSever failure.");
305 case IMP_ERR:
306 printf("\nUnsupported request error.\n");
307 case RFS_ERR:
308 printf("\nRefused error.\n");
309 case ACT_ERR:
310 printf("\nActive error.\n");
311 case CFT_ERR:
312 printf("\nName in conflict error.\n");
313 default:
314 printf("\n?%c?=%0x\n", '?', rcode);
315
316 }
317}
318
319#endif
320
321
322/* Handling Name field */
323static u_char *
324AliasHandleName(u_char * p, char *pmax)
325{
326
327 u_char *s;
328 u_char c;
329 int compress;
330
331 /* Following length field */
332
333 if (p == NULL || (char *)p >= pmax)
334 return (NULL);
335
336 if (*p & 0xc0) {
337 p = p + 2;
338 if ((char *)p > pmax)
339 return (NULL);
340 return ((u_char *) p);
341 }
342 while ((*p & 0x3f) != 0x00) {
343 s = p + 1;
344 if (*p == 0x20)
345 compress = 1;
346 else
347 compress = 0;
348
349 /* Get next length field */
350 p = (u_char *) (p + (*p & 0x3f) + 1);
351 if ((char *)p > pmax) {
352 p = NULL;
353 break;
354 }
355#ifdef LIBALIAS_DEBUG
356 printf(":");
357#endif
358 while (s < p) {
359 if (compress == 1) {
360 c = (u_char) (((((*s & 0x0f) << 4) | (*(s + 1) & 0x0f)) - 0x11));
361#ifdef LIBALIAS_DEBUG
362 if (isprint(c))
363 printf("%c", c);
364 else
365 printf("<0x%02x>", c);
366#endif
367 s += 2;
368 } else {
369#ifdef LIBALIAS_DEBUG
370 printf("%c", *s);
371#endif
372 s++;
373 }
374 }
375#ifdef LIBALIAS_DEBUG
376 printf(":");
377 fflush(stdout);
378#endif
379 }
380
381 /* Set up to out of Name field */
382 if (p == NULL || (char *)p >= pmax)
383 p = NULL;
384 else
385 p++;
386 return ((u_char *) p);
387}
388
389/*
390 * NetBios Datagram Handler (IP/UDP)
391 */
392#define DGM_DIRECT_UNIQ 0x10
393#define DGM_DIRECT_GROUP 0x11
394#define DGM_BROADCAST 0x12
395#define DGM_ERROR 0x13
396#define DGM_QUERY 0x14
397#define DGM_POSITIVE_RES 0x15
398#define DGM_NEGATIVE_RES 0x16
399
400static int
401AliasHandleUdpNbt(
402 struct libalias *la,
403 struct ip *pip, /* IP packet to examine/patch */
404 struct alias_link *lnk,
405 struct in_addr *alias_address,
406 u_short alias_port
407)
408{
409 struct udphdr *uh;
410 NbtDataHeader *ndh;
411 u_char *p = NULL;
412 char *pmax;
413
414 (void)la;
415 (void)lnk;
416
417 /* Calculate data length of UDP packet */
418 uh = (struct udphdr *)ip_next(pip);
419 pmax = (char *)uh + ntohs(uh->uh_ulen);
420
421 ndh = (NbtDataHeader *)udp_next(uh);
422 if ((char *)(ndh + 1) > pmax)
423 return (-1);
424#ifdef LIBALIAS_DEBUG
425 printf("\nType=%02x,", ndh->type);
426#endif
427 switch (ndh->type) {
428 case DGM_DIRECT_UNIQ:
429 case DGM_DIRECT_GROUP:
430 case DGM_BROADCAST:
431 p = (u_char *) ndh + 14;
432 p = AliasHandleName(p, pmax); /* Source Name */
433 p = AliasHandleName(p, pmax); /* Destination Name */
434 break;
435 case DGM_ERROR:
436 p = (u_char *) ndh + 11;
437 break;
438 case DGM_QUERY:
439 case DGM_POSITIVE_RES:
440 case DGM_NEGATIVE_RES:
441 p = (u_char *) ndh + 10;
442 p = AliasHandleName(p, pmax); /* Destination Name */
443 break;
444 }
445 if (p == NULL || (char *)p > pmax)
446 p = NULL;
447#ifdef LIBALIAS_DEBUG
448 printf("%s:%d-->", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port));
449#endif
450 /* Doing an IP address and Port number Translation */
451 if (uh->uh_sum != 0) {
452 int acc;
453 u_short *sptr;
454
455 acc = ndh->source_port;
456 acc -= alias_port;
457 sptr = (u_short *) & (ndh->source_ip);
458 acc += *sptr++;
459 acc += *sptr;
460 sptr = (u_short *) alias_address;
461 acc -= *sptr++;
462 acc -= *sptr;
463 ADJUST_CHECKSUM(acc, uh->uh_sum);
464 }
465 ndh->source_ip = *alias_address;
466 ndh->source_port = alias_port;
467#ifdef LIBALIAS_DEBUG
468 printf("%s:%d\n", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port));
469 fflush(stdout);
470#endif
471 return ((p == NULL) ? -1 : 0);
472}
473
474/* Question Section */
475#define QS_TYPE_NB 0x0020
476#define QS_TYPE_NBSTAT 0x0021
477#define QS_CLAS_IN 0x0001
478typedef struct {
479 u_short type; /* The type of Request */
480 u_short class; /* The class of Request */
481} NBTNsQuestion;
482
483static u_char *
484AliasHandleQuestion(
485 u_short count,
486 NBTNsQuestion * q,
487 char *pmax,
488 NBTArguments * nbtarg)
489{
490
491 (void)nbtarg;
492
493 while (count != 0) {
494 /* Name Filed */
495 q = (NBTNsQuestion *) AliasHandleName((u_char *) q, pmax);
496
497 if (q == NULL || (char *)(q + 1) > pmax) {
498 q = NULL;
499 break;
500 }
501 /* Type and Class filed */
502 switch (ntohs(q->type)) {
503 case QS_TYPE_NB:
504 case QS_TYPE_NBSTAT:
505 q = q + 1;
506 break;
507 default:
508#ifdef LIBALIAS_DEBUG
509 printf("\nUnknown Type on Question %0x\n", ntohs(q->type));
510#endif
511 break;
512 }
513 count--;
514 }
515
516 /* Set up to out of Question Section */
517 return ((u_char *) q);
518}
519
520/* Resource Record */
521#define RR_TYPE_A 0x0001
522#define RR_TYPE_NS 0x0002
523#define RR_TYPE_NULL 0x000a
524#define RR_TYPE_NB 0x0020
525#define RR_TYPE_NBSTAT 0x0021
526#define RR_CLAS_IN 0x0001
527#define SizeOfNsResource 8
528typedef struct {
529 u_short type;
530 u_short class;
531 unsigned int ttl;
532 u_short rdlen;
533} NBTNsResource;
534
535#define SizeOfNsRNB 6
536typedef struct {
537 u_short g: 1 , ont:2, resv:13;
538 struct in_addr addr;
539} NBTNsRNB;
540
541static u_char *
542AliasHandleResourceNB(
543 NBTNsResource * q,
544 char *pmax,
545 NBTArguments * nbtarg)
546{
547 NBTNsRNB *nb;
548 u_short bcount;
549
550 if (q == NULL || (char *)(q + 1) > pmax)
551 return (NULL);
552 /* Check out a length */
553 bcount = ntohs(q->rdlen);
554
555 /* Forward to Resource NB position */
556 nb = (NBTNsRNB *) ((u_char *) q + SizeOfNsResource);
557
558 /* Processing all in_addr array */
559#ifdef LIBALIAS_DEBUG
560 printf("NB rec[%s", inet_ntoa(nbtarg->oldaddr));
561 printf("->%s, %dbytes] ", inet_ntoa(nbtarg->newaddr), bcount);
562#endif
563 while (nb != NULL && bcount != 0) {
564 if ((char *)(nb + 1) > pmax) {
565 nb = NULL;
566 break;
567 }
568#ifdef LIBALIAS_DEBUG
569 printf("<%s>", inet_ntoa(nb->addr));
570#endif
571 if (!bcmp(&nbtarg->oldaddr, &nb->addr, sizeof(struct in_addr))) {
572 if (*nbtarg->uh_sum != 0) {
573 int acc;
574 u_short *sptr;
575
576 sptr = (u_short *) & (nb->addr);
577 acc = *sptr++;
578 acc += *sptr;
579 sptr = (u_short *) & (nbtarg->newaddr);
580 acc -= *sptr++;
581 acc -= *sptr;
582 ADJUST_CHECKSUM(acc, *nbtarg->uh_sum);
583 }
584 nb->addr = nbtarg->newaddr;
585#ifdef LIBALIAS_DEBUG
586 printf("O");
587#endif
588 }
589#ifdef LIBALIAS_DEBUG
590 else {
591 printf(".");
592 }
593#endif
594 nb = (NBTNsRNB *) ((u_char *) nb + SizeOfNsRNB);
595 bcount -= SizeOfNsRNB;
596 }
597 if (nb == NULL || (char *)(nb + 1) > pmax) {
598 nb = NULL;
599 }
600 return ((u_char *) nb);
601}
602
603#define SizeOfResourceA 6
604typedef struct {
605 struct in_addr addr;
606} NBTNsResourceA;
607
608static u_char *
609AliasHandleResourceA(
610 NBTNsResource * q,
611 char *pmax,
612 NBTArguments * nbtarg)
613{
614 NBTNsResourceA *a;
615 u_short bcount;
616
617 if (q == NULL || (char *)(q + 1) > pmax)
618 return (NULL);
619
620 /* Forward to Resource A position */
621 a = (NBTNsResourceA *) ((u_char *) q + sizeof(NBTNsResource));
622
623 /* Check out of length */
624 bcount = ntohs(q->rdlen);
625
626 /* Processing all in_addr array */
627#ifdef LIBALIAS_DEBUG
628 printf("Arec [%s", inet_ntoa(nbtarg->oldaddr));
629 printf("->%s]", inet_ntoa(nbtarg->newaddr));
630#endif
631 while (bcount != 0) {
632 if (a == NULL || (char *)(a + 1) > pmax)
633 return (NULL);
634#ifdef LIBALIAS_DEBUG
635 printf("..%s", inet_ntoa(a->addr));
636#endif
637 if (!bcmp(&nbtarg->oldaddr, &a->addr, sizeof(struct in_addr))) {
638 if (*nbtarg->uh_sum != 0) {
639 int acc;
640 u_short *sptr;
641
642 sptr = (u_short *) & (a->addr); /* Old */
643 acc = *sptr++;
644 acc += *sptr;
645 sptr = (u_short *) & nbtarg->newaddr; /* New */
646 acc -= *sptr++;
647 acc -= *sptr;
648 ADJUST_CHECKSUM(acc, *nbtarg->uh_sum);
649 }
650 a->addr = nbtarg->newaddr;
651 }
652 a++; /* XXXX */
653 bcount -= SizeOfResourceA;
654 }
655 if (a == NULL || (char *)(a + 1) > pmax)
656 a = NULL;
657 return ((u_char *) a);
658}
659
660typedef struct {
661 u_short opcode:4, flags:8, resv:4;
662} NBTNsResourceNULL;
663
664static u_char *
665AliasHandleResourceNULL(
666 NBTNsResource * q,
667 char *pmax,
668 NBTArguments * nbtarg)
669{
670 NBTNsResourceNULL *n;
671 u_short bcount;
672
673 (void)nbtarg;
674
675 if (q == NULL || (char *)(q + 1) > pmax)
676 return (NULL);
677
678 /* Forward to Resource NULL position */
679 n = (NBTNsResourceNULL *) ((u_char *) q + sizeof(NBTNsResource));
680
681 /* Check out of length */
682 bcount = ntohs(q->rdlen);
683
684 /* Processing all in_addr array */
685 while (bcount != 0) {
686 if ((char *)(n + 1) > pmax) {
687 n = NULL;
688 break;
689 }
690 n++;
691 bcount -= sizeof(NBTNsResourceNULL);
692 }
693 if ((char *)(n + 1) > pmax)
694 n = NULL;
695
696 return ((u_char *) n);
697}
698
699static u_char *
700AliasHandleResourceNS(
701 NBTNsResource * q,
702 char *pmax,
703 NBTArguments * nbtarg)
704{
705 NBTNsResourceNULL *n;
706 u_short bcount;
707
708 (void)nbtarg;
709
710 if (q == NULL || (char *)(q + 1) > pmax)
711 return (NULL);
712
713 /* Forward to Resource NULL position */
714 n = (NBTNsResourceNULL *) ((u_char *) q + sizeof(NBTNsResource));
715
716 /* Check out of length */
717 bcount = ntohs(q->rdlen);
718
719 /* Resource Record Name Filed */
720 q = (NBTNsResource *) AliasHandleName((u_char *) n, pmax); /* XXX */
721
722 if (q == NULL || (char *)((u_char *) n + bcount) > pmax)
723 return (NULL);
724 else
725 return ((u_char *) n + bcount);
726}
727
728typedef struct {
729 u_short numnames;
730} NBTNsResourceNBSTAT;
731
732static u_char *
733AliasHandleResourceNBSTAT(
734 NBTNsResource * q,
735 char *pmax,
736 NBTArguments * nbtarg)
737{
738 NBTNsResourceNBSTAT *n;
739 u_short bcount;
740
741 (void)nbtarg;
742
743 if (q == NULL || (char *)(q + 1) > pmax)
744 return (NULL);
745
746 /* Forward to Resource NBSTAT position */
747 n = (NBTNsResourceNBSTAT *) ((u_char *) q + sizeof(NBTNsResource));
748
749 /* Check out of length */
750 bcount = ntohs(q->rdlen);
751
752 if (q == NULL || (char *)((u_char *) n + bcount) > pmax)
753 return (NULL);
754 else
755 return ((u_char *) n + bcount);
756}
757
758static u_char *
759AliasHandleResource(
760 u_short count,
761 NBTNsResource * q,
762 char *pmax,
763 NBTArguments
764 * nbtarg)
765{
766 while (count != 0) {
767 /* Resource Record Name Filed */
768 q = (NBTNsResource *) AliasHandleName((u_char *) q, pmax);
769
770 if (q == NULL || (char *)(q + 1) > pmax)
771 break;
772#ifdef LIBALIAS_DEBUG
773 printf("type=%02x, count=%d\n", ntohs(q->type), count);
774#endif
775
776 /* Type and Class filed */
777 switch (ntohs(q->type)) {
778 case RR_TYPE_NB:
779 q = (NBTNsResource *) AliasHandleResourceNB(
780 q,
781 pmax,
782 nbtarg
783 );
784 break;
785 case RR_TYPE_A:
786 q = (NBTNsResource *) AliasHandleResourceA(
787 q,
788 pmax,
789 nbtarg
790 );
791 break;
792 case RR_TYPE_NS:
793 q = (NBTNsResource *) AliasHandleResourceNS(
794 q,
795 pmax,
796 nbtarg
797 );
798 break;
799 case RR_TYPE_NULL:
800 q = (NBTNsResource *) AliasHandleResourceNULL(
801 q,
802 pmax,
803 nbtarg
804 );
805 break;
806 case RR_TYPE_NBSTAT:
807 q = (NBTNsResource *) AliasHandleResourceNBSTAT(
808 q,
809 pmax,
810 nbtarg
811 );
812 break;
813 default:
814#ifdef LIBALIAS_DEBUG
815 printf(
816 "\nUnknown Type of Resource %0x\n",
817 ntohs(q->type)
818 );
819 fflush(stdout);
820#endif
821 break;
822 }
823 count--;
824 }
825 return ((u_char *) q);
826}
827
828static int
829AliasHandleUdpNbtNS(
830 struct libalias *la,
831 struct ip *pip, /* IP packet to examine/patch */
832 struct alias_link *lnk,
833 struct in_addr *alias_address,
834 u_short * alias_port,
835 struct in_addr *original_address,
836 u_short * original_port)
837{
838 struct udphdr *uh;
839 NbtNSHeader *nsh;
840 u_char *p;
841 char *pmax;
842 NBTArguments nbtarg;
843
844 (void)la;
845 (void)lnk;
846
847 /* Set up Common Parameter */
848 nbtarg.oldaddr = *alias_address;
849 nbtarg.oldport = *alias_port;
850 nbtarg.newaddr = *original_address;
851 nbtarg.newport = *original_port;
852
853 /* Calculate data length of UDP packet */
854 uh = (struct udphdr *)ip_next(pip);
855 nbtarg.uh_sum = &(uh->uh_sum);
856 nsh = (NbtNSHeader *)udp_next(uh);
857 p = (u_char *) (nsh + 1);
858 pmax = (char *)uh + ntohs(uh->uh_ulen);
859
860 if ((char *)(nsh + 1) > pmax)
861 return (-1);
862
863#ifdef LIBALIAS_DEBUG
864 printf(" [%s] ID=%02x, op=%01x, flag=%02x, rcode=%01x, qd=%04x"
865 ", an=%04x, ns=%04x, ar=%04x, [%d]-->",
866 nsh->dir ? "Response" : "Request",
867 nsh->nametrid,
868 nsh->opcode,
869 nsh->nmflags,
870 nsh->rcode,
871 ntohs(nsh->qdcount),
872 ntohs(nsh->ancount),
873 ntohs(nsh->nscount),
874 ntohs(nsh->arcount),
875 (u_char *) p - (u_char *) nsh
876 );
877#endif
878
879 /* Question Entries */
880 if (ntohs(nsh->qdcount) != 0) {
881 p = AliasHandleQuestion(
882 ntohs(nsh->qdcount),
883 (NBTNsQuestion *) p,
884 pmax,
885 &nbtarg
886 );
887 }
888 /* Answer Resource Records */
889 if (ntohs(nsh->ancount) != 0) {
890 p = AliasHandleResource(
891 ntohs(nsh->ancount),
892 (NBTNsResource *) p,
893 pmax,
894 &nbtarg
895 );
896 }
897 /* Authority Resource Recodrs */
898 if (ntohs(nsh->nscount) != 0) {
899 p = AliasHandleResource(
900 ntohs(nsh->nscount),
901 (NBTNsResource *) p,
902 pmax,
903 &nbtarg
904 );
905 }
906 /* Additional Resource Recodrs */
907 if (ntohs(nsh->arcount) != 0) {
908 p = AliasHandleResource(
909 ntohs(nsh->arcount),
910 (NBTNsResource *) p,
911 pmax,
912 &nbtarg
913 );
914 }
915#ifdef LIBALIAS_DEBUG
916 PrintRcode(nsh->rcode);
917#endif
918 return ((p == NULL) ? -1 : 0);
919}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette