- Joined
- Aug 1, 2011
- Messages
- 126
- Reaction score
- 93
Hi Ragezone! I releasing my simple connect server source for Linux that you can use to hide first contact IP. Feel free to modify code.
connectserver.c:
How to compile?
Debian:
and run
What will be next? Maybe simple linux gameserver or something.
connectserver.c:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
u_char gameserver_addr[] = "192.168.1.168";
int gameserver_port = 56900;
int server_port = 44405;
int max_clients = 30;
int server_count = 1;
u_char packet_welcome[] = { 0xC1, 0x04, 0x00, 0x01 };
void SetPacket(const uint8_t *source, size_t sourceOffset, uint8_t *result, size_t resultOffset, size_t length)
{
memcpy(result + resultOffset, source + sourceOffset, length);
}
int main(int argc , char *argv[])
{
int opt = 1;
int _ms_ , _ns_, i , valread , sd, max_sd;
int sockets[max_clients];
for (i = 0; i < max_clients; i++){ sockets[i] = 0; }
if( (_ms_ = socket(AF_INET , SOCK_STREAM , 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
if( setsockopt(_ms_, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0 )
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( server_port );
if (bind(_ms_, (struct sockaddr *)&address, sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("Listener on port %d \n", server_port);
if (listen(_ms_, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
int _as_ = sizeof(address);
puts("Waiting for connections ...");
fd_set readfds;
while(1)
{
FD_ZERO(&readfds);
FD_SET(_ms_, &readfds);
max_sd = _ms_;
for ( i = 0 ; i < max_clients ; i++)
{
sd = sockets[i];
if(sd > 0)
FD_SET( sd , &readfds);
if(sd > max_sd)
max_sd = sd;
}
int activity = select( max_sd + 1 , &readfds , NULL , NULL , NULL);
if ((activity < 0) && (errno!=EINTR))
{
printf("select error");
}
if (FD_ISSET(_ms_, &readfds))
{
if ((_ns_ = accept(_ms_, (struct sockaddr *)&address, (socklen_t*)&_as_))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
printf("New client , socket fd is %d , ip is : %s , port : %d \n" , _ns_ , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
if( send(_ns_, packet_welcome, sizeof(packet_welcome), 0) != sizeof(packet_welcome) )
{
perror("send");
}
puts("Welcome message sent successfully");
for (i = 0; i < max_clients; i++)
{
if( sockets[i] == 0 )
{
sockets[i] = _ns_;
printf("Adding to list of sockets as %d\n" , i);
break;
}
}
}
for (i = 0; i < max_clients; i++)
{
sd = sockets[i];
if (FD_ISSET( sd , &readfds))
{
u_char buffer[1025];
memset(buffer,0,strlen(buffer));
if ((valread = read( sd , buffer, 1024)) == 0)
{
getpeername(sd , (struct sockaddr*)&address , (socklen_t*)&_as_);
printf("Host disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
close( sd );
sockets[i] = 0;
}
else
{
switch ((u_char)buffer[0])
{
case 0xC1:
switch ((u_char)buffer[2])
{
case 0xF4:
switch ((u_char)buffer[3])
{
case 0x03:
{
int server_code = ((u_char)buffer[4] + (u_char)buffer[5] * 256);
int server_index = (server_code / 20);
int sub_index = (server_code % 20);
u_char packet_info[] = { 0xC1, 0x16, 0xF4, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (u_char)(gameserver_port & 0xFF), (u_char)(gameserver_port >> 8) };
SetPacket(gameserver_addr, 0, packet_info, 4, sizeof(gameserver_addr));
send(sd, packet_info, sizeof(packet_info), 0);
}
break;
case 0x06:
{
u_char server_list[2000];
int packet_size = 7 + (4 * server_count);
u_char packet_list[] = { 0xC2, (u_char)(packet_size >> 8), (u_char)(packet_size & 0xFF), 0xF4, 0x06, (u_char)(server_count << 8), (u_char)(server_count & 0xFF) };
SetPacket(packet_list, 0, server_list, 0, 7);
int temp = 7;
for (int i = 0; i < 21; i++)
{
for (int z = 0; z < 20; z++)
{
server_list[temp++] = (u_char)((i * 20 + z) & 0xFF );
server_list[temp++] = (u_char)((i * 20 + z) >> 8);
server_list[temp++] = 0;
server_list[temp++] = 0;
}
}
send(sd, server_list, sizeof(server_list), 0);
}
break;
}
break;
}
break;
}
}
}
}
}
return 0;
}
How to compile?
Debian:
Code:
apt-get install gcc
Code:
gcc connectserver.c -o connectserver
and run
Code:
./connectserver
What will be next? Maybe simple linux gameserver or something.
Last edited by a moderator: