C 64位上的代码SEGFULTS在32位上工作
下面的代码适用于32位debian,但会导致64位的segfault。添加代码片段C 64位上的代码SEGFULTS在32位上工作,c,networking,segmentation-fault,C,Networking,Segmentation Fault,下面的代码适用于32位debian,但会导致64位的segfault。添加代码片段 #include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/wait.h> #include <netinet/in.h> #include <errno.h> #include <netdb.h> #include <sig
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <errno.h>
#include <netdb.h>
#include <signal.h>
void brk(int no){
printf("^C Interrupt!\n");
exit(1);
}
void main(int argc, char **argv)
{
struct hostent *host; /* init stuff */
struct sockaddr_in sa;
int net, error;
int port=23, i, done=0;
char *curr_ip, *del, *cm[100];
int A1, A2, A3, A4;
int B1, B2, B3, B4;
int C1, C2, C3, C4;
printf("\nDomain Scanner v2.0 by HoGs HeaD\nHit any key to end.\n");
if(argc < 3){
printf("Usage: domscan ip_begin ip_end port\n");
exit(0);
}
signal(SIGINT, brk);
if(argv[3]==NULL){
}else{
port=atoi(argv[3]);
}
/* Parse in the first Ip.... */
curr_ip=argv[1];
del=(char *)strtok(curr_ip, ".");
A1=atoi(del);
del=(char *)strtok(NULL, ".");
A2=atoi(del);
del=(char *)strtok(NULL, ".");
A3=atoi(del);
del=(char *)strtok(NULL, ".");
A4=atoi(del);
/* Read in Second Ip... */
curr_ip = argv[2];
del=(char *)strtok(curr_ip, ".");
B1=atoi(del);
del=(char *)strtok(NULL, ".");
B2=atoi(del);
del=(char *)strtok(NULL, ".");
B3=atoi(del);
del=(char *)strtok(NULL, ".");
B4=atoi(del);
/* We're finished parsing, now onto the actual scan... */
C1=A1;
C2=A2; /* SaVe DeM VaLueS! */
C3=A3;
C4=A4;
for(A4=C4;A4<=B4; A4++){
for(A3=C3;A3<=B3; A3++){
for(A2=C2;A2<=C2; A2++){
for(A1=C1;A1<=B1; A1++){
sprintf(curr_ip, "%d.%d.%d.%d", A1, A2, A3, A4); /* build the ip */
if( ( fork() ) == 0){ /* fork a child */
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(curr_ip);
sa.sin_port = htons(port); /* socket is set and... */
net = socket(AF_INET, SOCK_STREAM, 0); /* create socket */
if(net < 2){
exit(2);
}
alarm(5); /* wait 5 sec onds until we cancel connection */
error = connect(net, (struct sockaddr *)&sa, sizeof sa); /* attempt connection */
error < 0 ? printf("Error connecting to: %s %s\n", curr_ip, strerror(errno)) : printf("Connection success at: %s\n", curr_ip);
shutdown(net, 2); /* disconnect socket */
exit(0); /* exit child process */
}
}
}
}
}
gets((char *)i); /* Wait for enter to be pressed to exit */
}
您需要包括包含
exit
、atoi
、strtok
和fork
的标题:
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#包括
#包括
#包括
从您发布的警告和错误消息中可以明显看出大多数
如有疑问,请务必查看文档,了解特定函数需要哪个标题。如果您不确定如何在系统中查找文档,可以通过谷歌man
查找文档
您还应该从strtok中删除返回值的强制转换,因为它掩盖了源于缺少声明的警告。Per:
简介
#include <string.h>
char *strtok(char *restrict s, const char *restrict sep);
#包括
char*strtok(char*restrict s,const char*restrict sep);
请注意
#include <string.h>
#包括
您的代码不包括该标头
这意味着假定strtok()
返回int
,从而将char*
从64位截断为32位,然后将其作为不正确的指针值分配给char*
,从而导致SIGSEGV
注意编译器警告。那么警告告诉您什么,为什么您希望程序能够正常工作,尽管您收到了这些警告?如果很多路标警告您在死路,您是否继续在那条路上……?
printf(^C Interrupt!\n”)代码>不是异步信号安全的。您不应该在信号处理程序中调用printf()
。OT:不是函数的好名称,也不要使用gets()
您看到的那些警告实际上更像是错误。顺便说一句,您应该正确缩进代码。#include
还声明程序使用的atoi
。它还应该有#include
来声明fork
@IanAbbott谢谢!我已经把它们添加到了答案中。从警告列表中看,这些并不明显,这就是为什么我没有把它们捡起来。
#include <string.h>