Deleted Added
full compact
alias_db.c (61677) alias_db.c (61861)
1/* -*- mode: c; tab-width: 8; c-basic-indent: 4; -*-
2 Alias_db.c encapsulates all data structures used for storing
3 packet aliasing data. Other parts of the aliasing software
4 access data through functions provided in this file.
5
6 Data storage is based on the notion of a "link", which is
7 established for ICMP echo/reply packets, UDP datagrams and
8 TCP stream connections. A link stores the original source

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

100
101 PacketAliasRedirectPort() and PacketAliasRedirectAddr()
102 added to the API. The first function is a more generalized
103 version of PacketAliasPermanentLink(). The second function
104 implements static network address translation.
105
106 See HISTORY file for additional revisions.
107
1/* -*- mode: c; tab-width: 8; c-basic-indent: 4; -*-
2 Alias_db.c encapsulates all data structures used for storing
3 packet aliasing data. Other parts of the aliasing software
4 access data through functions provided in this file.
5
6 Data storage is based on the notion of a "link", which is
7 established for ICMP echo/reply packets, UDP datagrams and
8 TCP stream connections. A link stores the original source

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

100
101 PacketAliasRedirectPort() and PacketAliasRedirectAddr()
102 added to the API. The first function is a more generalized
103 version of PacketAliasPermanentLink(). The second function
104 implements static network address translation.
105
106 See HISTORY file for additional revisions.
107
108 $FreeBSD: head/sys/netinet/libalias/alias_db.c 61677 2000-06-14 16:09:35Z ru $
108 $FreeBSD: head/sys/netinet/libalias/alias_db.c 61861 2000-06-20 11:41:48Z ru $
109*/
110
111
112/* System include files */
113#include <errno.h>
114#include <stdlib.h>
115#include <stdio.h>
116#include <unistd.h>

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

142
143/* Parameters used for cleanup of expired links */
144#define ALIAS_CLEANUP_INTERVAL_SECS 60
145#define ALIAS_CLEANUP_MAX_SPOKES 30
146
147/* Timeouts (in seconds) for different link types */
148#define ICMP_EXPIRE_TIME 60
149#define UDP_EXPIRE_TIME 60
109*/
110
111
112/* System include files */
113#include <errno.h>
114#include <stdlib.h>
115#include <stdio.h>
116#include <unistd.h>

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

142
143/* Parameters used for cleanup of expired links */
144#define ALIAS_CLEANUP_INTERVAL_SECS 60
145#define ALIAS_CLEANUP_MAX_SPOKES 30
146
147/* Timeouts (in seconds) for different link types */
148#define ICMP_EXPIRE_TIME 60
149#define UDP_EXPIRE_TIME 60
150#define PPTP_EXPIRE_TIME 60
150#define PROTO_EXPIRE_TIME 60
151#define FRAGMENT_ID_EXPIRE_TIME 10
152#define FRAGMENT_PTR_EXPIRE_TIME 30
153
154/* TCP link expire time for different cases */
155/* When the link has been used and closed - minimal grace time to
156 allow ACKs and potential re-connect in FTP (XXX - is this allowed?) */
157#ifndef TCP_EXPIRE_DEAD

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

260
261/* values for link_type */
262#define LINK_ICMP IPPROTO_ICMP
263#define LINK_UDP IPPROTO_UDP
264#define LINK_TCP IPPROTO_TCP
265#define LINK_FRAGMENT_ID (IPPROTO_MAX + 1)
266#define LINK_FRAGMENT_PTR (IPPROTO_MAX + 2)
267#define LINK_ADDR (IPPROTO_MAX + 3)
151#define PROTO_EXPIRE_TIME 60
152#define FRAGMENT_ID_EXPIRE_TIME 10
153#define FRAGMENT_PTR_EXPIRE_TIME 30
154
155/* TCP link expire time for different cases */
156/* When the link has been used and closed - minimal grace time to
157 allow ACKs and potential re-connect in FTP (XXX - is this allowed?) */
158#ifndef TCP_EXPIRE_DEAD

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

261
262/* values for link_type */
263#define LINK_ICMP IPPROTO_ICMP
264#define LINK_UDP IPPROTO_UDP
265#define LINK_TCP IPPROTO_TCP
266#define LINK_FRAGMENT_ID (IPPROTO_MAX + 1)
267#define LINK_FRAGMENT_PTR (IPPROTO_MAX + 2)
268#define LINK_ADDR (IPPROTO_MAX + 3)
269#define LINK_PPTP (IPPROTO_MAX + 4)
268
269 int flags; /* indicates special characteristics */
270
271/* flag bits */
272#define LINK_UNKNOWN_DEST_PORT 0x01
273#define LINK_UNKNOWN_DEST_ADDR 0x02
274#define LINK_PERMANENT 0x04
275#define LINK_PARTIALLY_SPECIFIED 0x03 /* logical-or of first two bits */

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

324 /* chains of link records. Each */
325static struct alias_link * /* link record is doubly indexed */
326linkTableIn[LINK_TABLE_IN_SIZE]; /* into input and output lookup */
327 /* tables. */
328
329static int icmpLinkCount; /* Link statistics */
330static int udpLinkCount;
331static int tcpLinkCount;
270
271 int flags; /* indicates special characteristics */
272
273/* flag bits */
274#define LINK_UNKNOWN_DEST_PORT 0x01
275#define LINK_UNKNOWN_DEST_ADDR 0x02
276#define LINK_PERMANENT 0x04
277#define LINK_PARTIALLY_SPECIFIED 0x03 /* logical-or of first two bits */

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

326 /* chains of link records. Each */
327static struct alias_link * /* link record is doubly indexed */
328linkTableIn[LINK_TABLE_IN_SIZE]; /* into input and output lookup */
329 /* tables. */
330
331static int icmpLinkCount; /* Link statistics */
332static int udpLinkCount;
333static int tcpLinkCount;
334static int pptpLinkCount;
332static int protoLinkCount;
333static int fragmentIdLinkCount;
334static int fragmentPtrLinkCount;
335static int sockCount;
336
337static int cleanupIndex; /* Index to chain of link table */
338 /* being inspected for old links */
339

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

450
451static void
452ShowAliasStats(void)
453{
454/* Used for debugging */
455
456 if (monitorFile)
457 {
335static int protoLinkCount;
336static int fragmentIdLinkCount;
337static int fragmentPtrLinkCount;
338static int sockCount;
339
340static int cleanupIndex; /* Index to chain of link table */
341 /* being inspected for old links */
342

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

453
454static void
455ShowAliasStats(void)
456{
457/* Used for debugging */
458
459 if (monitorFile)
460 {
458 fprintf(monitorFile, "icmp=%d, udp=%d, tcp=%d, proto=%d, frag_id=%d frag_ptr=%d",
461 fprintf(monitorFile, "icmp=%d, udp=%d, tcp=%d, pptp=%d, proto=%d, frag_id=%d frag_ptr=%d",
459 icmpLinkCount,
460 udpLinkCount,
461 tcpLinkCount,
462 icmpLinkCount,
463 udpLinkCount,
464 tcpLinkCount,
465 pptpLinkCount,
462 protoLinkCount,
463 fragmentIdLinkCount,
464 fragmentPtrLinkCount);
465
466 fprintf(monitorFile, " / tot=%d (sock=%d)\n",
467 icmpLinkCount + udpLinkCount
468 + tcpLinkCount
466 protoLinkCount,
467 fragmentIdLinkCount,
468 fragmentPtrLinkCount);
469
470 fprintf(monitorFile, " / tot=%d (sock=%d)\n",
471 icmpLinkCount + udpLinkCount
472 + tcpLinkCount
473 + pptpLinkCount
469 + protoLinkCount
470 + fragmentIdLinkCount
471 + fragmentPtrLinkCount,
472 sockCount);
473
474 fflush(monitorFile);
475 }
476}

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

545 u_short port_net;
546
547/*
548 Description of alias_port_param for GetNewPort(). When
549 this parameter is zero or positive, it precisely specifies
550 the port number. GetNewPort() will return this number
551 without check that it is in use.
552
474 + protoLinkCount
475 + fragmentIdLinkCount
476 + fragmentPtrLinkCount,
477 sockCount);
478
479 fflush(monitorFile);
480 }
481}

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

550 u_short port_net;
551
552/*
553 Description of alias_port_param for GetNewPort(). When
554 this parameter is zero or positive, it precisely specifies
555 the port number. GetNewPort() will return this number
556 without check that it is in use.
557
553 When this parameter is -1, it indicates to get a randomly
558 When this parameter is GET_ALIAS_PORT, it indicates to get a randomly
554 selected port number.
555*/
556
557 if (alias_port_param == GET_ALIAS_PORT)
558 {
559 /*
560 * The aliasing port is automatically selected
561 * by one of two methods below:

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

613 && (search_result->flags & LINK_PARTIALLY_SPECIFIED))
614 go_ahead = 1;
615 else
616 go_ahead = 0;
617
618 if (go_ahead)
619 {
620 if ((packetAliasMode & PKT_ALIAS_USE_SOCKETS)
559 selected port number.
560*/
561
562 if (alias_port_param == GET_ALIAS_PORT)
563 {
564 /*
565 * The aliasing port is automatically selected
566 * by one of two methods below:

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

618 && (search_result->flags & LINK_PARTIALLY_SPECIFIED))
619 go_ahead = 1;
620 else
621 go_ahead = 0;
622
623 if (go_ahead)
624 {
625 if ((packetAliasMode & PKT_ALIAS_USE_SOCKETS)
621 && (link->flags & LINK_PARTIALLY_SPECIFIED))
626 && (link->flags & LINK_PARTIALLY_SPECIFIED)
627 && ((link->link_type == LINK_TCP) ||
628 (link->link_type == LINK_UDP)))
622 {
623 if (GetSocket(port_net, &link->sockfd, link->link_type))
624 {
625 link->alias_port = port_net;
626 return(0);
627 }
628 }
629 else

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

833 case LINK_UDP:
834 udpLinkCount--;
835 break;
836 case LINK_TCP:
837 tcpLinkCount--;
838 if (link->data.tcp != NULL)
839 free(link->data.tcp);
840 break;
629 {
630 if (GetSocket(port_net, &link->sockfd, link->link_type))
631 {
632 link->alias_port = port_net;
633 return(0);
634 }
635 }
636 else

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

840 case LINK_UDP:
841 udpLinkCount--;
842 break;
843 case LINK_TCP:
844 tcpLinkCount--;
845 if (link->data.tcp != NULL)
846 free(link->data.tcp);
847 break;
848 case LINK_PPTP:
849 pptpLinkCount--;
850 break;
841 case LINK_FRAGMENT_ID:
842 fragmentIdLinkCount--;
843 break;
844 case LINK_FRAGMENT_PTR:
845 fragmentPtrLinkCount--;
846 if (link->data.frag_ptr != NULL)
847 free(link->data.frag_ptr);
848 break;

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

901 link->expire_time = ICMP_EXPIRE_TIME;
902 break;
903 case LINK_UDP:
904 link->expire_time = UDP_EXPIRE_TIME;
905 break;
906 case LINK_TCP:
907 link->expire_time = TCP_EXPIRE_INITIAL;
908 break;
851 case LINK_FRAGMENT_ID:
852 fragmentIdLinkCount--;
853 break;
854 case LINK_FRAGMENT_PTR:
855 fragmentPtrLinkCount--;
856 if (link->data.frag_ptr != NULL)
857 free(link->data.frag_ptr);
858 break;

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

911 link->expire_time = ICMP_EXPIRE_TIME;
912 break;
913 case LINK_UDP:
914 link->expire_time = UDP_EXPIRE_TIME;
915 break;
916 case LINK_TCP:
917 link->expire_time = TCP_EXPIRE_INITIAL;
918 break;
919 case LINK_PPTP:
920 link->expire_time = PPTP_EXPIRE_TIME;
921 break;
909 case LINK_FRAGMENT_ID:
910 link->expire_time = FRAGMENT_ID_EXPIRE_TIME;
911 break;
912 case LINK_FRAGMENT_PTR:
913 link->expire_time = FRAGMENT_PTR_EXPIRE_TIME;
914 break;
915 case LINK_ADDR:
916 break;

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

989 else
990 {
991#ifdef DEBUG
992 fprintf(stderr, "PacketAlias/AddLink: ");
993 fprintf(stderr, " cannot allocate auxiliary TCP data\n");
994#endif
995 }
996 break;
922 case LINK_FRAGMENT_ID:
923 link->expire_time = FRAGMENT_ID_EXPIRE_TIME;
924 break;
925 case LINK_FRAGMENT_PTR:
926 link->expire_time = FRAGMENT_PTR_EXPIRE_TIME;
927 break;
928 case LINK_ADDR:
929 break;

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

1002 else
1003 {
1004#ifdef DEBUG
1005 fprintf(stderr, "PacketAlias/AddLink: ");
1006 fprintf(stderr, " cannot allocate auxiliary TCP data\n");
1007#endif
1008 }
1009 break;
1010 case LINK_PPTP:
1011 pptpLinkCount++;
1012 break;
997 case LINK_FRAGMENT_ID:
998 fragmentIdLinkCount++;
999 break;
1000 case LINK_FRAGMENT_PTR:
1001 fragmentPtrLinkCount++;
1002 break;
1003 case LINK_ADDR:
1004 break;

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

1302
1303-- "external" means outside alias_db.c, but within alias*.c --
1304
1305 FindIcmpIn(), FindIcmpOut()
1306 FindFragmentIn1(), FindFragmentIn2()
1307 AddFragmentPtrLink(), FindFragmentPtr()
1308 FindProtoIn(), FindProtoOut()
1309 FindUdpTcpIn(), FindUdpTcpOut()
1013 case LINK_FRAGMENT_ID:
1014 fragmentIdLinkCount++;
1015 break;
1016 case LINK_FRAGMENT_PTR:
1017 fragmentPtrLinkCount++;
1018 break;
1019 case LINK_ADDR:
1020 break;

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

1318
1319-- "external" means outside alias_db.c, but within alias*.c --
1320
1321 FindIcmpIn(), FindIcmpOut()
1322 FindFragmentIn1(), FindFragmentIn2()
1323 AddFragmentPtrLink(), FindFragmentPtr()
1324 FindProtoIn(), FindProtoOut()
1325 FindUdpTcpIn(), FindUdpTcpOut()
1326 FindPptpIn(), FindPptpOut()
1310 FindOriginalAddress(), FindAliasAddress()
1311
1312(prototypes in alias_local.h)
1313*/
1314
1315
1316struct alias_link *
1317FindIcmpIn(struct in_addr dst_addr,

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

1528 src_port, dst_port, GET_ALIAS_PORT,
1529 link_type);
1530 }
1531
1532 return(link);
1533}
1534
1535
1327 FindOriginalAddress(), FindAliasAddress()
1328
1329(prototypes in alias_local.h)
1330*/
1331
1332
1333struct alias_link *
1334FindIcmpIn(struct in_addr dst_addr,

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

1545 src_port, dst_port, GET_ALIAS_PORT,
1546 link_type);
1547 }
1548
1549 return(link);
1550}
1551
1552
1553struct alias_link *
1554FindPptpIn(struct in_addr dst_addr,
1555 struct in_addr alias_addr,
1556 u_short call_id)
1557{
1558 struct alias_link *link;
1559
1560 link = FindLinkIn(dst_addr, alias_addr,
1561 NO_DEST_PORT, call_id,
1562 LINK_PPTP, 1);
1563
1564 if (link == NULL && !(packetAliasMode & PKT_ALIAS_DENY_INCOMING))
1565 {
1566 struct in_addr target_addr;
1567
1568 target_addr = FindOriginalAddress(alias_addr);
1569 link = AddLink(target_addr, dst_addr, alias_addr,
1570 call_id, NO_DEST_PORT, call_id,
1571 LINK_PPTP);
1572 }
1573
1574 return(link);
1575}
1576
1577
1578struct alias_link *
1579FindPptpOut(struct in_addr src_addr,
1580 struct in_addr dst_addr,
1581 u_short call_id)
1582{
1583 struct alias_link *link;
1584
1585 link = FindLinkOut(src_addr, dst_addr,
1586 call_id, NO_DEST_PORT,
1587 LINK_PPTP, 1);
1588
1589 if (link == NULL)
1590 {
1591 struct in_addr alias_addr;
1592
1593 alias_addr = FindAliasAddress(src_addr);
1594 link = AddLink(src_addr, dst_addr, alias_addr,
1595 call_id, NO_DEST_PORT, GET_ALIAS_PORT,
1596 LINK_PPTP);
1597 }
1598
1599 return(link);
1600}
1601
1602
1536struct in_addr
1537FindOriginalAddress(struct in_addr alias_addr)
1538{
1539 struct alias_link *link;
1540
1541 link = FindLinkIn(nullAddress, alias_addr,
1542 0, 0, LINK_ADDR, 0);
1543 if (link == NULL)

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

2303 }
2304
2305 aliasAddress.s_addr = INADDR_ANY;
2306 targetAddress.s_addr = INADDR_ANY;
2307
2308 icmpLinkCount = 0;
2309 udpLinkCount = 0;
2310 tcpLinkCount = 0;
1603struct in_addr
1604FindOriginalAddress(struct in_addr alias_addr)
1605{
1606 struct alias_link *link;
1607
1608 link = FindLinkIn(nullAddress, alias_addr,
1609 0, 0, LINK_ADDR, 0);
1610 if (link == NULL)

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

2370 }
2371
2372 aliasAddress.s_addr = INADDR_ANY;
2373 targetAddress.s_addr = INADDR_ANY;
2374
2375 icmpLinkCount = 0;
2376 udpLinkCount = 0;
2377 tcpLinkCount = 0;
2378 pptpLinkCount = 0;
2311 protoLinkCount = 0;
2312 fragmentIdLinkCount = 0;
2313 fragmentPtrLinkCount = 0;
2314 sockCount = 0;
2315
2316 cleanupIndex =0;
2317
2318 packetAliasMode = PKT_ALIAS_SAME_PORTS

--- 240 unchanged lines hidden ---
2379 protoLinkCount = 0;
2380 fragmentIdLinkCount = 0;
2381 fragmentPtrLinkCount = 0;
2382 sockCount = 0;
2383
2384 cleanupIndex =0;
2385
2386 packetAliasMode = PKT_ALIAS_SAME_PORTS

--- 240 unchanged lines hidden ---