+/* 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 0;
+ break;
+ }
+
+#ifdef DEBUG
+ {
+ int icount = 0;
+
+ printf("FindAliasPortOut:: srcaddr= %s:%u, ",
+ inet_ntoa(src_addr), ntohs(src_port));
+ printf("dstadd= %s:%u link_type= %d, lifetime= %d\n",
+ inet_ntoa(dst_addr), ntohs(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;
+ char src_str[32], dst_str[32], alias_str[32];
+
+ snprintf(src_str, sizeof(src_str), "%s:%u",
+ inet_ntoa(link->src_addr), ntohs(link->src_port));
+ snprintf(dst_str, sizeof(dst_str), "%s:%u",
+ inet_ntoa(link->dst_addr), ntohs(link->dst_port));
+ snprintf(alias_str, sizeof(alias_str), "%s:%u",
+ inet_ntoa(link->alias_addr), ntohs(link->alias_port));
+
+ printf(" linkTableOut[%d:%d] src= %s dst= %s alias= %s flags= 0x%x linktype= %d ts= %d exp= %d fd= %d\n",
+ i, icount, src_str, dst_str, alias_str,
+ link->flags, link->link_type, link->timestamp, link->expire_time, link->sockfd);
+
+ 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 */
+ link = AddLink(src_addr, dst_addr, alias_addr,
+ src_port, 0, GET_ALIAS_EPHEMERAL_PORT,
+ link_type);
+ if ( link != NULL ) {
+ /* link was create, set new lifetime */
+ SetExpire(link, lifetime);
+ /* Prevent link deletion when incoming connection arrive */
+ link->flags |= LINK_CONE;
+ }
+ }
+ if ( link )
+ {
+ if ( addmapping )
+ return( GetAliasPort(link));
+ else
+ {
+ SetExpire(link, 0); /* delete mapping */
+ return 0;
+ }
+ }
+
+ return -1;
+}
+