Commit 32f54f23 authored by chenhaowen01's avatar chenhaowen01

add support of windows(mingw)

parent 450b69fc
...@@ -19,6 +19,10 @@ else ...@@ -19,6 +19,10 @@ else
objects+= md5.o md4.o sha1.o objects+= md5.o md4.o sha1.o
endif endif
ifeq ($(LANG),)
LIBS+= -lws2_32
endif
gdut-drcom: $(objects) gdut-drcom: $(objects)
$(CC) *.o -o gdut-drcom $(CFLAGS) $(LIBS) $(CC) *.o -o gdut-drcom $(CFLAGS) $(LIBS)
......
...@@ -2,29 +2,15 @@ ...@@ -2,29 +2,15 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <sys/socket.h> #ifdef WIN32
#include <netinet/in.h> #include <winsock2.h>
#include <arpa/inet.h> //#define SOCKET int
/*
#ifdef _WITH_OPENSSL_
#include <openssl/md5.h>
#include <openssl/md4.h>
#include <openssl/sha.h>
#define _MD5 MD5
#define _MD4 MD4
#define _SHA1 SHA1
#else #else
#include <polarssl/md5.h> #include <sys/socket.h>
#include <polarssl/md4.h> #include <netinet/in.h>
#include <polarssl/sha1.h> #include <arpa/inet.h>
#define SOCKET int
#define _MD5 md5
#define _MD4 md4
#define _SHA1 sha1
#endif #endif
*/
#ifdef __WITH_POLARSSL__ #ifdef __WITH_POLARSSL__
#include <polarssl/md5.h> #include <polarssl/md5.h>
...@@ -73,12 +59,21 @@ static void gen_ka1_checksum(char *checksum, char *seed, unsigned char mode); ...@@ -73,12 +59,21 @@ static void gen_ka1_checksum(char *checksum, char *seed, unsigned char mode);
static void gen_ka2_checksum(char *data, int len, char *checksum); static void gen_ka2_checksum(char *data, int len, char *checksum);
static int32_t drcomCRC32(char *data, int len); static int32_t drcomCRC32(char *data, int len);
static void print_as_hex(char *buf, int len); static void print_as_hex(unsigned char *buf, int len);
/****local functions****/ /****local functions****/
int auth(void) int auth(void)
{ {
#ifdef WIN32
WORD sockVersion = MAKEWORD(2,2);
WSADATA wsaData;
if(WSAStartup(sockVersion, &wsaData)!=0)
{
return 0;
}
#endif
/*variavles of packets*/ /*variavles of packets*/
unsigned char pkt_data[1024] = {0}; //packet data buf unsigned char pkt_data[1024] = {0}; //packet data buf
int length; //packet data length int length; //packet data length
...@@ -94,7 +89,7 @@ int auth(void) ...@@ -94,7 +89,7 @@ int auth(void)
unsigned char kp2_cnt = 0x01; unsigned char kp2_cnt = 0x01;
unsigned char ka2_key[4] = {0}; unsigned char ka2_key[4] = {0};
unsigned char ka2_flag[2] = {0}; unsigned char ka2_flag[2] = {0};
unsigned char rand[2] = {0}; unsigned char rand_num[2] = {0};
/*variables used in keep alive2 paket*/ /*variables used in keep alive2 paket*/
struct sockaddr_in remote_addr; struct sockaddr_in remote_addr;
...@@ -103,15 +98,23 @@ int auth(void) ...@@ -103,15 +98,23 @@ int auth(void)
memset(&remote_addr, 0, sizeof(remote_addr)); memset(&remote_addr, 0, sizeof(remote_addr));
remote_addr.sin_family = AF_INET; remote_addr.sin_family = AF_INET;
#ifdef WIN32
remote_addr.sin_addr.S_un.S_addr = inet_addr(drcom_config.remote_ip);
#else
remote_addr.sin_addr.s_addr = inet_addr(drcom_config.remote_ip); remote_addr.sin_addr.s_addr = inet_addr(drcom_config.remote_ip);
#endif
remote_addr.sin_port=htons(drcom_config.remote_port); remote_addr.sin_port=htons(drcom_config.remote_port);
memset(&local_addr, 0, sizeof(local_addr)); memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET; local_addr.sin_family = AF_INET;
#ifdef WIN32
local_addr.sin_addr.S_un.S_addr = inet_addr("0.0.0.0");
#else
local_addr.sin_addr.s_addr = inet_addr("0.0.0.0"); local_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
#endif
local_addr.sin_port=htons(drcom_config.remote_port); local_addr.sin_port=htons(drcom_config.remote_port);
int client_sockfd; SOCKET client_sockfd;
if ((client_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) if ((client_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{ {
fprintf(stderr, "error!\n"); fprintf(stderr, "error!\n");
...@@ -122,10 +125,14 @@ int auth(void) ...@@ -122,10 +125,14 @@ int auth(void)
int sin_size; int sin_size;
sin_size = sizeof(struct sockaddr_in); sin_size = sizeof(struct sockaddr_in);
bind(client_sockfd, (struct sockaddr *) &local_addr, sin_size); bind(client_sockfd, (struct sockaddr *) &local_addr, sin_size);
#ifdef WIN32
int timeout = 2000;
#else
struct timeval timeout; struct timeval timeout;
timeout.tv_sec=2; timeout.tv_sec=2;
timeout.tv_usec=0; timeout.tv_usec=0;
#endif
setsockopt(client_sockfd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(struct timeval)); setsockopt(client_sockfd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(struct timeval));
...@@ -134,7 +141,7 @@ HEART_BEAT_START: ...@@ -134,7 +141,7 @@ HEART_BEAT_START:
fflush(stdout); fflush(stdout);
kp1_cnt = 1; kp1_cnt = 1;
kp2_cnt = 0; kp2_cnt = 0;
srandom((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
while (1) while (1)
{ {
retry_cnt = 1; retry_cnt = 1;
...@@ -209,14 +216,14 @@ HEART_BEAT_START: ...@@ -209,14 +216,14 @@ HEART_BEAT_START:
kp1_cnt++; kp1_cnt++;
retry_cnt = 0; retry_cnt = 0;
int16_t rand_tmp = random() % 0x10000; int16_t rand_tmp = rand() % 0x10000;
rand[0] = rand_tmp / 0x100; rand_num[0] = rand_tmp / 0x100;
rand[1] = rand_tmp % 0x100; rand_num[1] = rand_tmp % 0x100;
sleep(3); sleep(3);
while (1) while (1)
{ {
length = make_keep_alive2_pkt1(pkt_data, kp2_cnt, ka2_flag, rand, ka2_key); length = make_keep_alive2_pkt1(pkt_data, kp2_cnt, ka2_flag, rand_num, ka2_key);
sendto(client_sockfd, pkt_data, length, 0,\ sendto(client_sockfd, pkt_data, length, 0,\
(struct sockaddr *) &remote_addr, sizeof(remote_addr)); (struct sockaddr *) &remote_addr, sizeof(remote_addr));
fprintf(stdout, "<==[sended kap2_1 request %d] len = %d\n",\ fprintf(stdout, "<==[sended kap2_1 request %d] len = %d\n",\
...@@ -259,7 +266,7 @@ HEART_BEAT_START: ...@@ -259,7 +266,7 @@ HEART_BEAT_START:
while (1) while (1)
{ {
length = make_keep_alive2_pkt2(pkt_data, kp2_cnt, ka2_flag, rand, ka2_key, host_ip); length = make_keep_alive2_pkt2(pkt_data, kp2_cnt, ka2_flag, rand_num, ka2_key, host_ip);
sendto(client_sockfd, pkt_data, length, 0,\ sendto(client_sockfd, pkt_data, length, 0,\
(struct sockaddr *) &remote_addr, sizeof(remote_addr)); (struct sockaddr *) &remote_addr, sizeof(remote_addr));
fprintf(stdout, "<==[sended kap2_2 request %d] len = %d\n", \ fprintf(stdout, "<==[sended kap2_2 request %d] len = %d\n", \
...@@ -292,12 +299,17 @@ HEART_BEAT_START: ...@@ -292,12 +299,17 @@ HEART_BEAT_START:
sleep(17); sleep(17);
} }
#ifdef WIN32
closesocket(client_sockfd);
WSACleanup();
#else
close(client_sockfd); close(client_sockfd);
#endif
return 0; return 0;
} }
static void print_as_hex(char *buf, int len) static void print_as_hex(unsigned char *buf, int len)
{ {
int i; int i;
for (i=0; i<len; i++) for (i=0; i<len; i++)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment