c - 在 C 中发送手工制作的 UDP 数据报?

标签 c ip

我的教授指示我们手动制作 UDP 请求并发送。 到目前为止,我已经构建了 UDP header 和内容指针。问题是 sendto 函数需要数据结构 sockaddr 而我所拥有的只是指示目标 ipv4 的字符串。 .

那么知道如何将这个字符串转换成那个结构,或者是否有不同的发送消息?

#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

static void die (const char * format, ...)
{
    va_list vargs;
    va_start (vargs, format);
    vfprintf (stderr, format, vargs);
    fprintf (stderr, ".\n");
    exit (1);
}
void copy_lower_16_int(void * dest,int value)
{
    size_t sz  =sizeof(value);
    if(sz == 2)
        memcpy(dest,&value,2);
    else if (sz >2)
    {
        memcpy(dest,(&value)+sz-2,2);
    }
}
int sendTo(void * message,size_t size ,char * destinationIP,unsigned short  destinationPort)
{


    // Get the target transport protocol number
    const char* protocol_name="udp";
    struct protoent* protocol=getprotobyname(protocol_name);
    if (!protocol) {
        die("Protocol %s not found",protocol_name);
    }
    int protocol_number=protocol->p_proto;

    printf(" Protocol %s has number of %d \n",protocol_name,protocol_number);

    // Create raw socket for usage with IPv4 & the specified transport protocol (UDP)
    int fd=socket(AF_INET,SOCK_RAW,protocol_number);
    if (fd==-1) {
        die("Failed to create a raw socket, error : %s",strerror(errno));
    }

    // Construct the UDP datagram

    //Calculate total size ( message size + 8 byte for the headers)
    size_t total_size = size + 8;
    // Calculate the checksum 
    int checksum = 0x53AF;  

    void * content = (void*) malloc(total_size);
    const int sourcePort = 1524;


    copy_lower_16_int(content,sourcePort);
    copy_lower_16_int(content+2,destinationPort);
    copy_lower_16_int(content+4,total_size);

    copy_lower_16_int(content+6,checksum);


    memcpy(content+8,message,size);

}
void main(){
    printf("Size of int is %d",sizeof(2));
    sendTo("Cx",2,"127.0.0.1",5405);
}

最佳答案

我通过使用 getaddrinfo 解决了我的问题,如下所示

struct addrinfo *addr_result;
struct addrinfo hints;


memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;    /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_RAW; /* Datagram socket */
hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
hints.ai_protocol = protocol_number;          /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
getaddrinfo(destinationIP,NULL,&hints,&addr_result);

//Send the damn message
ssize_t sentsz = sendto(fd,content,total_size,0,addr_result->ai_addr,addr_result->ai_addrlen);
if (sentsz == -1)
{
        printf("It failed to send !!");
}
else {
        printf("It did send %d bytes",sentsz);
}
// free allocated memory

free(content);
freeaddrinfo(addr_result);

关于c - 在 C 中发送手工制作的 UDP 数据报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40433351/

相关文章:

java - 如何在 Java 中以字符串形式从 IP 获取 byte[] 表示

使用 strtol() 将 IP 地址从十六进制转换为十进制

c - 如何将指针分配给此数组(OpenCL)中某些结构的数组元素?

c - 以下哪项不等同于其他项?

c - C 中未知的数学级数

将 <netinet/in.h> 协议(protocol)转换为名称?

java 。将字符串转换为点分 IP 地址

c - 如何在 C 中正确地重新分配一个位数组

进程或库的多个副本之间的 C 字符串文字存储

java 如何指定程序应连接的正确IP地址