summaryrefslogtreecommitdiff
path: root/protocols/Xfire/src/Xfire_voicechat.cpp
blob: 7a3ce804d2f67f43c534e5e1980262a321d6025d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
329
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
371
372
373
374
375
376
377
378
379
380
#include "stdafx.h"
#include "Xfire_voicechat.h"

//konstruktor
Xfire_voicechat::Xfire_voicechat() {
	this->resetCurrentvoicestatus();
	ipport=NULL;
	tsrDLL=NULL;
	IpHlpApiDLL=NULL;
	tsrGetServerInfo=NULL;
	GetExtendedTcpTable=NULL;
	pid=0;
}

//dekonstruktor
Xfire_voicechat::~Xfire_voicechat() {
	//geladene tsr remote dll freigeben
	if(tsrDLL) {
		FreeLibrary(tsrDLL);
		tsrDLL=NULL;
	}
	//geladene iphlper freigeben
	if(IpHlpApiDLL) {
		FreeLibrary(IpHlpApiDLL);
		IpHlpApiDLL=NULL;
	}
}

//init
void Xfire_voicechat::initVoicechat() {
	//tsremotedll laden
	tsrDLL=this->loadTSR();
	//weitere dll's laden
	IpHlpApiDLL=LoadLibraryA("IpHlpApi.dll");
	if(IpHlpApiDLL) {
		GetExtendedTcpTable=(pGetExtendedTcpTable)GetProcAddress(IpHlpApiDLL,"GetExtendedTcpTable");
	}
	else
	{
		XFireLog("IpHlpApi.dll load failed!");
	}
}

//prüft ob das paket schonmal versendet wurde, soll unnötigen nwtraffic reduzieren, *ÜBERLEGUNG* ob wirklich notwendig
BOOL Xfire_voicechat::alreadySend(SendGameStatus2Packet* packet) {
	if(packet==NULL)
		return FALSE;
	
	if(packet->ip[3] != lastpacket.ip[3] ||
	   packet->ip[2] != lastpacket.ip[2] ||
	   packet->ip[1] != lastpacket.ip[1] ||
	   packet->ip[0] != lastpacket.ip[0] ||
	   packet->port != lastpacket.port) {
		   lastpacket=*packet;
		   return TRUE;
	}

	return FALSE;
}

//prüft nach laufenden voicechat anwendungen
BOOL Xfire_voicechat::checkVoicechat(SendGameStatus2Packet* packet) {
	//kein gültiger verweis?
	if(packet==NULL)
		return FALSE;
	//jeh nach letzten status handeln
	switch(currentvoice) {
		case XFIREVOICECHAT_TS2:
			if(checkforTS2(packet)) {
				return alreadySend(packet);
			}
			else
			{
				//kein ts2 mehr? dann paket restten
				resetSendGameStatus2Packet(packet);
				resetCurrentvoicestatus();
				//in db schreiben
				writeToDatabase(packet);
				return TRUE;
			}
			break;
		case XFIREVOICECHAT_TS3:
			if(checkforTS3(packet)) {
				return alreadySend(packet);
			}
			else
			{
				//kein ts3 mehr? dann paket restten
				resetSendGameStatus2Packet(packet);
				resetCurrentvoicestatus();
				//in db schreiben
				writeToDatabase(packet);
				return TRUE;
			}
			break;
		case XFIREVOICECHAT_MUMBLE:
			if(checkforMumble(packet)) {
				return alreadySend(packet);
			}
			else
			{
				//kein mumble mehr? dann paket restten
				resetSendGameStatus2Packet(packet);
				resetCurrentvoicestatus();
				//in db schreiben
				writeToDatabase(packet);
				return TRUE;
			}
			break;
		default:
			//prüfe nach ts3
			if(checkforTS3(packet)) {
				return alreadySend(packet);
			}
			//prüfe nach ts2
			else if(checkforTS2(packet)) {
				return alreadySend(packet);
			}
			//prüfe nach mumble
			else if(checkforMumble(packet)) {
				return alreadySend(packet);
			}
			break;
	};

	return FALSE;
}

//setzte currentvoice auf 0 zurück, falls es einen disconnect gab
void Xfire_voicechat::resetCurrentvoicestatus() {
	currentvoice=XFIREVOICECHAT_NOVOICE;
	lastpacket.ip[3]=0;
	lastpacket.ip[2]=0;
	lastpacket.ip[1]=0;
	lastpacket.ip[0]=0;
	lastpacket.port=0;
	pid=0;
}

//resettet das packet auf 0
void Xfire_voicechat::resetSendGameStatus2Packet(SendGameStatus2Packet* packet) {
	if(packet==NULL)
		return;
	//voiceid
	packet->gameid=XFIREVOICECHAT_NOVOICE;
	//ip zuweisen
	packet->ip[3]=0;
	packet->ip[2]=0;
	packet->ip[1]=0;
	packet->ip[0]=0;
	//port zuweisen
	packet->port=0;
}

//schreibt derzetigen status in die mirandadb für variables usw
void Xfire_voicechat::writeToDatabase(SendGameStatus2Packet* packet) {
	//für sprintf
	char temp[32]="";

	if(packet==NULL || packet->gameid==XFIREVOICECHAT_NOVOICE) {
		//einträge aus der db entfernen
		db_unset(NULL,protocolname, "VServerIP");
		db_unset(NULL,protocolname, "currentvoicename");
		//zurück
		return;
	}
	//ip speichern
	mir_snprintf(temp,32,"%d.%d.%d.%d:%d",(unsigned char)packet->ip[3],(unsigned char)packet->ip[2],(unsigned char)packet->ip[1],(unsigned char)packet->ip[0],packet->port);
	db_set_s(NULL, protocolname, "VServerIP", temp);
	//namen jeh nach id schreiben
	switch(packet->gameid) {
		case XFIREVOICECHAT_TS3:
			db_set_s(NULL, protocolname, "currentvoicename", "Teamspeak 3");
			break;
		case XFIREVOICECHAT_TS2:
			db_set_s(NULL, protocolname, "currentvoicename", "Teamspeak 2");
			break;
		case XFIREVOICECHAT_MUMBLE:
			db_set_s(NULL, protocolname, "currentvoicename", "Mumble");
			break;
		case XFIREVOICECHAT_VENTRILO:
			db_set_s(NULL, protocolname, "currentvoicename", "Ventrilo");
			break;
	};
}

//versucht die TSR zuladen
HMODULE Xfire_voicechat::loadTSR(char* path,BOOL nolocaltest) {
	char pathtotsr[MAX_PATH]="";

	/*if(path)
		; was tun*/
	strcat_s(pathtotsr,MAX_PATH,"TSRemote.dll");
	
	//versuche dll zuladen
	HMODULE tsrDLL = LoadLibrary(pathtotsr);
	//konnte nicht geladen werden 
	if(!tsrDLL)
	{
		XFireLog("TSRemote.dll load failed!");

		//bei keinem lokalen test abbruch
		if(nolocaltest) return NULL;

		//nochmal engl. lokal versuchen
		tsrDLL = LoadLibrary("C:\\Program Files\\Teamspeak2_RC2\\client_sdk\\TSRemote.dll");

		if(!tsrDLL) {
			XFireLog("TSRemote.dll load faild (using standard installationpath)!");
	
			//deutsches sys?
			tsrDLL = LoadLibrary("C:\\Programme\\Teamspeak2_RC2\\client_sdk\\TSRemote.dll");

			if(!tsrDLL)
				XFireLog("TSRemote.dll load failed (using standard installationpath2)!");
	
				//aufgeben
				return NULL;
		}
	}

	XFireLog("TSRemote.dll successfully loaded!");

	//getserverinfo funktion holen
	tsrGetServerInfo = (LPtsrGetServerInfo)GetProcAddress(tsrDLL,"tsrGetServerInfo");

	return tsrDLL;
}


//teamspeak 3 detection, benötigt ts3plugin
BOOL Xfire_voicechat::checkforTS3(SendGameStatus2Packet* packet) {
	ts3IPPORT* ipport=NULL;
	//kein gültiger verweis?
	if(packet==NULL)
		return FALSE;
	//existiert ein filemap?
	HANDLE hMapObject = OpenFileMappingA(FILE_MAP_READ, FALSE, "$ts3info4xfire$");
	//nicht gefunden, dann kein ts3
	if (hMapObject == NULL)
		return FALSE;
	//versuch ipport zubesorgen
	ipport = (ts3IPPORT *) MapViewOfFile(hMapObject, FILE_MAP_READ, 0, 0, sizeof(ts3IPPORT));
	//fehler beim zugriff auf filemap?
	if(ipport==NULL)
	{
		CloseHandle(hMapObject);
		return FALSE;
	}

	//wenn kein port, dann stimmt was mit der ip nicht, paket resetten
	if(ipport->port==0) {
		//packet resetten
		resetSendGameStatus2Packet(packet);
		//in db schreiben
		writeToDatabase(packet);
		return TRUE;
	}

	//voiceid zuweisen
	this->currentvoice=XFIREVOICECHAT_TS3;
	packet->gameid=XFIREVOICECHAT_TS3;
	//ip zuweisen
	packet->ip[3]=ipport->ip[3];
	packet->ip[2]=ipport->ip[2];
	packet->ip[1]=ipport->ip[1];
	packet->ip[0]=ipport->ip[0];
	//port zuweisen
	packet->port=ipport->port;
	//unmap, handle schließem
	UnmapViewOfFile(ipport);
	CloseHandle(hMapObject);
	//in db schreiben
	writeToDatabase(packet);
	//ts3 gefunden
	return TRUE; 
}

//teamspeak 2 detection mit hilfe der tsr
BOOL Xfire_voicechat::checkforTS2(SendGameStatus2Packet* packet) {
	TtsrServerInfo serverinfo={0};

	//get funktion ist nicht initialisiert
	if(this->tsrGetServerInfo==NULL || packet==NULL)
	{
		return FALSE;
	}

	//infos holen
	this->tsrGetServerInfo(&serverinfo);

	//auswerten wenn serverip gesetzt
	if(serverinfo.ServerIp[0]!=0)
	{
		char * pos=strrchr(serverinfo.ServerIp,':');
		if(pos==0)
		{
			return FALSE;
		}

		*pos=0;
		unsigned int ip=inet_addr(serverinfo.ServerIp);
		pos++;
		int port=atoi(pos);

		//port zuweisen
		packet->port=port;
		//ip zuweisen
		packet->ip[3]=LOBYTE(LOWORD(ip));
		packet->ip[2]=HIBYTE(LOWORD(ip));
		packet->ip[1]=LOBYTE(HIWORD(ip));
		packet->ip[0]=HIBYTE(HIWORD(ip));		
		//gameid/voice zuweisen
		packet->gameid=XFIREVOICECHAT_TS2;
		this->currentvoice=XFIREVOICECHAT_TS2;
		//ab in die db
		writeToDatabase(packet);

		return TRUE;
	}

	return FALSE;
}

//detection für mumble
BOOL Xfire_voicechat::checkforMumble(SendGameStatus2Packet* packet) {
	//kein gültiger verweis?
	if(packet==NULL)
		return FALSE;

	//gültige pid
	if(this->pid!=0 && !this->isValidPid(this->pid))
	{
		this->pid=0;
		return FALSE;
	}
	else {
		if(!this->getPidByProcessName("mumble.exe",&this->pid)) {
			return FALSE;
		}
	}

	DWORD size=0;
	MIB_TCPTABLE_OWNER_PID* ptab=NULL;
	//tcptabelle holen
	this->GetExtendedTcpTable(NULL,&size,FALSE, AF_INET, TCP_TABLE_OWNER_PID_CONNECTIONS, 0);
	//überhaupt was drin?
	if(size) {
		ptab=(MIB_TCPTABLE_OWNER_PID*)malloc(size);
		//liste auslesen
		if(GetExtendedTcpTable(ptab,&size,FALSE, AF_INET, TCP_TABLE_OWNER_PID_CONNECTIONS, 0)==NO_ERROR)
		{
			for(unsigned int i=0;i<ptab->dwNumEntries;i++)
			{
				if(ptab->table[i].dwOwningPid==this->pid && ptab->table[i].dwLocalAddr!=ptab->table[i].dwRemoteAddr) //verbindung gefunden, hoffentlich
				{
					unsigned char*rip=(unsigned char*)&ptab->table[i].dwRemoteAddr;
					XFireLog("IP %x,%x",ptab->table[i].dwRemoteAddr,ptab->table[i].dwRemotePort);
					//ipzuweisen
					packet->ip[3]=rip[0];
					packet->ip[2]=rip[1];
					packet->ip[1]=rip[2];
					packet->ip[0]=rip[3];
					//portzuweisen
					packet->port=r(ptab->table[i].dwRemotePort);
					//mumble
					packet->gameid=XFIREVOICECHAT_MUMBLE;
					this->currentvoice=XFIREVOICECHAT_MUMBLE;
					//table wieder freigeben
					delete ptab;
					//mumble läuft + ip gefunden also TRUE
					return TRUE;
				}
			}
		}
		delete ptab;
	}

	return FALSE;
}