2016-12-04 86 views
0

更新: 我剛剛在有問題的函數中初始化了結構硬編碼,我仍然有錯誤!UDP sendto()給出「無效的用戶空間地址」錯誤

void sendjoin(unsigned int ipaddress, unsigned short port, unsigned short preferredID){ 
    printf("Sending join-request...\n"); 
    char msg[9]; 
    memset(msg, 0, 9); 
    msg[0] = JOIN_MASK | INTERN_MASK; 
    ipaddress = ipaddress; 
    port = port; 
    preferredID = htons(preferredID); 
    memcpy(&msg[1], &ipaddress, 4); 
    memcpy(&msg[5], &port, 2); 
    memcpy(&msg[7], &preferredID, 2); 

    errno = 0; 

    //printMSG((void *)&succAddr, sizeof(succAddr), 0); 
    socklen_t addrlen = sizeof(struct sockaddr_in); 
    struct sockaddr_in *temp = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in)); 
    temp->sin_family = AF_INET; 
    temp->sin_port = htons((short)8888); 
    temp->sin_addr.s_addr = htonl(16777343); 

    if(0 > sendto(sock, msg, 9, 0, (struct sockaddr *)temp, addrlen)){ 
     perror("Error while sending a packet.\n"); 
     switch(errno){ 
      case EFAULT: 
       printf("Invalid user space address.\n"); 
       break; 
      case EAGAIN: 
       printf("Something with blocking.\n"); 
       break; 
      case EBADF: 
       printf("Invalid descriptor.\n"); 
       break; 
      case EINVAL: 
       printf("Invalid argument.\n"); 
       break; 
      case EDESTADDRREQ: 
       printf("No Peer address.\n"); 
       break; 
      case EISCONN: 
       printf("Connection mode socket.\n"); 
       break; 
      case ENOTSOCK: 
       printf("The given socket is not a socket.\n"); 
       break; 
      case ENOTCONN: 
       printf("No Target.\n"); 
       break; 
      case ENOBUFS: 
       printf("Network output is full.\n"); 
       break; 
      default: 
       printf("No spezific error.\n"); 
     } 
     exit(1); 
    } 
    printf("Send to: %u %u\n", ntohl(succAddr.sin_addr.s_addr), ntohs(succAddr.sin_port)); 
} 

我正在寫這個分佈式散列表服務器。這是一個超級簡單的版本。我只是剛剛起步,得到一個只是在聽包裝的服務器。另一個試圖加入第一個的dht網絡。所以我正在用一個端口號,家族AF_INET和一個IP地址填充全局struct sockaddr_in。

我使用函數inet_ntop()將我的「localhost」 - 字符串轉換爲我的結構。 後來我使用sendto()開始與dht網絡進行通信。但它只是給出了「無效的用戶空間地址」錯誤。 sendto()獲取一個開放的套接字(自己的open serversocket!),一個初始化的緩衝區,他的長度,sockaddr_in結構體的地址和長度。

這是相當一些代碼。我刪除了一些未使用的函數,並試圖僅包含使用變量的函數。 在main()中可以看到三個函數。一個是checkarguments(),這並不是很有趣。 然後你有setsocket()函數,它讓我一個服務器套接字使用。在這個函數的最後,我填寫了這個的前任和後繼服務器的ip地址,端口和id。 然後你有試圖加入DHT網絡的letsgo()函數。它使用已知的dht節點的地址填充全局struct sockaddr_in succAddr,因爲對sendjoin()函數的調用總是發送給succAddr。 並在sendjoin()它試圖發送到succAddr但失敗。我不知道爲什麼。我的succAddr被初始化,並且不應該指向一個無效的用戶空間?!

請幫助:)

這裏是我的代碼:

//================================= 
//=Variablen======================= 
//================================= 

unsigned short myID = 0; 
char *myIP= NULL; 
char *myPORT = NULL; 
char *nodeIP = NULL; 
char *nodePORT = NULL; 

int sock = 0; 
struct sockaddr_in preAddr; 
unsigned short preID = 0; 
int preknown = 1; 
struct sockaddr_in succAddr; 
unsigned short succID = 0; 

char packet[PACKETSIZE]; 
struct sockaddr_storage sender; 
socklen_t sendersize = sizeof sender; 
int tablefunctionstarted = 0; 

char INTERN_MASK = 0b10000000; 
char STABILIZE_MASK = 0b00000100; 
char NOTIFY_MASK = 0b00000010; 
char JOIN_MASK = 0b00000001; 
char SNJ_MASK = 0b00000111; 

//================================= 
//=Functions======================= 
//================================= 


void setSocket(); 
int checkarguments(int argc, char **argv); 
int letsgo(); 
void printMSG(char *msg, int msglength, int plain); 
void handleintern(unsigned long msglength); 
void handleoutside(unsigned long msglength); 
void sendjoin(unsigned int ipaddress,unsigned short port, unsigned short preferredID); 
void sendnotify(unsigned int ipaddress,unsigned short port, unsigned short preferredID); 
void sendstabilize(); 

int main(int argc, char **argv){ 
    printf("Starting server...\n"); 
    int einklinken = checkarguments(argc, argv); 
    setSocket(); 
    if(einklinken) if(letsgo()){ perror("Problem while joining DHT network!\n"); exit(2);}; 

    /* 
    //Hauptpacketannahmeverarbeitungsschleife 
    long check = 0; 
    while(1){ 
     printf("Server is waiting for packets... \n"); 
     if(-1 == (check = recvfrom(sock, packet, PACKETSIZE, 0, (struct sockaddr *)&sender, &sendersize))){ 
      perror("Error with new packet!\n"); 
      exit(2); 
     } 
     printf("Found packet... \n"); 
     //printMSG(packet, check, 1); 

     if(0 != (packet[0] & INTERN_MASK)) handleintern(check); 
     else handleoutside(check); 
    } 
    */ 
} 


int checkarguments(int argc, char **argv){ 
    if(argc < 3){ fprintf(stderr,"Too few arguments! \nUsage: %s myip myport\nOptions:\n-id preferredID (Your preferred id in the DHT network.)\n-new ip port (IP address and port of a DHT network node.)", argv[0]); exit(1);} 
    if(argc > 8){ fprintf(stderr,"Too many arguments! \nUsage: %s myip myport\nOptions:\n-id preferredID (Your preferred id in the DHT network.)\n-new ip port (IP address and port of a DHT network node.)", argv[0]); exit(1);} 

    /* printf("Arguments:\n"); 
    for(int i = 0; i < argc; ++i){ 
     printf("%s\n", argv[i]); 
    } */ 
    printf("Parsing arguments...\n"); 
    myIP = argv[1]; 
    myPORT = argv[2]; 
    printf("My ip: %s\n", myIP); 
    printf("My port: %s\n", myPORT); 

    int newNode = 0; 
    int count = 3; 
    while(argc > count){ 
     if(argv[count][0]!='-'){fprintf(stderr,"Wrong usage! \nUsage: %s myip myport\nOptions:\n-id preferredID (Your preferred id in the DHT network.)\n-new ip port (IP address and port of a DHT network node.)", argv[0]); exit(1);} 
     if(argv[count][1]=='i'){ 
      ++count; 
      errno = 0; 
      long value = 0; 
      value = strtol(argv[count++], NULL, 10); 
      switch(errno){ 
       case ERANGE: 
        printf("The ID could not be represented.\n"); 
        exit(1); 
       case EINVAL: 
        printf("Unsupported base/radix.\n"); 
        exit(1); 
      } 
      if(value > 65536){ perror("Your desired ID is over the max value of 65536!\n"); exit(1);} 
      myID = (short) value; 
      printf("My id: %u\n", myID); 
     } 
     else if(argv[count][1]=='n'){ 
      ++count; 
      nodeIP = argv[count++]; 
      nodePORT = argv[count++]; 
      printf("The nodes ip: %s\n", nodeIP); 
      printf("The nodes port: %s\n", nodePORT); 
      newNode = 1; 
     } 
    } 

    printf("Finished parsing arguments.\n"); 
    return newNode; 
} 


void setSocket(){ 

    printf("Configuring the socket...\n"); 
    struct addrinfo hints, *locations, *p; 
    int yes=1; 

    memset(&hints, 0, sizeof hints); 
    hints.ai_family = AF_UNSPEC; 
    hints.ai_socktype = SOCK_DGRAM; 
    hints.ai_flags = AI_PASSIVE; 

    int rv = getaddrinfo(NULL, myPORT, &hints, &locations); 

    if(rv != 0){ 
     fprintf(stderr, "getaddrinfo threw an error! Maybe your port is used?\n"); 
     exit(1); 
    } 

    for(p = locations; p!=NULL; p=p->ai_next){ 

     sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol); 
     if(sock == -1){perror("SocketError");continue;} 

     if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){perror("Can't clean socket!\n"); exit(1);} 

     if(bind(sock, p->ai_addr, p->ai_addrlen) == -1){perror("BindError");close(sock);continue;} 

     break; 
    } 

    if(p==NULL){ 
     perror("Failed to bind!\n"); 
     exit(2); 
    } 

    int check = 0; 
    if(-1 == (check = inet_pton(AF_INET, myIP, &(preAddr.sin_addr)))){ perror("Wrong ip format!\n"); exit(1);} 
    if(-1 == (check = inet_pton(AF_INET, myIP, &(succAddr.sin_addr)))){ perror("Wrong ip format!\n"); exit(1);} 
    memcpy(&(succAddr.sin_port), &(((struct sockaddr_in *)p->ai_addr)->sin_port), 2); 
    memcpy(&(preAddr.sin_port), &(((struct sockaddr_in *)p->ai_addr)->sin_port), 2); 
    memset(preAddr.sin_zero, 0, 8); 
    memset(succAddr.sin_zero, 0, 8); 
    preID = myID; 
    succID = myID; 

    printf("Finished configuring the socket.\n"); 

    freeaddrinfo(locations); 
} 


int letsgo(){ 
    printf("Trying to join an existent DHT network...\n"); 
    preknown = 0; 


    int check = 0; 
    succAddr.sin_family = AF_INET; 
    if(-1 == (check = inet_pton(AF_INET, nodeIP, &(succAddr.sin_addr)))){ perror("Wrong ip format!\n"); exit(1);} 
    errno = 0; 
    long value = 0; 
    value = strtol(nodePORT, NULL, 10); 
    switch(errno){ 
     case ERANGE: 
      printf("The node port could not be represented.\n"); 
      exit(1); 
     case EINVAL: 
      printf("Unsupported base/radix.\n"); 
      exit(1); 
    } 
    if(value > 65536){ perror("Your node port is over the max value of 65536!\n"); exit(1);} 
    succAddr.sin_port = htons((short) value); 

    unsigned int ipaddress = preAddr.sin_addr.s_addr; 
    unsigned short port = preAddr.sin_port; 
    unsigned short preferredID = 0; 

    sendjoin(ipaddress, port, myID); 

    while(1){ 
     printf("Wait for notify answer... \n"); 
     if(-1 == (check = recvfrom(sock, packet, PACKETSIZE, 0, (struct sockaddr *)&sender, &sendersize))){ 
      perror("Error with new packet!\n"); 
      exit(2); 
     } 
     printf("Got answer... \n"); 

     if(0 == (packet[0] & INTERN_MASK)){ printf("Can't understand the packet.\n"); continue;} 
     if(0 == (packet[0] & NOTIFY_MASK || check < 8)){ printf("Not a notify packet.\n"); continue;} 

     memcpy(&preferredID, &packet[7], 2); 
     preferredID = ntohs(preferredID); 
     if(preferredID > TABLE_LENGTH){ perror("Preferred id is too big!\n"); continue;} 
     memcpy(&ipaddress, &packet[1], 4); 
     memcpy(&port, &packet[5], 2); 

     succAddr.sin_addr.s_addr = ipaddress; 
     succAddr.sin_port = port; 
     succID = preferredID; 
     break; 
    } 


    return 0; 
} 


void sendjoin(unsigned int ipaddress, unsigned short port, unsigned short preferredID){ 
    printf("Sending join-request...\n"); 
    char msg[9]; 
    memset(&msg, 0, 9); 
    msg[0] = JOIN_MASK | INTERN_MASK; 
    ipaddress = ipaddress; 
    port = port; 
    preferredID = htons(preferredID); 
    memcpy(&msg[1], &ipaddress, 4); 
    memcpy(&msg[5], &port, 2); 
    memcpy(&msg[7], &preferredID, 2); 

    errno = 0; 

    //printMSG((void *)&succAddr, sizeof(succAddr), 0); 
    socklen_t addrlen = sizeof(struct sockaddr_in); 

    if(0 > sendto(sock, msg, 9, 0, (struct sockaddr *)&succAddr, addrlen)){ 
     perror("Error while sending a packet.\n"); 
     switch(errno){ 
      case EFAULT: 
       printf("Invalid user space address.\n"); 
       break; 
      case EAGAIN: 
       printf("Something with blocking.\n"); 
       break; 
      case EBADF: 
       printf("Invalid descriptor.\n"); 
       break; 
      case EINVAL: 
       printf("Invalid argument.\n"); 
       break; 
      case EDESTADDRREQ: 
       printf("No Peer address.\n"); 
       break; 
      case EISCONN: 
       printf("Connection mode socket.\n"); 
       break; 
      case ENOTSOCK: 
       printf("The given socket is not a socket.\n"); 
       break; 
      case ENOTCONN: 
       printf("No Target.\n"); 
       break; 
      case ENOBUFS: 
       printf("Network output is full.\n"); 
       break; 
      default: 
       printf("No spezific error.\n"); 
     } 
     exit(1); 
    } 
    printf("Send to: %u %u\n", ntohl(succAddr.sin_addr.s_addr), ntohs(succAddr.sin_port)); 
} 
+0

'PERROR()'已經打印錯誤的正確文本。根本不需要那個巨大的開關語句,特別是當它打印*錯誤的*文本時。不要'翻譯'系統錯誤代碼或消息。否則你冒着給出錯誤信息的風險,只有你可以調試代碼。 – EJP

+0

您是否嘗試過使用'strace'來確認sendto()'的參數是否正確? –

+0

@EJP我只從'perror()'得到了「壞地址」作爲錯誤描述,我在因特網上找到的errno手冊頁用「無效的用戶空間地址」指定它。但是,是的,我將在未來停止這樣做:)。 我仍然不知道錯誤是什麼。但是今天,我在ubuntu 14上測試了我的程序,並沒有給出任何錯誤。這與我的cygwin在Windows上有關。 – Hyrikan

回答

0

這條線:

socklen_t addrlen = sizeof(struct sockaddr); 

需要是:

socklen_t addrlen = sizeof(struct sockaddr_in); 
+0

對不起。這條線是幾個小時後我發現我犯了一個小錯誤。但它不會改變結果:( – Hyrikan

+0

是的,我只是注意到''sizeof(sockaddr_in)== sizeof(sockaddr)== 16'在Linux上,所以不是這樣。 – selbie

相關問題