本篇文章给大家谈谈linux下域名劫持c语言实现,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、Linux下如何通过编程得到DNS ip地址和默认网关ip地址。(C 语言)2、linux下编码转换问题,C语言实现,使用iconv函数族3、写一个C语言程序: 能修改Linux 主机的IP、DHCP、DNS 等设置4、linux中cp命令如何用 C语言实现5、如何用C语言获取Linux系统配置的DNS地址6、Linux中怎么用C语言打开网页
Linux下如何通过编程得到DNS ip地址和默认网关ip地址。(C 语言)
方案一:
你用C语言去读取/etc/resolv.conf,格式是namserver *.*.*.* 提取以下 就有DNS了
至于网关 你可以去读取/etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0等文件,但是各个Linux系统可能少有差别。
方案二:
在C程序中调用exec函数,执行route命令,从返回的结果中提取网关
至于DNS 也可以通过dig www.easyaq.com, 从结果中提取DNS 但是就没有方案一中快了
linux下编码转换问题,C语言实现,使用iconv函数族
1、iconv的含义是将一个抽象的符号的编码进行转换。
但是如果一个符号比如“个”,可能在BIG5的编码中不存在(繁体字中不同)
GBK包含的是简体字,BIG5包含的是繁体字,Unicode包含全部,
所以
GBK-Unicode,Big5-Unicode (总是OK)
Unicode-GBK (当里面仅包含英文及简体时OK)
Unicode-BIG5 (当里面仅包含英文及繁体时OK)
GBK-Big5 (基本上不行,除非某些字没有特别的简体字)
GBK-Big5是汉字的简繁转换,不是编码转换,简体字转繁体字还有一个问题,一个简体字可能是对应多个繁体字,这种很难转换正确。繁体字转换成简体字相对难度低。
2、#include iconv.h
size_t iconv(iconv_t cd,
char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft);
函数原型, outbuf是一个 char **类型
在函数手册中:
The iconv() function converts one multibyte character at a time, and for each character conversion it increments *inbuf and decrements
*inbytesleft by the number of converted input bytes, it increments *outbuf and decrements *outbytesleft by the number of converted
output bytes
写一个C语言程序: 能修改Linux 主机的IP、DHCP、DNS 等设置
可以调用system()函数。把linux系统命令拼成字符串,然后调用system去执行这个字符串就可以了。修改系统配置,需要root身份。
1、system函数:
原型:int system(const char * command);
功能:执行 dos(windows系统) 或 shell(Linux/Unix系统) 命令,参数字符串command为命令名;
说明:在windows系统中,system函数直接在控制台调用一个command命令。在Linux/Unix系统中,system函数会调用fork函数产生子进程,由子进程来执行command命令,命令执行完后随即返回原调用的进程;
头文件:stdlib.h;
返回值:命令执行成功返回0,执行失败返回-1。
2、例程:
#includestdio.h
#includestdlib.h
int main(){
system(“del C:\\123.txt”);//在控制台中,执行命令del C:\\123.txt,删除C盘目录下的123.txt文件
return 0;
}
linux中cp命令如何用 C语言实现
1,首先需要了解cp的原理。
2,可以参考cp的源码去了解其原理
3,cp命令的源码可以在linux内核中找到。
4,或者下载busybox其中也会有cp的源码
只有了解其原理之后才能谈如何实现。参考代码如下:
#include stdio.h
#include stdlib.h
#include sys/stat.h
#include sys/types.h
#include fcntl.h
#include errno.h
#include unistd.h
#include string.h
#define BUF_SIZE 1024
#define PATH_LEN 128
void my_err(char *err_string, int line )
{
fprintf(stderr,”line:%d “,line);
perror(err_string);
exit(1);
}
void copy_data(const int frd,const int fwd)
{
int read_len = 0, write_len = 0;
unsigned char buf[BUF_SIZE], *p_buf;
while ( (read_len = read(frd,buf,BUF_SIZE)) ) {
if (-1 == read_len) {
my_err(“Read error”, __LINE__);
}
else if (read_len 0) { //把读取部分写入目标文件
p_buf = buf;
while ( (write_len = write(fwd,p_buf,read_len)) ) {
if(write_len == read_len) {
break;
}
else if (write_len 0) { //只写入部分
p_buf += write_len;
read_len -= write_len;
}
else if(-1 == write_len) {
my_err(“Write error”, __LINE__);
}
}
if (-1 == write_len) break;
}
}
}
int main(int argc, char **argv)
{
int frd, fwd; //读写文件描述符
int len = 0;
char *pSrc, *pDes; //分别指向源文件路径和目标文件路径
struct stat src_st,des_st;
if (argc 3) {
printf(“用法 ./MyCp 源文件路径 目标文件路径\n”);
my_err(“arguments error “, __LINE__);
}
frd = open(argv[1],O_RDONLY);
if (frd == -1) {
my_err(“Can not opne file”, __LINE__);
}
if (fstat(frd,src_st) == -1) {
my_err(“stat error”,__LINE__);
}
/*检查源文件路径是否是目录*/
if (S_ISDIR(src_st.st_mode)) {
my_err(“略过目录”,__LINE__);
}
pDes = argv[2];
stat(argv[2],des_st);
if (S_ISDIR(des_st.st_mode)) { //目标路径是目录,则使用源文件的文件名
len = strlen(argv[1]);
pSrc = argv[1] + (len-1); //指向最后一个字符
/*先找出源文件的文件名*/
while (pSrc = argv[1] *pSrc != ‘/’) {
pSrc–;
}
pSrc++;//指向源文件名
len = strlen(argv[2]);
// . 表示复制到当前工作目录
if (1 == len ‘.’ == *(argv[2])) {
len = 0; //没有申请空间,后面就不用释放
pDes = pSrc;
}
else { //复制到某目录下,使用源文件名
pDes = (char *)malloc(sizeof(char)*PATH_LEN);
if (NULL == pDes) {
my_err(“malloc error “, __LINE__);
}
strcpy(pDes,argv[2]);
if ( *(pDes+(len-1)) != ‘/’ ) { //目录缺少最后的’/’,则补上’/‘
strcat(pDes,”/”);
}
strcat(pDes+len,pSrc);
}
}
/* 打开目标文件, 使权限与源文件相同*/
fwd = open(pDes,O_WRONLY | O_CREAT | O_TRUNC,src_st.st_mode);
if (fwd == -1) {
my_err(“Can not creat file”, __LINE__);
}
copy_data(frd,fwd);
//puts(“end of copy”);
if (len 0 pDes != NULL)
free(pDes);
close(frd);
close(fwd);
return 0;
}
如何用C语言获取Linux系统配置的DNS地址
用system(“ifconfig dns.txt”)命令保存IP配置,然后你直接读取dns.txt就可以查看所有配置了,包括ip, mac,DNS
Linux中怎么用C语言打开网页
给你一个哈,我自己调试好的,并且加了详细注释~~记得给分啊,我没分问问题了~
#include stdlib.h
#include stdio.h
#include unistd.h
#include fcntl.h
#include sys/types.h
#include sys/socket.h
#include netinet/in.h
#include errno.h
#include time.h
#include sys/types.h
#include sys/ioctl.h
#include string.h
#include stdarg.h
#include netdb.h
#include setjmp.h
#include signal.h
/*gethostbyname 超时返回
这里使用的办法是设置一个时钟,如果gethostbyname在指定的时间内尚未返回,
时钟会强制其返回,得到的返回值显然是空指针,等价于告诉用户主机未连如互联网或者该域名无法解析。*/
static sigjmp_buf jmpbuf;
static void alarm_func() //该函数执行之后会执行跳转
{
siglongjmp(jmpbuf, 1);
}
static struct hostent *gngethostbyname(char *HostName, int timeout)
{
struct hostent *lpHostEnt;
signal(SIGALRM, alarm_func); //接受alarm信号,然后调用函数
if(sigsetjmp(jmpbuf, 1) != 0)//跳转目的地
{
alarm(0);//timout
signal(SIGALRM, SIG_IGN);
return NULL;
}
alarm(timeout);//setting alarm
printf(“\nwill gethost!\n”);
lpHostEnt = gethostbyname(HostName);
signal(SIGALRM, SIG_IGN);
return lpHostEnt;
}
/*(linux socket编程实现connect超时的一种方法
创建套接字,将其设置成非阻塞状态。
调用connect连接对端主机,如果失败,判断当时的errno是否为EINPROGRESS,也就是说是不是连接正在进行中,如果是,转到步骤3,如果不是,返回错误。
用select在指定的超时时间内监听套接字的写就绪事件,如果select有监听到,证明连接成功,否则连接失败。*/
int main(int argc, char *argv[])
{
//最好检查一下参数,要求传入3个参数 URL PORT TIMEOUT(connect send recv 3个参数的超时)
int fd, retval,res,error;
struct sockaddr_in addr;
struct timeval timeo = {15, 0}; //time ou struct
struct hostent *site;
socklen_t len = sizeof(timeo);
fd_set set;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (argc == 4)
timeo.tv_sec = atoi(argv[3]);
site=gngethostbyname(argv[1],3); //解析域名的超时设置,测试域名超时,可以写一个可以ping的通但是没有办法解析域名
//的IP地址到resolv.conf里面,然后加上一个默认路由,直接PING一个百度,就能发现如果不加超时机制就会一直卡在那里
if(NULL == site)
{
printf(“\ncan not find the site!\n”);
return -2;
}
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); //设置为非阻塞模式
addr.sin_family = AF_INET;
//addr.sin_addr.s_addr = inet_addr(argv[1]);
memcpy(addr.sin_addr, site-h_addr_list[0], site-h_length);
addr.sin_port = htons(atoi(argv[2]));
printf(“%d\n”, time(NULL));
/*if (connect(fd, (struct sockaddr*)addr, sizeof(addr)) == 0) {
printf(“connected1\n”);
// return 0;
}*/
//res=connect(fd, (struct sockaddr*)addr, sizeof(addr));
//printf(“\nconnect result:[%d]\n”,res);
if (connect(fd, (struct sockaddr*)addr, sizeof(addr)) != 0)
{
//调用一次系统函数失败后直接看errno,确定是什么问题,下面的代码可以实现在没有默认路由的情况下直接返回失败.
if (errno != EINPROGRESS) {
printf(“connect:normal network unreach!!”);
return -1;
}
printf(“\nwill select\n”);
FD_ZERO(set);/*将set清零使集合中不含任何fd*/
FD_SET(fd,set); /*将一个给定的文件描述符加入集合之中*/
retval = select(fd + 1, NULL, set, NULL, timeo);
if (retval == -1) {
printf(“select”);
return -1;
} else if(retval == 0) {
printf(“timeout\n”); //这样的select等于是变成了再timeout时间内是阻塞模式,超过timeout就直接返回
printf(“%d\n”, time(NULL));
return 0;
}
else
{
printf(“connected—:[%d]\n”,retval);
getsockopt(fd, SOL_SOCKET, SO_ERROR, error, (socklen_t *)len); //判断在connected成功之后,获取套接口目前的一些信息来判断是否真的是连接上了,返回0表示真的连上了
printf(“error—:[%d]\n”,error);
if(0!=error)
return -1;
}
}
int ul = 0;
ioctl(fd, FIONBIO, ul); //设置为阻塞模式
//return 0;
setsockopt(fd,SOL_SOCKET,SO_SNDTIMEO,(char *)timeo,sizeof(timeo));
setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,(char *)timeo,sizeof(timeo));
printf(“\nbefore\n”);
sleep(5); //在sleep 5的时候,拔掉网线就可以测试出recv超时的功能,如果不加recv 的超时功能,拔掉网线后就会一直卡在那里,当然你在实际应用的时候没必要加这个
printf(“\nafter\n”);
char *msg=”GET / HTTP/1.0\r\n\r\n”;
if( send(fd, msg, strlen(msg), 0)0 )
{
printf(“error in send msg\n”);
exit(1);
}
int i=0;
char buf[1000];
while((recv(fd,buf,1000,MSG_WAITALL))0)
{
printf(“[%d]:[%s]”,i,buf);
i++;
}
printf(“\n——end———\n”);
close(fd);
return;
}
关于linux下域名劫持c语言实现和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。