- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
运行此程序后,它会抛出 Errno-101。我只是想使用 DGRAM 套接字发送一个广播数据包。仅此而已。有人可以帮助我理解这段代码中的错误吗??
//###########ALGORITHM FOR CHECKSUM CALCULATION################################
unsigned short csum(unsigned short*buf,int nwords) {
unsigned long sum;
for(sum=0; nwords>0;nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum &0xffff);
sum += (sum >>16);
return (unsigned short) (~sum);
}
//##############################################################################
int main() {
//######################### Variable Declarataions#################################
int optval,optlen,opt_broadcast = 1,one=1;
const int* val = &one;
int sockfd,bytes_read,bytes_send,addr_len,n,i,j,dhcp_msg_type,fn_return = -5,z=0,temp;
unsigned char txmsg[1024],buffer[10],original[1024];
unsigned char* rxmsg;
socklen_t abc;
char str[50];
struct sockaddr_in server_addr,client_addr,recv_addr;
struct iphdr *iph,*iph_trans;struct udphdr *udph,*udph_trans;
BOOTP *dhcp_ptr;
//##########################################################################################
//######################Original Implementation#############################################
if((sockfd = socket(PF_INET,SOCK_RAW,IPPROTO_UDP)) < 0) {
perror("Error in Socket Creation \n");
exit(1);
}
fn_return = setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&opt_broadcast,sizeof(opt_broadcast));
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
if(setsockopt(sockfd,IPPROTO_IP,IP_HDRINCL,val,sizeof(one)) < 0) {
perror("Socket() Error \n");
}
client_addr.sin_family=AF_INET;
client_addr.sin_port=htons(68);
client_addr.sin_addr.s_addr=INADDR_BROADCAST;
bzero(&(client_addr.sin_zero),8);
addr_len = sizeof(client_addr);
bzero(txmsg,sizeof(txmsg));
//####################Packet Receiving#####################################################
while(1) {
bytes_read = recvfrom(sockfd, original,4096,0,(struct sockaddr*)&recv_addr, &addr_len);
printf("\n\n******************STARTING HERE***************\n");
printf("No:of Bytes Read are %d \n",bytes_read);
/*for(i=0;i<=bytes_read;i++) {
printf("%d-%x:",i,rxmsg[i]);
} */
iph = (struct iphdr*)original;
udph = (struct udphdr*)(original + (4*(iph->ihl)));
//printf("The Value of iph is %p \n",iph);
//printf("The Value of udph is %p \n",udph);
printf("The Value in UDP Header Source Port was %d \n",ntohs(udph->source));
printf("The Value in UDP Header Destination Port was %d \n",ntohs(udph->dest));
iph_trans = (struct iphdr*)txmsg;
//udph_trans = (struct udphdr*)(txmsg + sizeof(struct iphdr));
udph_trans = (struct udphdr*)(txmsg + (4*(iph->ihl)));
dhcp_ptr = (BOOTP *)(txmsg + sizeof(struct iphdr) + sizeof(struct udphdr));
//Filling the IP Header & UDP Header
iph_trans->ihl=5;
iph_trans->version=4;
iph_trans->tos=16;
iph_trans->tot_len=sizeof(struct iphdr) + sizeof(struct udphdr);
iph_trans->id=htons(54321);
//iph_trans->frag_off = ;
iph_trans->ttl=64;
iph_trans->protocol=17;
iph_trans->saddr=inet_addr("192.168.117.129");
iph_trans->daddr=client_addr.sin_addr.s_addr;
udph_trans->source=htons(67);
udph_trans->dest=htons(68);
udph_trans->len=htons(sizeof(struct udphdr));
//udph_trans->check=0;
//htons(csum((unsigned short*)dhcp_ptr,sizeof(struct iphdr)+sizeof(struct udphdr)));
if(ntohs(udph->dest) == 67) {
rxmsg = original+28;
if(rxmsg[240] == 53 && rxmsg[242] == 1) {
printf("DHCP DISCOVER Packet was received....\n");
//printf("The Value of txmsg was %p \n",txmsg);
//printf("The Value of iph is %p \n",iph_trans);
//printf("The Value of udph is %p \n",udph_trans);
//printf("The Value of dhcp_ptr is %p \n",dhcp_ptr);
dhcp_ptr->op = 2;
dhcp_ptr->htype = 1;
dhcp_ptr->hlen = 6;
dhcp_ptr->hops = 2;
dhcp_ptr->xid[0] = (unsigned char)rxmsg[4];
dhcp_ptr->xid[1] = (unsigned char)rxmsg[5];
dhcp_ptr->xid[2] = (unsigned char)rxmsg[6];
dhcp_ptr->xid[3] = (unsigned char)rxmsg[7];
dhcp_ptr->secs = 0;
dhcp_ptr->flags= 0;
inet_pton(AF_INET,"0.0.0.0",&(dhcp_ptr->ciaddr.s_addr));
inet_pton(AF_INET,"8.5.5.5",&(dhcp_ptr->yiaddr.s_addr));
inet_pton(AF_INET,"192.168.117.129",&(dhcp_ptr->siaddr.s_addr));
inet_pton(AF_INET,"0.0.0.0",&(dhcp_ptr->giaddr.s_addr));
dhcp_ptr->chaddr[0]=(unsigned char)rxmsg[28];
dhcp_ptr->chaddr[1]=(unsigned char)rxmsg[29];
dhcp_ptr->chaddr[2]=(unsigned char)rxmsg[30];
dhcp_ptr->chaddr[3]=(unsigned char)rxmsg[31];
dhcp_ptr->chaddr[4]=(unsigned char)rxmsg[32];
dhcp_ptr->chaddr[5]=(unsigned char)rxmsg[33];
for(i=6;i<=15;i++){dhcp_ptr->chaddr[i]=0;}
strcpy(dhcp_ptr->sname,"Test_DHCP_SERVER\0"); /* 40: Server name */
strcpy(dhcp_ptr->file,"bc.txt\0"); /* 104: Boot filename */
dhcp_ptr->magic_cookie[0] = 99;
dhcp_ptr->magic_cookie[1] = 130;
dhcp_ptr->magic_cookie[2] = 83;
dhcp_ptr->magic_cookie[3] = 99;
dhcp_ptr->options[0]=53;
dhcp_ptr->options[1]=1 ;
dhcp_ptr->options[2]=2;
dhcp_ptr->options[3]=54;
dhcp_ptr->options[4]=4;
dhcp_ptr->options[5]= 0xc0;
dhcp_ptr->options[6]= 0xa8;
dhcp_ptr->options[7]= 0x38;
dhcp_ptr->options[8]= 0x65;
dhcp_ptr->options[9]=1;
dhcp_ptr->options[10]=4;
dhcp_ptr->options[11]= 0xff;
dhcp_ptr->options[12]= 0xff;
dhcp_ptr->options[13]= 0x00;
dhcp_ptr->options[14]= 0x00;
//Address Lease Time
dhcp_ptr->options[15]=51;
dhcp_ptr->options[16]=4;
dhcp_ptr->options[17]=0x00;
dhcp_ptr->options[18]=0x00;
dhcp_ptr->options[19]=0x0e;
dhcp_ptr->options[20]=0x10;
//END OPTION
dhcp_ptr->options[21]=0xff;
//PADDING
for(i=22;i<=298;i++) {
dhcp_ptr->options[i] = 0;
}
}
else {
printf("DHCP Request Packet was Recieved...\n");
dhcp_ptr->op = 2;
dhcp_ptr->htype = 1;
dhcp_ptr->hlen = 6;
dhcp_ptr->hops = 2;
dhcp_ptr->xid[0] = (unsigned char)rxmsg[4];
dhcp_ptr->xid[1] = (unsigned char)rxmsg[5];
dhcp_ptr->xid[2] = (unsigned char)rxmsg[6];
dhcp_ptr->xid[3] = (unsigned char)rxmsg[7];
dhcp_ptr->secs = 0;
dhcp_ptr->flags= 0;
inet_pton(AF_INET,"0.0.0.0",&(dhcp_ptr->ciaddr.s_addr));
inet_pton(AF_INET,"8.5.5.5",&(dhcp_ptr->yiaddr.s_addr));
inet_pton(AF_INET,"0.0.0.0",&(dhcp_ptr->siaddr.s_addr));
inet_pton(AF_INET,"0.0.0.0",&(dhcp_ptr->giaddr.s_addr));
dhcp_ptr->chaddr[0]=(unsigned char)rxmsg[28];
dhcp_ptr->chaddr[1]=(unsigned char)rxmsg[29];
dhcp_ptr->chaddr[2]=(unsigned char)rxmsg[30];
dhcp_ptr->chaddr[3]=(unsigned char)rxmsg[31];
dhcp_ptr->chaddr[4]=(unsigned char)rxmsg[32];
dhcp_ptr->chaddr[5]=(unsigned char)rxmsg[33];
for(i=6;i<=15;i++){dhcp_ptr->chaddr[i]=0;}
strcpy(dhcp_ptr->sname,"Test_DHCP_SERVER\0"); /* 40: Server name */
strcpy(dhcp_ptr->file,"bc.txt\0"); /* 104: Boot filename */
dhcp_ptr->magic_cookie[0] = 99;
dhcp_ptr->magic_cookie[1] = 130;
dhcp_ptr->magic_cookie[2] = 83;
dhcp_ptr->magic_cookie[3] = 99;
//DHCP Message Type
dhcp_ptr->options[0]=53;
dhcp_ptr->options[1]=1 ;
dhcp_ptr->options[2]=5;
//DHCP Server Identifier
dhcp_ptr->options[3]=54;
dhcp_ptr->options[4]=4;
dhcp_ptr->options[5]=0xc0;
dhcp_ptr->options[6]=0xa8;
dhcp_ptr->options[7]=0x38;
dhcp_ptr->options[8]=0x65;
//Address Lease Time
dhcp_ptr->options[9]=51;
dhcp_ptr->options[10]=4;
dhcp_ptr->options[11]=0x00;
dhcp_ptr->options[12]=0x00;
dhcp_ptr->options[13]=0x0e;
dhcp_ptr->options[14]=0x10;
// Subnet Mask
dhcp_ptr->options[15]=1;
dhcp_ptr->options[16]=4;
dhcp_ptr->options[17]=0xff;
dhcp_ptr->options[18]=0xff;
dhcp_ptr->options[19]=0x00;
dhcp_ptr->options[20]=0x00;
//END OPTION
dhcp_ptr->options[21]= 0xff;
//PADDING
for(i=22;i<=299;i++) {
dhcp_ptr->options[i] = 0;
}
}
iph_trans->check = htons(csum((unsigned short*)txmsg,sizeof(struct iphdr)+sizeof(struct udphdr)+sizeof(BOOTP)));
// SENDING THE STRUCTURE VIA SENDTO
if(bytes_send=sendto(sockfd,txmsg,1024,0,(struct sockaddr *)&client_addr, sizeof(client_addr)) < 0) {
printf("Send to ERROR %d\n",errno); }
else {
printf("Packet Sent \n"); }
}}
close(sockfd);
return 1;
}
最佳答案
由于功能请求到 mark a comment as an answer仍然被拒绝,我在这里复制上面的解决方案。
最终发现问题不在于代码,而在于默认网关。我的 PC 上没有配置默认网关,因此看到了 ERR-101。配置默认网关后,广播数据包就会出来。对不起。 – 凯萨瓦
关于sockets - 套接字发送到抛出 Errno-101,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11403348/
我正在维护一些 Java 代码,我目前正在将它们转换为 C#。 Java 代码是这样做的: sendString(somedata + '\000'); 在 C# 中,我正在尝试做同样的事情: sen
如何确定函数中传递的参数是字符串还是字符(不确定如何正确调用它)文字? 我的函数(不正确): void check(const char* str) { // some code here }
我真的不知道如何准确地提出这个问题,但我希望标题已经说明了这一点。 我正在寻找一种方法(一个框架/库),它提供了执行 String.contains() 函数的能力,该函数告诉我给定的字符串是否与搜索
我正在尝试编写一些读取 Lambda 表达式并输出 beta 缩减版本的东西。 Lambda 的类型如下:\variable -> expression,应用程序的形式为 (表达式) (表达式)。因此
StackOverflow 上的第 1 篇文章,如果我没能把它做好,我深表歉意。我陷入了一个愚蠢的练习,我需要制作一个“刽子手游戏”,我尝试从“.txt”文件中读取单词,然后我得到了我的加密函数,它将
我想在 Groovy 中测试我的 Java 自定义注释,但由于字符问题而未能成功。 Groovyc: Expected 'a' to be an inline constant of type cha
当我尝试在单击按钮期间运行 javascript location.href 时,出现以下错误“字 rune 字中的字符过多”。 最佳答案 这应该使用 OnClientClick相反? 您可能还想停
我想要类似的东西: let a = ["v".utf8[0], 1, 2] 我想到的最接近的是: let a = [0x76, 1, 2] 和 "v".data(using: String.Encod
有没有办法在 MySQL 中指定 Unicode 字 rune 字? 我想用 Ascii 字符替换 Unicode 字符,如下所示: Update MyTbl Set MyFld = Replace(
阅读 PNG 规范后,我有点惊讶。我读过字 rune 字应该用像 0x41 这样的二进制值进行硬编码,而不是在(程序员友好的)'A' 中。问题似乎是在具有不同底层字符集的不同系统上编译期间字 rune
考虑一个具有 UTF-8 执行字符集的 C++11 编译器(并且符合要求 char 类型为有符号 8 位字节的 x86-64 ABI) . 字母 Ä(元音变音)具有 0xC4 的 unicode 代码
为什么即使有 UTF-8 字符串文字,C11 或 C++11 中也没有 UTF-8 字 rune 字?我知道,一般来说,字 rune 字表示单个 ASCII 字符,它与单字节 UTF-8 代码点相同,
我怎样才能用 Jade 做到这一点? how would I do this 我几乎可以做任何事情,除了引入一个 span 中间句子。 最佳答案 h3.blur. how would I do t
这似乎是一个非常简单的问题,但我只是想澄清我的疑问。我正在查看其他开发人员编写的代码。有一些涉及 float 的计算。 示例:Float fNotAvlbl = new Float(-99); 他为什
我想知道第 3 行“if dec:”中的“dec”是什么意思 1 def dec2bin(dec): 2 result='' 3 if dec:
我试图在字符串中查找不包含任何“a”字符的单词。我写了下面的代码,但它不起作用。我怎么能对正则表达式说“不包括”?我不能用“^”符号表示“不是”吗? import re string2 = "asfd
这个问题在这里已经有了答案: Is floating point math broken? (31 个答案) Is floating point arbitrary precision availa
我正在创建一个时尚的文本应用程序,但在某些地方出现错误(“字 rune 字中的字符太多”)。我只写了一个字母,但是当我粘贴它时,它会转换成许多这样的字母:“\uD83C\uDD89”,原始字母是“🆉
我正在尝试检查用户是否在文本框中输入了一个数字值,是否接受了小数位。非常感谢任何帮助。 Private Sub textbox1_AfterUpdate() If IsNumeric(textbox1
我知道一个 Byte 是 8 位,但其他的代表什么?我正在参加一个使用摩托罗拉 68k 架构的汇编类(class),我对目前的词汇感到困惑。 最佳答案 如 operator's manual for
我是一名优秀的程序员,十分优秀!