Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

windows - Receive data using Python sockets UDP IPV6 for a specific source address and port

im trying to implement a server client app with python using the sockets module (running on windows), this is the simplest way that ive found over the internet:

for server:

import socket

UDP_IP = ''
UDP_PORT = 42557

sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
a = 0
while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print ("received message:", data)
    print("addr is:", addr)
    sock.close()
    break

for client :

import socket

UDP_IP = "::1"
UDP_PORT = 42557
MESSAGE = b"0000000EC4030004004000000000000000000000000000000000000000000000"

print ("UDP target IP:", UDP_IP)
print ("UDP target port:", UDP_PORT)
print ("message:", MESSAGE)

sock = socket.socket(socket.AF_INET6, # Internet
                    socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

sock.close()

ok this is working fine.

The thing is, i have a pcap file with several packages, if i reproduce that file i can see all the packages passing through the interface (ethernet card) using wire shark .

the question is, how do i get those packages using the socket module? ive tried like a bunch of ways and i cant see a thing (im a noob with sockets).

in the pcap file basically the info is the following:

Source Address: FD53:7CB8:0383:0002:0000:0000:0.0.0.105 Destination Address: FF14:0000:0000:0000:0000:0000:0.0.0.28 Source port: 42994 Destination port: 42512

with some random data in the payload. i.e: 0000000EC4030004004000000000000000000000000000000000000000000000

thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I used https://github.com/rigtorp/udpreplay and it worked. I used the loop-back interface so the IP address was not an issue and the send port and the receive port must be different.

My sequence was.

  1. send a short text message from a client to the server and record it with Wireshark.
  2. record with Wireshark the message I sent.
  3. replay the recorded with udpreplay message and check if the server receives it.

I would first suggest:

  1. Try to replay your message with a different tool. Initially, I tried with tcpreplay and it did not work.
  2. Try to replay one message on the same PC with the loopback interface to be sure that your tool is working before you move to more complex pcap recorded on a different PC.

to try to reply to this simple message.

./udpreplay -i lo -l hello_from_client.pcapng

Server/Client example I found online

Client:

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 
  
#define DST_PORT    8080 
#define SRC_PORT    8081
#define MAXLINE 1024 

#define IP      "127.0.0.1"
  
// Driver code 
int main() { 
    struct sockaddr_in addr, srcaddr;
    int sockfd;
    char message[] = "Hello, World!";
    char *hello = "Hello from clinet"; 

    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket");
        exit(1);
    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(IP);
    addr.sin_port = htons(DST_PORT);

    memset(&srcaddr, 0, sizeof(srcaddr));
    srcaddr.sin_family = AF_INET;
    srcaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    srcaddr.sin_port = htons(SRC_PORT);

    if (bind(sockfd, (struct sockaddr *) &srcaddr, sizeof(srcaddr)) < 0) {
        perror("bind");
        exit(1);
    }

    sendto(sockfd, (const char *)hello, strlen(hello), 
        MSG_CONFIRM, (const struct sockaddr *) &addr,  
            sizeof(addr)); 
    printf("Hello message sent.
"); 

    int n, len;
    char buffer[256];
          
    n = recvfrom(sockfd, (char *)buffer, MAXLINE,  
                MSG_WAITALL, (struct sockaddr *) &addr, 
                &len); 
    buffer[n] = ''; 
    printf("Server : %s
", buffer); 
  
    close(sockfd); 
    return 0; 
}

Server:

// Server side implementation of UDP client-server model 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 
  
#define PORT     8080 
#define MAXLINE 1024 
  
// Driver code 
int main() { 
    int sockfd; 
    char buffer[MAXLINE]; 
    char *hello = "Hello from server"; 
    struct sockaddr_in servaddr, cliaddr; 
      
    // Creating socket file descriptor 
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { 
        perror("socket creation failed"); 
        exit(EXIT_FAILURE); 
    } 
      
    memset(&servaddr, 0, sizeof(servaddr)); 
    memset(&cliaddr, 0, sizeof(cliaddr)); 
      
    // Filling server information 
    servaddr.sin_family    = AF_INET; // IPv4 
    servaddr.sin_addr.s_addr = INADDR_ANY; 
    servaddr.sin_port = htons(PORT); 
      
    // Bind the socket with the server address 
    if ( bind(sockfd, (const struct sockaddr *)&servaddr,  
            sizeof(servaddr)) < 0 ) 
    { 
        perror("bind failed"); 
        exit(EXIT_FAILURE); 
    } 
      
    int len, n; 
  
    len = sizeof(cliaddr);  //len is value/resuslt 
  
    n = recvfrom(sockfd, (char *)buffer, MAXLINE,  
                MSG_WAITALL, ( struct sockaddr *) &cliaddr, 
                &len); 
    buffer[n] = ''; 
    printf("Client : %s
", buffer); 
    sendto(sockfd, (const char *)hello, strlen(hello),  
        MSG_CONFIRM, (const struct sockaddr *) &cliaddr, 
            len); 
    printf("Hello message sent.
");  
      
    return 0; 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...