Please help compile script for Openwrt Blackfire Asus WL-500gpv2 .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
//#include <sys/perm.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
//#include <sys/io.h>
#include <syslog.h>
#include <sys/signal.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <time.h>
#ifndef WIN32
#define sleep(x) usleep(x*1000)
#else
#define sleep(x) Sleep(x)
#endif
#ifndef WIN32
typedef int socket_t;
#define SOCKET_NULL -1
#define SOCKET_ERROR -1
#define closesocket(x) close(x)
#else
typedef SOCKET socket_t;
#define SOCKET_NULL INVALID_SOCKET
#endif
#define EXTERNAL_HOST "192.168.0.50"
#define EXTERNAL_PORT 6778
#define INTERNAL_HOST "127.0.0.1"
#define INTERNAL_PORT 6779
socket_t externalSocket = SOCKET_NULL;
socket_t internalSocket = SOCKET_NULL;
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define CLIENTS_MAX 12
const char* addresses[CLIENTS_MAX] = {
"192.168.0.201",
"192.168.0.202",
"192.168.0.203",
"192.168.0.204",
"192.168.0.205",
"192.168.0.206",
"192.168.0.207",
"192.168.0.208",
"192.168.0.209",
"192.168.0.210",
"192.168.0.211",
"192.168.0.212"
};
const char* games[CLIENTS_MAX] = {
"s2",
"rs",
"cm",
"fc",
"ks",
"lh",
"gg",
"rs",
"cm",
"fc",
"ks",
"gm"
};
struct Client
{
socket_t socket;
unsigned char buffer[1000000];
int bytes;
};
Client clients[CLIENTS_MAX];
#define RAMS_PATH "/var/lib/rams/"
int GetClientId(const char* ip)
{
if(ip == 0)
return -1;
for(int i = 0; i < CLIENTS_MAX; i++)
{
if(0 == strcmp(addresses[i], ip))
return i;
}
return -1;
}
#define CMD_RECV_INIT 1
#define CMD_SEND_INIT 1
#define CMD_RECV_SAVE 2
void ReadRam(int id, char* buf)
{
memset(buf, 0, 32768);
char path[1024];
sprintf(path, "%s%i.ram", RAMS_PATH, id);
FILE* file = fopen(path, "rb");
if(file)
{
fread(buf, 32768, 1, file);
fclose(file);
}
}
void WriteRam(int id, const char* data)
{
char path[1024];
sprintf(path, "%s%i.ram", RAMS_PATH, id);
char path_bak[1024];
sprintf(path_bak, "%s%i.bak", RAMS_PATH, id);
FILE* file = fopen(path_bak, "wb");
if(file)
{
fwrite(data, 32768, 1, file);
fclose(file);
rename(path_bak, path);
}
}
void OnRecv(int id)
{
if(clients[id].bytes >= 2)
{
unsigned short bytes = *((unsigned short*)clients[id].buffer); // first 2 bytes - packet size
if(clients[id].bytes >= bytes)
{
if(clients[id].buffer[2] == CMD_RECV_INIT && bytes == 3)
{
printf("Recv CMD_RECV_INIT\n");
unsigned char buf[40000];
char* p = (char*)buf;
*((unsigned short*)p) = 2 + 1 + 2 + 32768;
p+=2;
*p = CMD_SEND_INIT;
p++;
strncpy(p, games[id], 2);
p+=2;
ReadRam(id, p);
send(clients[id].socket, buf, 2 + 1 + 2 + 32768, 0);
// 2 bytes - size
// 1 byte - command
// 2 bytes - game
// 32768 bytyes - ram
}
else if(clients[id].buffer[2] == CMD_RECV_SAVE && bytes == (2 + 1 + 32768))
{
//printf("Recv CMD_RECV_SAVE\n");
// 2 bytes(size) + 1 byte(cmd) + 32768(ram);
WriteRam(id, (const char*)(clients[id].buffer + 3));
}
if(clients[id].bytes >= bytes)
{
int diff = clients[id].bytes - bytes;
for(int i = 0; i < diff; i++)
clients[id].buffer[i] = clients[id].buffer[i + bytes];
clients[id].bytes = diff;
OnRecv(id);
}
}
}
}
void OnClose(int id)
{
if(clients[id].socket != SOCKET_NULL)
{
closesocket(clients[id].socket);
clients[id].socket = SOCKET_NULL;
clients[id].bytes = 0;
}
}
int main()
{
signal(SIGIO, SIG_IGN);
srand(time(NULL));
for(int i = 0; i < CLIENTS_MAX; i++)
{
clients[i].socket = SOCKET_NULL;
clients[i].bytes = 0;
}
printf("Start...\n");
externalSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in externalAdddress;
externalAdddress.sin_family = AF_INET;
externalAdddress.sin_port = ::htons(EXTERNAL_PORT);
externalAdddress.sin_addr.s_addr = ::inet_addr(EXTERNAL_HOST);
bind(externalSocket, (sockaddr*)&externalAdddress, sizeof(externalAdddress));
listen(externalSocket, 0x100);
internalSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in internalAdddress;
internalAdddress.sin_family = AF_INET;
internalAdddress.sin_port = ::htons(INTERNAL_PORT);
internalAdddress.sin_addr.s_addr = ::inet_addr(INTERNAL_HOST);
bind(internalSocket, (sockaddr*)&internalAdddress, sizeof(internalAdddress));
listen(internalSocket, 0x100);
fd_set readfds;
int result = 0;
int nfds = 0;
while(1)
{
struct timeval tm = {2, 0};
FD_ZERO(&readfds);
FD_SET(externalSocket, &readfds);
FD_SET(internalSocket, &readfds);
nfds = MAX(externalSocket, internalSocket);
for(int i = 0; i < CLIENTS_MAX; i++)
{
if(clients[i].socket != SOCKET_NULL)
{
FD_SET(clients[i].socket, &readfds);
nfds = MAX(nfds, clients[i].socket);
}
}
if((result = select(nfds + 1, &readfds, 0,0, &tm)) < 0)
{
printf("error\n");
continue;
}
else if(result == 0) // timeout
{
//printf("timeout\n");
}
else
{
if(FD_ISSET(externalSocket, &readfds)) // external
{
sockaddr_in addr;
socklen_t size = sizeof(addr);
socket_t s = accept(externalSocket, (sockaddr*)&addr, &size);
int id = GetClientId(inet_ntoa((in_addr)addr.sin_addr));
printf("Client %i connected from %s\n", id, inet_ntoa((in_addr)addr.sin_addr));
if(id == -1)
{
closesocket(s);
}
else
{
if(clients[id].socket != SOCKET_NULL)
closesocket(clients[id].socket);
clients[id].socket = s;
clients[id].bytes = 0;
}
}
else if(FD_ISSET(internalSocket, &readfds))
{
printf("Admin connected\n");
sockaddr_in addr;
socklen_t size = sizeof(addr);
socket_t s = accept(internalSocket, (sockaddr*)&addr, &size);
unsigned char data[100];
recv(s, (char*)data, 12, 0);
unsigned int command = *((unsigned int*)data);
unsigned int number = *((unsigned int*)(data + 4));
unsigned int credits = *((unsigned int*)(data + 8));
printf("CMD %i %i %i\n", command, number, credits);
closesocket(s);
if(command == 0) // add
{
if(number > 0 && number <= CLIENTS_MAX)
{
number--;
unsigned char buf[1024];
buf[0] = 4;
*((unsigned int*)(buf + 1)) = credits;
if(clients[number].socket != SOCKET_NULL)
{
printf("Add credits\n");
send(clients[number].socket, (char*)buf, 5, 0);
}
}
}
if(command == 1)
{
if(number > 0 && number <= CLIENTS_MAX)
{
number--;
unsigned char buf[1024];
buf[0] = 3;
if(clients[number].socket != SOCKET_NULL)
{
printf("Del credits\n");
send(clients[number].socket, (char*)buf, 5, 0);
}
}
}
}
else
{
for(int i = 0; i < CLIENTS_MAX; i++)
{
if(clients[i].socket != SOCKET_NULL && FD_ISSET(clients[i].socket, &readfds))
{
unsigned long bytes = 0;
ioctl(clients[i].socket, FIONREAD, &bytes);
if(bytes == 0)
{
OnClose(i);
}
else
{
recv(clients[i].socket, clients[i].buffer + clients[i].bytes, bytes, 0);
clients[i].bytes += bytes;
OnRecv(i);
}
}
}
}
}
}
return 0;
}