+/* FindAliasPortOut */
+/* external routine for NatPortMap */
+/* return alias port for the src_addr,dst_addr,src_port and proto */
+/* if one doesn't existed, create a mapping with providing pub_port if it's not 0 */
+/* delete mapping if addmapping is not true */
+int
+FindAliasPortOut(struct in_addr src_addr, struct in_addr dst_addr, u_short src_port, u_short pub_port, u_char proto, int lifetime, char addmapping)
+{
+ u_int i;
+ struct alias_link *link;
+ int link_type;
+
+ switch (proto)
+ {
+ case IPPROTO_UDP:
+ link_type = LINK_UDP;
+ break;
+ case IPPROTO_TCP:
+ link_type = LINK_TCP;
+ break;
+ default:
+ return NULL;
+ break;
+ }
+
+#ifdef DEBUG
+ {
+ int icount;
+
+ printf("PORTMAP::srcaddr = 0x%x.%d, dstaddr = 0x%x.%d link_type = %d, lifetime = %d\n",
+ src_addr.s_addr, src_port, dst_addr.s_addr, pub_port, link_type, lifetime);
+
+ for (i=0; i<LINK_TABLE_OUT_SIZE; i++)
+ {
+ link = LIST_FIRST(&linkTableOut[i]);
+ while (link != NULL)
+ {
+ struct alias_link *link_next;
+
+ printf("PORTMAP:: linksrcaddr = 0x%x.%d, linkdstaddr = 0x%x.%d, aliasaddr=0x%x.%d, linktype = %d\n",
+ link->src_addr.s_addr,link->src_port,link->dst_addr.s_addr,link->dst_port, link->alias_addr.s_addr,link->alias_port,
+ link->link_type);
+
+ link_next = LIST_NEXT(link, list_out);
+ icount++;
+ link = link_next;
+ }
+ }
+
+ }
+#endif
+
+ i = StartPointOut(src_addr, dst_addr, src_port, 0, link_type);
+#ifdef DEBUG
+ printf("PORTMAP::StartPointOut returns %d\n", i);
+#endif
+ LIST_FOREACH(link, &linkTableOut[i], list_out)
+ {
+ if (link->src_addr.s_addr == src_addr.s_addr &&
+ link->dst_addr.s_addr == dst_addr.s_addr &&
+ link->src_port == src_port && link->link_type == link_type)
+ break;
+ }
+
+ if ( link == NULL && addmapping)
+ {
+ struct in_addr alias_addr;
+#ifdef DEBUG
+ printf("PORTMAP:: cannot find mapping, adding mapping private port =%d, public port = %d\n",src_port, pub_port);
+#endif
+ /* address/port in not in list, create new mapping */
+
+ alias_addr = FindAliasAddress(src_addr);
+ /* create new mapping */
+ if ( !pub_port )
+ pub_port = GET_ALIAS_PORT;
+ link = AddLink(src_addr, dst_addr, alias_addr,
+ src_port, 0, pub_port,
+ link_type);
+ if ( link != NULL )
+ /* link was create, set new lifetime */
+ SetExpire(link, lifetime);
+ }
+ if ( link )
+ {
+ if ( addmapping )
+ return( GetAliasPort(link));
+ else
+ {
+ SetExpire(link, 0); /* delete mapping */
+ return 0;
+ }
+ }
+
+ return -1;
+}
+