irc.c 8.18 KB
Newer Older
1 2 3
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
's avatar
committed
4 5 6 7 8
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <string.h>
#include <errno.h>
's avatar
committed
9

's avatar
committed
10 11 12 13 14 15 16 17
#include "mapidflib.h"
#include "mapidlib.h"
#include "mapidevices.h"
#include "mapid.h"
#include "fhelp.h"
#include "debug.h"
#include "mapiipc.h"
#include "mstring.h"
's avatar
committed
18 19

#include <pcap.h>
's avatar
committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <netinet/in.h>
#include <net/ethernet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <time.h>

#include "log.h"
41
#include "trackflib.h"
's avatar
committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

struct filters {
	int protocol;
	unsigned int saddr;
	unsigned int daddr;
	uint16_t sp;
	uint16_t dp;
	struct timeval ts;
	struct filters *next;
};

struct list{
	struct filters *head;
	struct filters *tail;
};

's avatar
committed
58
#define IRC_HASHTABLESIZE 1024
's avatar
committed
59 60 61 62
#define STRING_NUM 5

int isIRC(mapidflib_function_instance_t *instance, unsigned char *pkt, unsigned int len);

's avatar
committed
63
char *irc_strings[STRING_NUM]={"PRIVMSG ", "NOTICE ", " :DOWN// ", " :EFTP// ", "ACTION"};
's avatar
committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77

int irc_string_len[STRING_NUM] = {0};

struct mapid_irc {
	int *shift[STRING_NUM];
	int *skip[STRING_NUM];
	struct list **irclist;
};

static int irc_init(mapidflib_function_instance_t *instance, MAPI_UNUSED int fd)
{
	int i=0;	
	
	instance->internal_data = malloc(sizeof(struct mapid_irc));
's avatar
committed
78 79 80
	((struct mapid_irc*)instance->internal_data)->irclist = (struct list**)malloc(sizeof(struct list*)*IRC_HASHTABLESIZE);
	memset(((struct mapid_irc*)instance->internal_data)->irclist, 0, (sizeof(struct list*)*IRC_HASHTABLESIZE));
	for(i = 0; i < IRC_HASHTABLESIZE; i++) {
's avatar
committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		((struct mapid_irc*)instance->internal_data)->irclist[i] = (struct list*)malloc(sizeof(struct list));
		((struct mapid_irc*)instance->internal_data)->irclist[i]->head = NULL;
		((struct mapid_irc*)instance->internal_data)->irclist[i]->tail = NULL;
	}
	for(i=0;i<STRING_NUM;i++) {
		((struct mapid_irc*)instance->internal_data)->shift[i] = make_shift(irc_strings[i],strlen(irc_strings[i]));
		((struct mapid_irc*)instance->internal_data)->skip[i] = make_skip(irc_strings[i], strlen(irc_strings[i]));
	}

	return 0;
}	

int isIRC(mapidflib_function_instance_t *instance, unsigned char *pkt, unsigned int len)
{
	int i=0;

	for(i=0;i<STRING_NUM;i++) {
		if(len < strlen(irc_strings[i]))
				continue;
		
		if(len >= 100) {
's avatar
committed
102
			if(mSearch((char *)(pkt), 100, irc_strings[i], strlen(irc_strings[i]),
's avatar
committed
103 104 105 106 107 108 109
						((struct mapid_irc *)instance->internal_data)->skip[i],
						((struct mapid_irc *)instance->internal_data)->shift[i]))
			{
				return i;
			}
		}
		else {
's avatar
committed
110
			if(mSearch((char *)(pkt), len, irc_strings[i], strlen(irc_strings[i]),
's avatar
committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
						((struct mapid_irc *)instance->internal_data)->skip[i],
						((struct mapid_irc *)instance->internal_data)->shift[i]))
			{
				return i;
			}
		}
	}
	return -1;

}

static int irc_process(mapidflib_function_instance_t *instance,
			MAPI_UNUSED unsigned char* dev_pkt,
			unsigned char* pkt,
			mapid_pkthdr_t* pkt_head)
{
	struct filters *temp = NULL, *prev = NULL, *new = NULL;
	int len = pkt_head->caplen;
	unsigned char *p = NULL;
	struct timeval ts;

	struct list **irclist = ((struct mapid_irc*)instance->internal_data)->irclist;
	uint16_t ethertype;
	struct ether_header *ep = NULL;
's avatar
committed
135 136 137 138
	struct pos_header {
		uint16_t af;
		uint16_t cf;
	}	*pp = NULL;
's avatar
committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	struct iphdr *iph = NULL;
	struct tcphdr *tcph = NULL;
	struct udphdr *udph = NULL;

	struct vlan_802q_header *vlan_header;

	unsigned int saddr, daddr;
	
	struct in_addr source, dest;
	uint16_t sp, dp;
	
	unsigned int hashval = 0;
	int i = 0;

	p = pkt;

's avatar
committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	switch(instance->hwinfo->link_type) {
		case DLT_EN10MB:
				// lay the Ethernet header struct over the packet data
				ep = (struct ether_header *)p;

				// skip ethernet header
				p += sizeof(struct ether_header);
				len -= sizeof(struct ether_header);

				ethertype = ntohs(ep->ether_type);

				if(ethertype  == ETHERTYPE_8021Q) {
					vlan_header = (struct vlan_802q_header*)p;
					ethertype = ntohs(vlan_header->ether_type);
					p += sizeof(struct vlan_802q_header);
				}
				
				if(ethertype == MPLS_MASK) {
					p += 4;			
				}
				else if(ethertype != ETHERTYPE_IP) {
					return 0;
				}
			break;
		case DLT_CHDLC:
				pp = (struct pos_header *)p;

				p += sizeof(struct pos_header);
				len -= sizeof(struct pos_header);

				ethertype = ntohs(pp->cf);

				if (ethertype != ETHERTYPE_IP) {
					return 0;
				}
			break;
		default:
			//DEBUG_CMD(Debug_Message("Link layer not supported"));
			return 0;
's avatar
committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207
	}
	
	// IP header struct over the packet data;
	iph = (struct iphdr*)p;

	saddr = *((unsigned int *)&(iph->saddr));
	daddr = *((unsigned int *)&(iph->daddr));
	
	source.s_addr = (unsigned long int)iph->saddr ;
	dest.s_addr = (unsigned long int)iph->daddr;

	p += iph->ihl * 4;
	len -= iph->ihl * 4;

's avatar
committed
208
	hashval = (saddr + daddr) % IRC_HASHTABLESIZE;
's avatar
committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

	if(iph->protocol == 6)	// TCP
	{
		tcph = (struct tcphdr *)p;
		
		sp = ntohs(tcph->source);
		dp = ntohs(tcph->dest);

		p += tcph->doff * 4;
		len -= tcph->doff * 4;

		if((unsigned int)(p - pkt) == pkt_head->caplen) {
			return 0;
		}
	}
	else if(iph->protocol == 17)	// UDP
	{
		udph = (struct udphdr *)p;

		sp = ntohs(udph->source);
		dp = ntohs(udph->dest);

		p += sizeof(struct udphdr);
		len -= sizeof(struct udphdr);

		if((unsigned int)(p - pkt) == pkt_head->caplen) {
			return 0;
		}
	}
	else
	{
		return 0;
	}

	gettimeofday(&ts, NULL);
	
	for(temp = irclist[hashval]->head, prev = irclist[hashval]->head; temp != NULL; prev = temp, temp = temp->next)
	{
			if(temp->protocol == iph->protocol &&
					(
					(temp->saddr == saddr && temp->daddr == daddr && temp->sp == sp && temp->dp == dp)
					||
					(temp->saddr == daddr && temp->daddr == saddr && temp->sp == dp && temp->dp == sp))
			  )
			{
				gettimeofday(&(temp->ts), NULL);

				if(iph->protocol == 6 && tcph->fin) {
					if(temp == irclist[hashval]->head){
						irclist[hashval]->head = temp->next;
					}
					else {
						prev->next = temp->next;
					}
					temp->next = NULL;
					free(temp);
				}
				
				return 1;
			}
			
			if(ts.tv_sec - temp->ts.tv_sec > 60) {
				if(temp == irclist[hashval]->head){
					irclist[hashval]->head = temp->next;
				}
				else {
					prev->next = temp->next;
				}
				temp->next = NULL;
				free(temp);
			}
	}


	if((i = isIRC(instance,pkt,len)) >= 0)
	{
's avatar
committed
285
#ifdef __TRACKFLIB_LOGGING__
's avatar
committed
286
		unsigned char *p_b = p;
's avatar
committed
287
#endif
's avatar
committed
288
		new = (struct filters*)malloc(sizeof(struct filters));
's avatar
committed
289

's avatar
committed
290 291 292 293 294
		new->protocol = iph->protocol;
		new->saddr = saddr;
		new->daddr = daddr;
		new->sp = sp;
		new->dp = dp;
295
#ifdef __TRACKFLIB_LOGGING__
's avatar
committed
296
		write_to_log("IRC", irc_strings[i], iph->protocol, source, sp, dest, dp, p_b, len);
297
#endif
's avatar
committed
298

's avatar
committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
		for(temp = irclist[hashval]->head; temp != NULL; temp = temp->next)
		{
			if(new->protocol == temp->protocol && (
					(new->saddr == temp->saddr && new->daddr == temp->daddr && new->sp == temp->sp && new->dp == temp->dp) 
					||
					(new->daddr == temp->saddr && new->saddr == temp->daddr && new->dp == temp->sp && new->sp == temp->dp)
					)
				)
			{
				return 1;
			}
		}

		gettimeofday(&(new->ts), NULL);
		
		new->next = irclist[hashval]->head;
		irclist[hashval]->head = new;

		return 1;
	}

	return 0;
}

static int irc_cleanup(mapidflib_function_instance_t *instance) 
{
	struct filters *temp = NULL, *tmp = NULL;
	int i = 0;

  if(instance->internal_data != NULL){
's avatar
committed
329
	  for(i = 0; i < IRC_HASHTABLESIZE; i++) {
's avatar
committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
		  temp = ((struct mapid_irc*)instance->internal_data)->irclist[i]->head;
		  
		  while(temp != NULL) {
			  tmp = temp;
			  temp = temp->next;
			  free(tmp);
		  }
	  }
	free(((struct mapid_irc*)instance->internal_data)->irclist);
	free(instance->internal_data);
  }
  return 0;
}

static mapidflib_function_def_t finfo={
  "",
  "TRACK_IRC",
  "Searches for Internet Relay Chat packets\n",
  "",
  MAPI_DEVICE_ALL,
  MAPIRES_NONE,
  0, //shm size
  0, //modifies_pkts
  1, //filters packets
  MAPIOPT_NONE,
  NULL,
  irc_init,
  irc_process,
  NULL, //get_result
  NULL, //reset
  irc_cleanup,
  NULL, //client_init
  NULL, //client_read_result
  NULL  //client_cleanup
};

mapidflib_function_def_t* irc_get_funct_info();
mapidflib_function_def_t* irc_get_funct_info() {
  return &finfo;
};