64位代码段错误在32位上运行

时间:2016-11-21 11:21:13

标签: c networking segmentation-fault

以下代码适用于32位debian,但会导致64位的段错误。添加代码段。

#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 */
}

这是一个简单的端口扫描程序,用于扫描单个端口的ip范围。

编译后我也收到以下警告:

omscan.c: In function ‘brk’:
domscan.c:22:4: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
domscan.c: In function ‘main’:
domscan.c:38:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
domscan.c:50:8: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
domscan.c:52:8: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
domscan.c:54:8: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
domscan.c:56:8: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
domscan.c:61:8: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
domscan.c:63:8: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
domscan.c:65:8: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
domscan.c:67:8: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
domscan.c:86:7: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
domscan.c:93:4: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
domscan.c:100:8: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

2 个答案:

答案 0 :(得分:1)

您需要包含包含exitatoistrtokfork的标头:

#include <string.h>
#include <stdlib.h>
#include <unistd.h>

大多数情况都来自您发布的警告和错误消息。

如有疑问,请务必查看文档以了解特定功能所需的标题。如果您不确定如何在系统中查找文档,可以通过Google搜索man <function>找到文档。

您还应该从strtok中移除返回值的强制转换,因为它会掩盖来自缺失声明的警告。

答案 1 :(得分:0)

the strtok() standard

  

<强>概要

#include <string.h>

char *strtok(char *restrict s, const char *restrict sep);

请注意

#include <string.h>

您的代码不包含该标题。

这意味着假定strtok()返回int,从而将char *从64位截断为32位,然后将其分配给char * - 作为一个不正确的指针值,从而导致SIGSEGV

注意编译器警告。