Counter Strike 1.6 Master Server facut in 28 ianuarie 2013
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <pthread.h>
#define SMALL_STACK 16384 //16K for threads stack-minimum
char ip[50];
int port;
using namespace std;
std::vector<string> europe_servers;
int sock;
struct sockaddr_in from;
void* getServers(void *arg)
{
while(1)
{
europe_servers.clear();
ifstream in("all_servers.ini");
if(in)
{
string server="";
int num=0;
while(getline(in,server))
{
num++;
europe_servers.push_back(server);
}
in.close();
ofstream out("log.txt",ios::app);
out<<"Am citit "<<num<<" servere"<<endl;
out.close();
}
else
{
ofstream out("log.txt",ios::app);
out<<"Nu am putut deschide fisierul all_servers.ini"<<endl;
out.close();
}
sleep(60*3);
}
return NULL;
}
void StringExplode(string str, string separator, vector<string>* results)
{
int found;
found = str.find_first_of(separator);
while(found != string::npos)
{
if(found > 0)
{
results->push_back(str.substr(0,found));
}
str = str.substr(found+1);
found = str.find_first_of(separator);
}
if(str.length() > 0)
{
results->push_back(str);
}
}
void sendResponseAll2(sockaddr_in client)
{
char query[6];
bzero(&query,strlen(query));
query[0]=0xff;
query[1]=0xff;
query[2]=0xff;
query[3]=0xff;
query[4]=0x66;
query[5]=0x0A;
char query2[6144]; //6K Stack
bzero(&query2,strlen(query2));
int len=0;
memcpy(query2+len,query,sizeof(query));
len+=sizeof(query);
string server = "";
for(int i=europe_servers.size()/2;i<europe_servers.size();i++)
{
server=europe_servers[i];
//Calculez IPul si Portul fiecarui server...
vector<string> parts;
StringExplode(server,":",&parts);
char *ip_s = (char *)&parts[0][0];
char *port_s = (char *)&parts[1][0];
//Calculez bitii...
unsigned short port = ntohs((unsigned short)atoi(port_s));
unsigned long ip = inet_addr(ip_s);
memcpy(query2+len,&ip,sizeof(ip));
len+=sizeof(ip);
memcpy(query2+len,&port,sizeof(port));
len+=sizeof(port);
}
char q3[] = {0x00,0x00,0x00,0x00,0x00,0x00};
memcpy(query2+len,q3,sizeof(q3));
len+=sizeof(q3);
sendto(sock,query2,len,0,(struct sockaddr *)&client,(unsigned int)sizeof(sockaddr));
}
void sendResponseAll1(sockaddr_in client)
{
char query[6];
bzero(&query,strlen(query));
query[0]=0xff;
query[1]=0xff;
query[2]=0xff;
query[3]=0xff;
query[4]=0x66;
query[5]=0x0A;
char query2[6144]; //6K Stack
bzero(&query2,strlen(query2));
int len=0;
memcpy(query2+len,query,sizeof(query)); // copiaza toti bytes query in query 2
len+=sizeof(query); // len devine , cat query
string server = "";
for(int i=0;i<europe_servers.size()/2;i++)
{
server=europe_servers[i];
//Calculez IPul si Portul fiecarui server...
vector<string> parts;
StringExplode(server,":",&parts);
char *ip_s = (char *)&parts[0][0];
char *port_s = (char *)&parts[1][0];
//Calculez bitii...
unsigned short port = ntohs((unsigned short)atoi(port_s));
unsigned long ip = inet_addr(ip_s);
memcpy(query2+len,&ip,sizeof(ip));
len+=sizeof(ip);
memcpy(query2+len,&port,sizeof(port));
len+=sizeof(port);
}
sendto(sock,query2,len,0,(struct sockaddr *)&client,(unsigned int)sizeof(sockaddr));
}
int main(int argc, char *argv[])
{
//Argumente: port "ip_listening"
port=atoi(argv[1]);
strcpy(ip,argv[2]);
ofstream log("log.txt",ios::app);
pthread_t sql;
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
pthread_attr_setstacksize(&thread_attr, SMALL_STACK);
pthread_create(&sql,&thread_attr,getServers,NULL);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock<0)
{
cout<<"Nu am putut deschide socketul pe portul: "<<port<<'\n';
time_t t = time(0);
tm* lt = localtime(&t);
log<<lt->tm_mday<<"-"<<lt->tm_mon+1<<"-"<<lt->tm_year+1900<<" "<<lt->tm_hour<<":"<<lt->tm_min<<":"<<lt->tm_sec<<" Nu am putut deschide socketul pe portul "<<port<<endl;
}
else
{
cout<<"Socket deschis pe portul: "<<port<<'\n';
time_t t = time(0);
tm* lt = localtime(&t);
log<<lt->tm_mday<<"-"<<lt->tm_mon+1<<"-"<<lt->tm_year+1900<<" "<<lt->tm_hour<<":"<<lt->tm_min<<":"<<lt->tm_sec<<" Socket deschis pe portul "<<port<<endl;
}
struct sockaddr_in binder;
bzero(&binder,sizeof(binder));
binder.sin_family=AF_INET;
binder.sin_addr.s_addr=inet_addr(ip);
binder.sin_port = htons(port);
if(bind(sock,(sockaddr *)&binder,sizeof(binder))>=0)
{
cout<<"Socket bindat pe portul: "<<port<<'\n';
time_t t = time(0);
tm* lt = localtime(&t);
log<<lt->tm_mday<<"-"<<lt->tm_mon+1<<"-"<<lt->tm_year+1900<<" "<<lt->tm_hour<<":"<<lt->tm_min<<":"<<lt->tm_sec<<" Socket bindat pe portul: "<<port<<endl;
}
else
{
cout<<"Nu am putut binda socketul pe portul: "<<port<<'\n';
time_t t = time(0);
tm* lt = localtime(&t);
log<<lt->tm_mday<<"-"<<lt->tm_mon+1<<"-"<<lt->tm_year+1900<<" "<<lt->tm_hour<<":"<<lt->tm_min<<":"<<lt->tm_sec<<" Nu am putut binda socketul pe portul: "<<port<<endl;
}
cout<<"Incep ascultarea!"<<endl;
char buf[2048];
unsigned int fromlen = sizeof(struct sockaddr_in);
while (1)
{
int n = recvfrom(sock,buf,2048,0,(struct sockaddr *)&from,&fromlen);
if(n>0)
{
char ipc[40];
sprintf(ipc,"%s",inet_ntoa(from.sin_addr));
time_t t = time(0);
tm* lt = localtime(&t);
log<<"IPul "<<ipc<<" a trimis requst la "<<lt->tm_mday<<"-"<<lt->tm_mon+1<<"-"<<lt->tm_year+1900<<" "<<lt->tm_hour<<":"<<lt->tm_min<<":"<<lt->tm_sec<<endl;
if(buf[0]==0x31)
{
sendResponseAll1(from);
sendResponseAll2(from);
}
}
sleep(0.5);
}
return 0;
}
/*
g++ -Wall -pthread main2.cpp -o ptr `mysql_config --cflags --libs`
*/