Skip to content

Commit

Permalink
build on MinGW
Browse files Browse the repository at this point in the history
  • Loading branch information
mchome committed Nov 21, 2016
1 parent 7847ab7 commit ac7f927
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ CC = gcc
ifeq ($(debug), y)
CFLAGS += -std=gnu99 -Werror -DDEBUG -g
endif
ifeq ($(win32), y)
CFLAGS += -std=gnu99 -Werror -lws2_32
endif
ifeq ($(test), y)
CFLAGS += -std=gnu99 -Werror -DTEST
else
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ Config file is compatible with [drcom-generic](https://github.com/drcoms/drcom-g

#### To build:

$ make
```bash
$ make # Linux
$ make win32=y # Windows(MinGW)
```

### Thanks:
- [gdut-drcom](https://github.com/chenhaowen01/gdut-drcom 'chenhaowen01')
Expand Down
50 changes: 41 additions & 9 deletions auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#ifdef WIN32
#include <winsock2.h>
typedef int socklen_t;
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif

#include "libs/md4.h"
#include "libs/md5.h"
#include "libs/sha1.h"
Expand All @@ -28,7 +35,7 @@ int challenge(int sockfd, struct sockaddr_in addr, unsigned char seed[]) {
challenge_packet[4] = drcom_config.AUTH_VERSION[0];

sendto(sockfd, challenge_packet, 20, 0, (struct sockaddr *)&addr, sizeof(addr));

if (verbose_flag) {
print_packet("[Challenge sent] ", challenge_packet, 20);
}
Expand Down Expand Up @@ -95,8 +102,8 @@ int login(int sockfd, struct sockaddr_in addr, unsigned char seed[], unsigned ch
memcpy(login_packet + 20, drcom_config.username, strlen(drcom_config.username));
memcpy(login_packet + 56, &drcom_config.CONTROLCHECKSTATUS, 1);
memcpy(login_packet + 57, &drcom_config.ADAPTERNUM, 1);
unsigned long int sum = 0;
unsigned long int mac = 0;
uint64_t sum = 0;
uint64_t mac = 0;
// unpack
for (int i = 0; i < 6; i++) {
sum = (int)MD5A[i] + sum * 256;
Expand All @@ -105,6 +112,7 @@ int login(int sockfd, struct sockaddr_in addr, unsigned char seed[], unsigned ch
for (int i = 0; i < 6; i++) {
mac = (int)drcom_config.mac[i] + mac * 256;
}
printf("%ld\n", mac);
sum ^= mac;
// pack
for (int i = 6; i > 0; i--) {
Expand Down Expand Up @@ -180,7 +188,7 @@ int login(int sockfd, struct sockaddr_in addr, unsigned char seed[], unsigned ch
memcpy(checksum2_str + counter + 2, checksum2_tmp, 6);
memcpy(checksum2_str + counter + 8, drcom_config.mac, 6);
sum = 1234;
unsigned long int ret = 0;
uint64_t ret = 0;
for (int i = 0; i < counter + 14; i += 4) {
ret = 0;
// reverse unsigned char array[4]
Expand Down Expand Up @@ -331,8 +339,8 @@ int pppoe_login(int sockfd, struct sockaddr_in addr, int *pppoe_counter, unsigne
unsigned char crc_tmp[32] = {0};
memcpy(crc_tmp, login_packet, 32);
memcpy(crc_tmp + 24, crc, 8);
unsigned long int ret = 0;
unsigned long int sum = 0;
uint64_t ret = 0;
uint64_t sum = 0;
unsigned char crc2[4] = {0};
if (*encrypt_type == 0) {
for (int i = 0; i < 32; i += 4) {
Expand Down Expand Up @@ -394,18 +402,33 @@ int pppoe_login(int sockfd, struct sockaddr_in addr, int *pppoe_counter, unsigne
}

int dogcom(int try_times, char *mode) {
#ifdef WIN32
WORD sockVersion = MAKEWORD(2, 2);
WSADATA wsaData;
if(WSAStartup(sockVersion, &wsaData) != 0) {
return 1;
}
#endif
int sockfd;

struct sockaddr_in bind_addr;
memset(&bind_addr, 0, sizeof(bind_addr));
bind_addr.sin_family = AF_INET;
#ifdef WIN32
bind_addr.sin_addr.S_un.S_addr = inet_addr(BIND_IP);
#else
bind_addr.sin_addr.s_addr = inet_addr(BIND_IP);
#endif
bind_addr.sin_port = htons(BIND_PORT);

struct sockaddr_in dest_addr;
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
#ifdef WIN32
dest_addr.sin_addr.S_un.S_addr = inet_addr(drcom_config.server);
#else
dest_addr.sin_addr.s_addr = inet_addr(drcom_config.server);
#endif
dest_addr.sin_port = htons(DEST_PORT);

srand(time(NULL));
Expand All @@ -422,9 +445,13 @@ int dogcom(int try_times, char *mode) {
}

// set timeout
#ifdef WIN32
int timeout = 3000;
#else
struct timeval timeout;
timeout.tv_sec = 3;
timeout.tv_usec = 0;
#endif
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
perror("Failed to set sock opt");
return 1;
Expand Down Expand Up @@ -518,7 +545,12 @@ int dogcom(int try_times, char *mode) {
if (logging_flag) {
logging(">>>>> Failed to keep in touch with server, exiting <<<<<", NULL, 0);
}
#ifdef WIN32
closesocket(sockfd);
WSACleanup();
#else
close(sockfd);
#endif
return 1;
}

Expand Down
6 changes: 5 additions & 1 deletion auth.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#ifndef AUTH_H_
#define AUTH_H_

#include <netinet/in.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif

int challenge(int sockfd, struct sockaddr_in addr, unsigned char seed[]);
int login(int sockfd, struct sockaddr_in addr, unsigned char seed[], unsigned char auth_information[]);
Expand Down
44 changes: 23 additions & 21 deletions configparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,49 +51,51 @@ static int read_d_config(char *buf, int size) {

if (strcmp(key, "server") == 0) {
strcpy(drcom_config.server, value);
DEBUG_PRINT(("%s\n", drcom_config.server));
DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.server));
} else if (strcmp(key, "username") == 0) {
strcpy(drcom_config.username, value);
DEBUG_PRINT(("%s\n", drcom_config.username));
DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.username));
} else if (strcmp(key, "password") == 0) {
strcpy(drcom_config.password, value);
DEBUG_PRINT(("%s\n", drcom_config.password));
DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.password));
} else if (strcmp(key, "CONTROLCHECKSTATUS") == 0) {
value = strtok(value, delim2);
sscanf(value, "%02hhx", &drcom_config.CONTROLCHECKSTATUS);
DEBUG_PRINT(("0x%02x\n", drcom_config.CONTROLCHECKSTATUS));
DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.CONTROLCHECKSTATUS));
} else if (strcmp(key, "ADAPTERNUM") == 0) {
value = strtok(value, delim2);
sscanf(value, "%02hhx", &drcom_config.ADAPTERNUM);
DEBUG_PRINT(("0x%02x\n", drcom_config.ADAPTERNUM));
DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.ADAPTERNUM));
} else if (strcmp(key, "host_ip") == 0) {
strcpy(drcom_config.host_ip, value);
DEBUG_PRINT(("%s\n", drcom_config.host_ip));
DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.host_ip));
} else if (strcmp(key, "IPDOG") == 0) {
value = strtok(value, delim2);
sscanf(value, "%02hhx", &drcom_config.IPDOG);
DEBUG_PRINT(("0x%02x\n", drcom_config.IPDOG));
DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.IPDOG));
} else if (strcmp(key, "host_name") == 0) {
strcpy(drcom_config.host_name, value);
DEBUG_PRINT(("%s\n", drcom_config.host_name));
DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.host_name));
} else if (strcmp(key, "PRIMARY_DNS") == 0) {
strcpy(drcom_config.PRIMARY_DNS, value);
DEBUG_PRINT(("%s\n", drcom_config.PRIMARY_DNS));
DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.PRIMARY_DNS));
} else if (strcmp(key, "dhcp_server") == 0) {
strcpy(drcom_config.dhcp_server, value);
DEBUG_PRINT(("%s\n", drcom_config.dhcp_server));
DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.dhcp_server));
} else if (strcmp(key, "AUTH_VERSION") == 0) {
char *v1 = strtok(value, delim2);
char *v2 = strtok(NULL, delim2);
sscanf(v1, "%02hhx", v1);
sscanf(v2, "%02hhx", v2);
memcpy(&drcom_config.AUTH_VERSION[0], v1, 1);
memcpy(&drcom_config.AUTH_VERSION[1], v2, 1);
DEBUG_PRINT(("0x%02x\n", drcom_config.AUTH_VERSION[0]));
DEBUG_PRINT(("0x%02x\n", drcom_config.AUTH_VERSION[1]));
DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.AUTH_VERSION[0]));
DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.AUTH_VERSION[1]));
} else if (strcmp(key, "mac") == 0) {
char *delim3 = "x";
strsep(&value, delim3);
// strsep(&value, delim3);
value = strtok(value, delim3);
value = strtok(NULL, delim3);
sscanf(value, "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
&drcom_config.mac[0],
&drcom_config.mac[1],
Expand All @@ -102,31 +104,31 @@ static int read_d_config(char *buf, int size) {
&drcom_config.mac[4],
&drcom_config.mac[5]);
#ifdef DEBUG
printf("0x");
printf("[PARSER_DEBUG]0x");
for (int i = 0; i < 6; i++) {
printf("%02x", drcom_config.mac[i]);
}
printf("\n");
#endif
} else if (strcmp(key, "host_os") == 0) {
strcpy(drcom_config.host_os, value);
DEBUG_PRINT(("%s\n", drcom_config.host_os));
DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.host_os));
} else if (strcmp(key, "KEEP_ALIVE_VERSION") == 0) {
char *v1 = strtok(value, delim2);
char *v2 = strtok(NULL, delim2);
sscanf(v1, "%02hhx", v1);
sscanf(v2, "%02hhx", v2);
memcpy(&drcom_config.KEEP_ALIVE_VERSION[0], v1, 1);
memcpy(&drcom_config.KEEP_ALIVE_VERSION[1], v2, 1);
DEBUG_PRINT(("0x%02x\n", drcom_config.KEEP_ALIVE_VERSION[0]));
DEBUG_PRINT(("0x%02x\n", drcom_config.KEEP_ALIVE_VERSION[1]));
DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.KEEP_ALIVE_VERSION[0]));
DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.KEEP_ALIVE_VERSION[1]));
} else if (strcmp(key, "ror_version") == 0) {
if (strcmp(value, "True") == 0) {
drcom_config.ror_version = 1;
} else {
drcom_config.ror_version = 0;
}
DEBUG_PRINT(("\n%d\n", drcom_config.ror_version));
DEBUG_PRINT(("\n[PARSER_DEBUG]\n%d\n", drcom_config.ror_version));
} else {
return 1;
}
Expand All @@ -149,15 +151,15 @@ static int read_p_config(char *buf, int size) {

if (strcmp(key, "server") == 0) {
strcpy(drcom_config.server, value);
DEBUG_PRINT(("%s\n", drcom_config.server));
DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.server));
} else if (strcmp(key, "pppoe_flag") == 0) {
value = strtok(value, delim2);
sscanf(value, "%02hhx", &drcom_config.pppoe_flag);
DEBUG_PRINT(("0x%02x\n", drcom_config.pppoe_flag));
DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.pppoe_flag));
} else if (strcmp(key, "keep_alive2_flag") == 0) {
value = strtok(value, delim2);
sscanf(value, "%02hhx", &drcom_config.keep_alive2_flag);
DEBUG_PRINT(("0x%02x\n", drcom_config.keep_alive2_flag));
DEBUG_PRINT(("\n[PARSER_DEBUG]0x%02x\n", drcom_config.keep_alive2_flag));
} else {
return 1;
}
Expand Down
10 changes: 9 additions & 1 deletion keepalive.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>

#ifdef WIN32
#include <winsock2.h>
typedef int socklen_t;
#else
#include <sys/socket.h>
#include <netinet/in.h>
#endif

#include "libs/md4.h"
#include "libs/md5.h"
#include "libs/sha1.h"
Expand Down
2 changes: 0 additions & 2 deletions keepalive.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef KEEPALIVE_H_
#define KEEPALIVE_H_

#include <netinet/in.h>

int keepalive_1(int sockfd, struct sockaddr_in addr, unsigned char seed[], unsigned char auth_information[]);
int keepalive_2(int sockfd, struct sockaddr_in addr, int *keepalive_counter, int *first, int *encrypt_type);
void gen_crc(unsigned char seed[], int encrypt_type, unsigned char crc[]);
Expand Down
7 changes: 4 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "configparse.h"
#include "auth.h"

#define VERSION "1.0.1"
#define VERSION "1.1.0"

void print_help(int exval);

Expand Down Expand Up @@ -36,9 +36,9 @@ int main(int argc, char *argv[]) {
switch (c) {
case 'm':
if (strcmp(optarg, "dhcp") == 0) {
mode = optarg;
mode = strdup(optarg);
} else if (strcmp(optarg, "pppoe") == 0) {
mode = optarg;
mode = strdup(optarg);
} else {
printf("unknown mode\n");
exit(1);
Expand Down Expand Up @@ -95,4 +95,5 @@ void print_help(int exval) {
printf("\t--log <LOGPATH>, -l <LOGPATH> specify log file\n");
printf("\t--verbose, -v set verbose flag\n");
printf("\t--help, -h display this help\n\n");
exit(exval);
}

0 comments on commit ac7f927

Please sign in to comment.