求助:关于一个C 写的CGI 更改IP 地址等参数的

我不会Debug 2011-07-17 02:15:01
简述:小弟感觉问题出在CIP()函数中 前面显示解码出的IP netmask参数 是printf()能够显示出来的 就是在CIP()函数不执行更改。。。郁闷 服务器是boa-0.94.13 CIP()函数单独编译在终端执行是可以更改成功 的 希望大家热心帮忙 小弟对C 和CGI都不太熟练 在线等 需要各位热心帮忙
CGI脚本是在浏览器访问时无法执行更改部分  调试证明程序是进入CIP()中了  可是就是没有实现更改IP    会不会是boa访问权限的问题  还是不知道怎样解决。。。
源码:
//修改IP地址、子网掩码、的CGI脚本程序

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <regex.h>


void CIP();

char *getcgidata(FILE *fp, char *requestmethod);


int main(int argc,char *argv[])

{

// printf("Content-type: text/plain; charset=iso-8859-1\n\n");

printf("Content-type: text/html\n\n");



char *input;

char *req_method;

char IPadd[15];

char netmask[15];

char gateway[15];

int i=0;

int j=0;
char *IPa=IPadd;
char *netm=netmask;
char *gatew=gateway;






req_method = getenv("REQUEST_METHOD");

input = getcgidata(stdin,req_method);



// 我们获取的input字符串如下形式

//这是 IPaddr="IPadd"&maskaddr="IPmask"&gateaddr="gateway"

// 其中"IPaddr="和"&maskaddr"和"&gateaddr="是HTML中TEXT的NAME
// 而"IPadd"和"IPmask"和"gateway"也是固定的长度(15位)




for ( i=7; i <(int)strlen(input);i++ )

{

if ( input == '&' )

{

IPadd[j] = '\0';

break;

}

IPadd[j++] = input;

}



for ( i = 17 + strlen(IPadd),j=0; i <(int)strlen(input);i++ )

{

if ( input == '&' )

{

netmask[j] = '\0';

break;

}

netmask[j++] = input;

}



for ( i = 27 + strlen(IPadd) + strlen(netmask),j=0; i <(int)strlen(input);i++ )

{

if ( input == '&' )

{

gateway[j] = '\0';

break;

}

gateway[j++] = input;

}


gateway[j]='\0';

printf("%s\n<br>",IPa);
printf("%s\n<br>",netm);
printf("%s\n<br><br><br>",gatew);

CIP();


printf("<html>\n");

printf("<head>\n");
printf("<title>智能光纤通信实验室</title>\n");

printf("</head>\n");
printf("<body>\n");
printf("<h1 align=\"center\">配置成功</h1>\n");
printf("<p>");

printf("<p>");

printf("<p><a href=\"../html/IP01.htm\">点此返回首页</a></p>");

printf("<H2> Thank You!\n");
printf("</body>\n");
printf("</html>");


return 0;


}

void CIP()
{

struct ifreq req;
strncpy(req.ifr_name, "eth0", IFNAMSIZ );
struct sockaddr_in *sin;

int fd = socket(AF_INET, SOCK_DGRAM, 0 );


if( fd < 0 )
{
perror("error: ");
}

sin = (struct sockaddr_in *)&req.ifr_addr;
sin->sin_family = AF_INET;

if( inet_aton("172.168.133.1", &sin->sin_addr) == 0 ){
perror("inet_aton error: ");
}
if( ioctl( fd, SIOCSIFADDR, &req ) == 0 ){
printf("set ip success!\n");
}else{
perror("ioctl failed: ");
}


req.ifr_flags = IFF_UP;
if( ioctl( fd, SIOCSIFFLAGS, &req ) == 0 ){
printf("set flags success!\n");
}else{
perror("ioctl failed: ");
}

sin->sin_family = AF_INET;
if( inet_aton("172.16.48.255", &sin->sin_addr) == 0 ){
perror("inet_aton error: ");
}
if( ioctl( fd, SIOCSIFBRDADDR, &req ) == 0 ){
printf("set broadcast ip success!\n");
}else{
perror("ioctl failed: ");
}

sin->sin_family = AF_INET;
if( inet_aton("255.255.0.0", &sin->sin_addr) == 0 ){
perror("inet_aton error: ");
}
if( ioctl( fd, SIOCSIFNETMASK, &req ) == 0 ){
printf("set netmask success!\n");
}else{
perror("ioctl failed: ");
}
close( fd );
}



char *getcgidata(FILE *fp, char* requestmethod)

{

char *input;

int len;

int size=124;

int i=0;



if (!strcmp(requestmethod,"GET"))

{

input = getenv("QUERY_STRING");

return input;

}

else if (!strcmp(requestmethod,"POST"))

{

len = atoi(getenv("CONTENT_LENGTH"));

input = (char*)malloc(sizeof(char)*(size + 1));



if(len == 0)

{

input[0] = '\0';

return input;

}



while(1)

{

input = (char)fgetc(fp);

if(i == size)

{

input[i+1] = '\0';

return input;

}



--len;

if (feof(fp)||(!(len)))

{

i++;

input = '\0';

return input;

}

i++;



}

}

return NULL;



}
...全文
147 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
我不会Debug 2011-07-24
  • 打赏
  • 举报
回复
shell 的系统命令是可以执行的,bhdgx 你的意思是用CGI来进行系统调用吗?
Soulic 2011-07-23
  • 打赏
  • 举报
回复
可以调用系统命令试试看

2,204

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 CGI
社区管理员
  • CGI社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧