免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3646 | 回复: 6

PF_PACKET板子上运行报错,socket返回-1,,而且errno=0. [复制链接]

论坛徽章:
0
发表于 2009-12-01 10:36 |显示全部楼层
10可用积分
devfd = socket(PF_PACKET, SOCK_RAW, htons(0));编译能通过,但是在板子上运行报错。socket返回-1,,而且errno=0.

请教,Networking已经配置成支持packet,而且用的是root用户,为什么还是返回-1呀
附件是可以编译的程序,请大侠帮忙测试一下
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/sysinfo.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <netinet/tcp.h>
#include <net/if.h>
#include <net/route.h>
#include <net/if_arp.h>

#include <time.h>
#include "sys/epoll.h"

#include <linux/version.h>//FOR LINUX_VERSION_CODE
#include <linux/if_ether.h>


#define EPOLL_PORT 8004
#define ENABLE_DEUBG

#ifdef ENABLE_DEUBG
#include <stdio.h>
typedef enum{
DL_NONE, //=0
DL_ERROR,
DL_WARNING,
DL_FLOW,
DL_DEBUG,
DL_ALL, //Largest
}EDebugLevle;

#define DEBUG_LEVEL (DL_ALL) // TODO: modify DEBUG Level
#if 1
#define DEBUG(x,fmt,arg...) \
do {\
if ((x) <DEBUG_LEVEL){\
fprintf(stderr, "\n[%s] ", __func__);\
fprintf(stderr, fmt, ##arg);\
}\
}while (0)
#else
#include <syslog.h>
#define DEBUG(x,fmt,arg...) \
do {\
if ((x) <DEBUG_LEVEL){ \
syslog(LOG_INFO, "%s",__func__);\
syslog(LOG_INFO,fmt, ##arg);\
}\
}while (0)
#endif
#else
#define DEBUG(x,fmt,arg...) do {}while(0)
#endif

typedef enum{
eTCP,
eUDP,
eProtocolNr
}EPROTOCOL;

int nListenSock[eProtocolNr];
int nEpollFd;
int nFd;


#define SIN_ADDR(x) (((struct sockaddr_in *)(&(x)))->sin_addr.s_addr)

static int set_promisc(void)
{
#ifndef PC_SIM_ONLY
struct ifreq ifr;
int sock;
struct sockaddr_in *inet_addr_t;
struct rtentry rt;
char sDevName[16];
int i;
for (i=0;i <1;i++)
{
  
inet_addr_t = (struct sockaddr_in *)&ifr.ifr_addr;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
DEBUG(DL_ERROR, "Can't create socket for %s.\n", strerror(errno));
continue;
}

sprintf(ifr.ifr_name, "eth%d", i);// set the device name
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
DEBUG(DL_ERROR,"%s:SIOCGIFFLAGS error for %s\n",ifr.ifr_name, strerror(errno));
close(sock);
continue;
}

/* Close Device */
sprintf(ifr.ifr_name, "eth%d", i);// set the device name
ifr.ifr_flags &= ~IFF_UP;
if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
DEBUG(DL_ERROR,"%s: SIOCSIFFLAGS error for %s",ifr.ifr_name, strerror(errno));
close(sock);
continue;
}

/* Promisc Device */
sprintf(ifr.ifr_name, "eth%d", i);// set the device name
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
DEBUG(DL_ERROR,"%s: SIOCGIFFLAGS error for %s",ifr.ifr_name, strerror(errno));
close(sock);
continue;
}
sprintf(ifr.ifr_name, "eth%d", i);// set the device name
ifr.ifr_flags |= IFF_PROMISC;
if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
DEBUG(DL_ERROR,"%s: SIOCSIFFLAGS error for %s",ifr.ifr_name, strerror(errno));
close(sock);
continue;
}

#if 0
/* set hardware address????????? */
sprintf(ifr.ifr_name, "eth%d", i);// set the device name
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
memcpy(ifr.ifr_hwaddr.sa_data, SysInfoMgr.portInfo.nMac, 6);
if (ioctl(sock, SIOCSIFHWADDR, &ifr) < 0) {//find the device's IP
DEBUG(DL_ERROR,"%s:SIOCSHWADDR error for %s\n",ifr.ifr_name, strerror(errno));
close(sock);
continue;
}
#endif


/* Open Device */
sprintf(ifr.ifr_name, "eth%d", i);// set the device name
ifr.ifr_flags |= IFF_UP;
if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
DEBUG(DL_ERROR,"%s:SIOCSIFFLAGS error for %s\n", ifr.ifr_name,strerror(errno));
close(sock);
continue;
}
#if 0
/* set ip address */
sprintf(ifr.ifr_name, "eth%d", i);// set the device name
inet_addr_t->sin_family = AF_INET;
inet_addr_t->sin_addr.s_addr = (in_addr_t)SysInfoMgr.portInfo.nIP;
if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
DEBUG(DL_ERROR,"%s:SIOCSIFADDR error for %s\n",ifr.ifr_name, strerror(errno));
close(sock);
continue;
}

/* set netmask */
sprintf(ifr.ifr_name, "eth%d", i);// set the device name
inet_addr_t->sin_family = AF_INET;
inet_addr_t->sin_addr.s_addr = (in_addr_t)SysInfoMgr.portInfo.nMask;;
if (ioctl(sock, SIOCSIFNETMASK, &ifr) < 0) {
DEBUG(DL_ERROR,"%s:SIOCSIFNETMASK error for %s\n",ifr.ifr_name, strerror(errno));
close(sock);
continue;
}

/* set gateway */
if (SysInfoMgr.portInfo.nGW)
{
memset(&rt, 0, sizeof(rt));
rt.rt_dst.sa_family = AF_INET;
rt.rt_gateway.sa_family = AF_INET;
snprintf(sDevName,sizeof(sDevName), "eth%d", i);
rt.rt_dev = sDevName;
rt.rt_genmask.sa_family = AF_INET;
SIN_ADDR(rt.rt_genmask) = 0L;
SIN_ADDR(rt.rt_gateway) = (in_addr_t)SysInfoMgr.portInfo.nGW;
rt.rt_flags = RTF_UP | RTF_GATEWAY;
if (ioctl(sock, SIOCADDRT, &rt) < 0) {
if (errno != EEXIST) {
DEBUG(DL_FLOW, "%s:Invalid gateway setting for (%s).\n",sDevName
,strerror(errno));
close(sock);
continue;
}
}
}
#endif

#if 0
if (SysInfoMgr.portInfo.nEnable==0)
{
sprintf(ifr.ifr_name, "eth%d", i);// set the device name
ifr.ifr_flags &= ~IFF_UP;
if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
DEBUG(DL_ERROR,"%s: SIOCSIFFLAGS error for %s",ifr.ifr_name, strerror(errno));
close(sock);
continue;
}
}
#endif
close(sock);
}
#endif
return 0;
}

static int CreateListenSock(EPROTOCOL ePro)
{
int nSock = -1;
int nOptVal = 1;
struct sockaddr_in sServAddr;
//SOCKET
//IPPROTO_TCP|IPPROTO_UDP|IPPROTO_ICMP
//nSock=socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

//|ETH_P_ARP|ETH_P_ALL
setuid(getuid());
//nSock == socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));//0x886C
nSock == socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));//0x886C
if (nSock < 0)
{
printf("create socket failed for nSock=%d,errno=%d,%s\n", nSock,errno, strerror(errno));
goto err_exit;
}
DEBUG(DL_ERROR, "111 socket success for %d\n",nSock);
nListenSock[ePro]=nSock;
return 0;
err_exit:
if (nSock>=0)
{
close(nSock);
}
return -1;
}


/*MAIN LOOP*/
int srv(void)
{
int nRet=-1;
#define EPOLLEVENT_CNT (10)
#define MAXDATALEN (1522 - 14 - 4)
struct epoll_event epollEvent[EPOLLEVENT_CNT];
int nFds;
int i;
int nTimeout=1000*1000; //1s
struct sysinfo info;
char data[MAXDATALEN];
int data_len, length;
struct sockaddr_in from;
int from_len = sizeof(struct sockaddr_in);

//nEpollFd=epoll_create(65535);
//Create Listen FD
i=eTCP;
{
if (CreateListenSock(i) <0)
{
goto EXIT;
}
#if 0
if (EPOLLADD_LISTEN(i))
{
goto EXIT;
}
#endif
}
nRet=0;
while(1)
{
printf("prepare recvfrom:len:%08x\n",length);
length = recvfrom(nListenSock[eTCP], data, MAXDATALEN, 0, (struct sockaddr *)&from, &from_len);
printf("recvfrom:len:%08x\n",length);
if(length == -1)
{
if(errno == EINTR)
continue;
else
break;
}else
{
printf("Received:");
for(i = 0;i < length; i++)
printf("%02x ",data);
}
}

EXIT:
close(nListenSock[eTCP]);
return nRet;
}


int main()
{
//set_promisc();
srv();
return 0;
}

最佳答案

查看完整内容

对了 记得把分给我

论坛徽章:
0
发表于 2009-12-01 10:36 |显示全部楼层
对了 记得把分给我

论坛徽章:
0
发表于 2009-12-01 10:46 |显示全部楼层
帮顶!

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
发表于 2009-12-01 10:55 |显示全部楼层
socket返回-1,,而且errno=0.

你的意思是socket返回-1,但是errno还是0?

论坛徽章:
0
发表于 2009-12-01 11:22 |显示全部楼层
socket那行写了两个等号

论坛徽章:
0
发表于 2009-12-01 13:06 |显示全部楼层
nSock == socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

论坛徽章:
0
发表于 2009-12-01 15:37 |显示全部楼层
我现在突然想抽我自己,这个问题困扰了我4天,用很多系统测试。
很感谢大家,也意识到了自己的粗心,不过最近做项目压力很大,时间紧迫。
分给大家。
谢谢大家了,我的帖子只要我找到答案的我都写出了解决的方法,以便大家都能进步
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP