# Suggestions welcomed to mstan at asesoft ro
# Patch to log the source/destination port for all traffic
# Execute these queries on the netacct db:
# ALTER TABLE `traffic` ADD `srcport` INT(2)  UNSIGNED DEFAULT "0" NOT NULL;
# ALTER TABLE `traffic` ADD `dstport` INT(2)  UNSIGNED DEFAULT "0" NOT NULL
# ALTER TABLE `traffic` ADD INDEX srcp (srcport)
# ALTER TABLE `traffic` ADD INDEX dstp (dstport)

# Apply this over the 0.76 src

diff src/capture.c /var/lib/asterisk/src/capture.c
332c332
< 			  ntohs(tmp_iphdr.ip_len), cfg->promisc->name);
---
> 			  ntohs(tmp_iphdr.ip_len), cfg->promisc->name,srcport,dstport);
336c336
< 			  ntohs(tmp_iphdr.tot_len), cfg->promisc->name);
---
> 			  ntohs(tmp_iphdr.tot_len), cfg->promisc->name,srcport,dstport);
diff src/collect.c /var/lib/asterisk/src/collect.c
57a58,59
>                 pHostItem->m_pHostData->srcPort = 0;
>                 pHostItem->m_pHostData->dstPort = 0;
95c97
< struct HOST_DATA* FindHostInfo( IP_TYPE ipHost, int nPeer )
---
> struct HOST_DATA* FindHostInfo( IP_TYPE ipHost, int nPeer, int srcPort, int dstPort)
103c105,107
< 				( pHostItem->m_pHostData->nPeerFlag == nPeer ) ) )
---
> 				( pHostItem->m_pHostData->nPeerFlag == nPeer ) &&
> 				( pHostItem->m_pHostData->srcPort == srcPort ) &&
> 				( pHostItem->m_pHostData->dstPort == dstPort ) ) )
112c116,119
< 				( pHostItem->m_pHostData->nPeerFlag == nPeer ) )
---
> 				( pHostItem->m_pHostData->nPeerFlag == nPeer ) && 
> 				( pHostItem->m_pHostData->srcPort == srcPort ) &&
> 				( pHostItem->m_pHostData->dstPort == dstPort )
> 				)
130c137
< struct HOST_DATA* GetHostInfo( IP_TYPE ipHost, int nPeer )
---
> struct HOST_DATA* GetHostInfo( IP_TYPE ipHost, int nPeer, int srcPort, int dstPort)
133c140
<         struct  HOST_DATA* pHostInfo = FindHostInfo( ipHost, nPeer );
---
>         struct  HOST_DATA* pHostInfo = FindHostInfo( ipHost, nPeer, srcPort, dstPort);
145c152,154
< 			pNewHostData->nPeerFlag			= nPeer;
---
> 			pNewHostData->nPeerFlag		= nPeer;
> 			pNewHostData->srcPort		= srcPort;
> 			pNewHostData->dstPort		= dstPort;
163c172,174
< 			  int 	       nPeerFlag)
---
> 			  int 	       nPeerFlag,
> 			  int	       srcPort,
> 			  int          dstPort)
165c176
<         struct HOST_DATA* pHostData = GetHostInfo( ipHost, nPeerFlag );
---
>         struct HOST_DATA* pHostData = GetHostInfo( ipHost, nPeerFlag, srcPort, dstPort );
diff src/mysql.c /var/lib/asterisk/src/mysql.c
48c48
<         int rc;
---
>         int rc, srcPort, dstPort;
53a54,56
> 	
> 	srcPort = tmpData->srcPort;
> 	dstPort = tmpData->dstPort;
55c58
<         sprintf(query, "SELECT input,output,peer_input,peer_output,direct_input,direct_output,local_input,local_output FROM %s WHERE ip='%s' AND time=%s","traffic", spyip,TIME_MASK);
---
>         sprintf(query, "SELECT input,output,peer_input,peer_output,direct_input,direct_output,local_input,local_output FROM %s WHERE ip='%s' AND time=%s AND srcport='%d' AND dstport='%d'","traffic", spyip,TIME_MASK,srcPort,dstPort);
88c91
<                 sprintf(query,"UPDATE %s SET input='%li', output='%li', peer_input='%li', peer_output='%li', direct_input='%li', direct_output='%li', local_input='%li',local_output='%li' WHERE ip='%s' AND time=%s", "traffic", old_input,old_output, old_peer_input, old_peer_output,old_direct_input, old_direct_output,old_local_input,old_local_output, spyip,TIME_MASK);
---
>                 sprintf(query,"UPDATE %s SET input='%li', output='%li', peer_input='%li', peer_output='%li', direct_input='%li', direct_output='%li', local_input='%li',local_output='%li' WHERE ip='%s' AND time=%s AND srcport='%d' AND dstport='%d'", "traffic", old_input,old_output, old_peer_input, old_peer_output,old_direct_input, old_direct_output,old_local_input,old_local_output, spyip,TIME_MASK,srcPort,dstPort);
107c110
<         sprintf(query,"INSERT INTO %s (ip,time, input,output,peer_input,peer_output,direct_input,direct_output,local_input,local_output) VALUES ( '%s',%s,'%li','%li','%li','%li','%li','%li','%li','%li')", "traffic", spyip, TIME_MASK, old_input,old_output,old_peer_input,old_peer_output,old_direct_input,old_direct_output, old_local_input,old_local_output);
---
>         sprintf(query,"INSERT INTO %s (ip,time, input,output,peer_input,peer_output,direct_input,direct_output,local_input,local_output,srcport,dstport) VALUES ( '%s',%s,'%li','%li','%li','%li','%li','%li','%li','%li','%d','%d')", "traffic", spyip, TIME_MASK, old_input,old_output,old_peer_input,old_peer_output,old_direct_input,old_direct_output, old_local_input,old_local_output,srcPort,dstPort);
diff src/netacct.h /var/lib/asterisk/src/netacct.h
271c271
< void register_packet(unsigned long int src,unsigned long int dst, int size, char *devname);
---
> void register_packet(unsigned long int src,unsigned long int dst, int size, char *devname, int srcport, int dstport);
301a302,303
> 	int		srcPort;		// Source Port
> 	int		dstPort;		// Destination Port
diff src/process.c /var/lib/asterisk/src/process.c
229c229
< void register_packet(unsigned long int src,unsigned long int dst, int size, char *devname)
---
> void register_packet(unsigned long int src,unsigned long int dst, int size, char *devname, int srcport, int dstport)
240c240
< 		  AddHostTrafficInfo(src, 0, size, check_peering(src,dst));
---
> 		  AddHostTrafficInfo(src, 0, size, check_peering(src,dst), srcport, dstport);
244c244
< 		  AddHostTrafficInfo(dst, size, 0 , check_peering(src,dst));
---
> 		  AddHostTrafficInfo(dst, size, 0 , check_peering(src,dst), dstport, srcport);

