Skip to content

Commit

Permalink
Merge branch 'drcom' into cli
Browse files Browse the repository at this point in the history
  • Loading branch information
leetking authored and leetking committed Nov 12, 2016
2 parents f9e6733 + 5e6905c commit 572ef04
Show file tree
Hide file tree
Showing 12 changed files with 1,287 additions and 50 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ IS_DEBUG := DEBUG
#如果是, 就取GUI,否则就是空
IS_GUI :=

VERSION := 0.0.3.4
VERSION := 0.0.4.0
CONFIG := ./drcomrc
ifneq "$(IS_GUI)" ""
RES := resource
Expand Down Expand Up @@ -64,7 +64,7 @@ endif #IS_GUI
else
CFLAGS += -DLINUX
INSTALL := install
OBJS += eapol.o
OBJS += eapol.o drcom.o
endif

ifeq ($(IS_DEBUG), DEBUG)
Expand All @@ -79,7 +79,7 @@ ifeq ($(IS_GUI), GUI)
LDFLAGS += $(LDFLAGSS_GUI)
OBJS += main_gui.o gui.o
else
OBJS += main_cli.o wrap_eapol.o
OBJS += main_cli.o wrap_eapol.o dhcp.o
endif

all: drcom
Expand Down Expand Up @@ -134,7 +134,7 @@ help:
@echo " gui: GUI or empty(don't type it. e.g. 'IS_GUI=')"

clean:
$(RM) *.o netif-config.exe netif-config drcom dist
$(RM) *.o netif-config.exe netif-config drcom dist gmon.out
dist-clean: clean
$(RM) cscope.* tags dist
$(RM) $(APP)*
Expand Down
110 changes: 109 additions & 1 deletion common.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
/*
* 一些通用的代码
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include "common.h"

#ifdef LINUX
# include <unistd.h>
# include <arpa/inet.h>
# include <net/ethernet.h>
# include <sys/select.h>
# include <net/if_arp.h>
# include <net/if.h>
# include <sys/ioctl.h>
# include <netpacket/packet.h>
# define PATH_SEP '/'
#elif defined(WINDOWS)
# include <windows.h>
Expand Down Expand Up @@ -187,3 +195,103 @@ extern int copy(char const *f1, char const *f2)

return 0;
}
/*
* 本地是否是小端序
* @return: !0: 是
* 0: 不是(大端序)
*/
static int islsb()
{
static uint16 a = 0x0001;
return (int)(*(uchar*)&a);
}
static uint16 exorders(uint16 n)
{
return ((n>>8)|(n<<8));
}
static uint32 exorderl(uint32 n)
{
return (n>>24)|((n&0x00ff0000)>>8)|((n&0x0000ff00)<<8)|(n<<24);
}
extern uint16 htols(uint16 n)
{
return islsb()?n:exorders(n);
}
extern uint16 htoms(uint16 n)
{
return islsb()?exorders(n):n;
}
extern uint16 ltohs(uint16 n)
{
return islsb()?n:exorders(n);
}
extern uint16 mtohs(uint16 n)
{
return islsb()?exorders(n):n;
}
extern uint32 htoll(uint32 n)
{
return islsb()?n:exorderl(n);
}
extern uint32 htoml(uint32 n)
{
return islsb()?exorderl(n):n;
}
extern uint32 ltohl(uint32 n)
{
return islsb()?n:exorderl(n);
}
extern uint32 mtohl(uint32 n)
{
return islsb()?exorderl(n):n;
}
extern uchar const *format_mac(uchar const *macarr)
{
static uchar formatmac[] =
"xx:xx:xx:xx:xx:xx";
if (NULL == macarr)
return NULL;
sprintf((char*)formatmac, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
macarr[0], macarr[1], macarr[2],
macarr[3], macarr[4], macarr[5]);
return formatmac;
}

#ifdef LINUX
/*
* 返回t1-t0的时间差
* 由于这里精度没必要达到ns,故返回相差微秒ms
* @return: 时间差,单位微秒(1s == 1000ms)
*/
extern long difftimespec(struct timespec t1, struct timespec t0)
{
long d = t1.tv_sec-t0.tv_sec;
d *= 1000;
d += (t1.tv_nsec-t0.tv_nsec)/(long)(1e6);
return d;
}

/*
* 判断网络是否连通
* 最长延时3s,也就是说如果3s内没有检测到数据回应,那么认为网络不通
* TODO 使用icmp协议判断
* @return: !0: 连通
* 0: 没有连通
*/
extern int isnetok(char const *ifname)
{
static char baidu[] = "baidu.com";
return 1;
}

/*
* 休眠ms微秒
*/
extern void msleep(long ms)
{
struct timeval tv;
tv.tv_sec = ms/1000;
tv.tv_usec = ms%1000*1000;
select(0, 0, 0, 0, &tv);
}
#endif /* LINUX */
37 changes: 37 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef unsigned int uint32; /* 四个字节 */
#ifdef LINUX
# include <linux/limits.h>
# include <netinet/if_ether.h>
# include <arpa/inet.h>
# include <net/if.h>
# define EXE_PATH_MAX (PATH_MAX+1)
#elif defined(WINDOWS)
Expand Down Expand Up @@ -106,4 +107,40 @@ extern char const *format_time(void);
*/
extern int copy(char const *src, char const *dst);

/*
* 字节序转换相关函数
* host to lsb/msb short/long (host->l/m)
* lsb/msb to host short/long (l/m->host)
*/
extern uint16 htols(uint16 n);
extern uint16 htoms(uint16 n);
extern uint16 ltohs(uint16 n);
extern uint16 mtohs(uint16 n);

extern uint32 htoll(uint32 n);
extern uint32 htoml(uint32 n);
extern uint32 ltohl(uint32 n);
extern uint32 mtohl(uint32 n);

extern uchar const *format_mac(uchar const *mac);

/*
* 判断网络是否连通
* ifname: 接口名字
* @return: !0: 连通
* 0: 没有连通
*/
extern int isnetok(char const *ifname);
/*
* 返回t1-t0的时间差
* 由于这里精度没必要达到ns,故返回相差微秒ms
* @return: 时间差,单位微秒(1s == 1000ms)
*/
extern long difftimespec(struct timespec t1, struct timespec t0);

/*
* 休眠ms微秒
*/
extern void msleep(long ms);

#endif
86 changes: 86 additions & 0 deletions dhcp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <time.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "dhcp.h"
#include "common.h"

/*
* TODO 这里没有实现dhcp协议,只是把接口留在这里了。
* 使用了一种替代方案来使程序工作
*/

/* TODO 暂时为我们学校固定 */
#define SERIP "10.255.0.195"

/*
* 为s设置接口名字
* 使用s进行dhcp获取ip时,**必须**先指明接口
* @return: 0: 成功
* -1: 失败
*/
int dhcp_setif(dhcp_t *s, char const *ifname)
{
strncpy(s->ifname, ifname, IFNAMSIZ);
_D("ifname: %s\n", s->ifname);
return 0;
}
/*
* 从s里获取dhcp服务器ip信息
* s: dhcp_t结构体
* type: AF_INET: ipv4; AF_INET6: ipv6
* serip: 如果是AF_INET,这是一个struct in_addr指针
* 如果是AF_INET6, 这是一个struct in6_addr指针
* @return: 0: 成功
* -1: s没有需要获取的信息
*/
int dhcp_getsip(dhcp_t *s, int type, void *serip)
{
memcpy(serip, &s->_data._v4._serip, sizeof(struct in_addr));
return 0;
}
/* 获取到的ip, 和上述类似 */
int dhcp_getcip(dhcp_t *s, int type, void *clip)
{
memcpy(clip, &s->_data._v4._clip, sizeof(struct in_addr));
return 0;
}
/* 获取获取到ip的subnet mask, 和上述类似 */
int dhcp_getmask(dhcp_t *s, int type, void *mask)
{
memcpy(mask, &s->_data._v4._mask, sizeof(struct in_addr));
return 0;
}
/*
* 执行dhcp协议获取可用ip和mask信息
* tip需要给出接口信息
* 结果存储在tip里
* @return: 0: 成功
* 1: 没有这个接口
* 2: 网络并不通,没有服务器响应
* -1: 超时
*/
int dhcp_run(dhcp_t *tip)
{
struct in_addr ip;
inet_pton(AF_INET, SERIP, &ip);
memcpy(&tip->_data._v4._serip, &ip, sizeof(ip));
struct timespec stime, etime;
/*
* 每3秒检测网络是否通
* 直到一个超过DHCP_TIMEOUT时间
*/
clock_gettime(CLOCK_MONOTONIC, &stime);
clock_gettime(CLOCK_MONOTONIC, &etime);
while (!isnetok(tip->ifname) && difftimespec(etime, stime) <= 1000*DHCP_TIMEOUT) {
sleep(3);
clock_gettime(CLOCK_MONOTONIC, &etime);
}
if (difftimespec(etime, stime) > 1000*DHCP_TIMEOUT) {
return -1;
}

return 0;
}

65 changes: 65 additions & 0 deletions dhcp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef _DHCP_H
#define _DHCP_H

#include <arpa/inet.h>
#include <net/if.h>

/*
* dhcp请求超时
* 单位秒s
*/
#define DHCP_TIMEOUT (120)

/*
* 信息存储结构
*/
typedef struct dhcp_t {
char ifname[IFNAMSIZ];
union {
struct { /* ipv4 */
struct in_addr _serip;
struct in_addr _clip;
struct in_addr _mask;
}_v4;
struct { /* ipv6 */
struct in6_addr _serip;
struct in6_addr _clip;
struct in6_addr _mask;
}_v6;
}_data;
}dhcp_t;

/*
* 为s设置接口名字
* NOTE 使用s进行dhcp获取ip时,**必须**先指明接口
* @return: 0: 成功
* -1: 失败
*/
int dhcp_setif(dhcp_t *s, char const *ifname);
/*
* 从s里获取dhcp服务器ip信息
* s: dhcp_t结构体
* type: AF_INET: ipv4; AF_INET6: ipv6
* serip: 如果是AF_INET,这是一个struct in_addr指针
* 如果是AF_INET6, 这是一个struct in6_addr指针
* @return: 0: 成功
* -1: s没有需要获取的信息
*/
int dhcp_getsip(dhcp_t *s, int type, void *serip);
/* 获取到的ip, 和上述类似 */
int dhcp_getcip(dhcp_t *s, int type, void *clip);
/* 获取获取到ip的subnet mask, 和上述类似 */
int dhcp_getmask(dhcp_t *s, int type, void *mask);

/*
* 执行dhcp协议获取可用ip和mask信息
* tip需要给出接口信息
* 结果存储在tip里
* @return: 0: 成功
* 1: 没有这个接口
* 2: 网络并不通,没有服务器响应
* -1: 超时
*/
int dhcp_run(dhcp_t *tip);

#endif
Loading

0 comments on commit 572ef04

Please sign in to comment.