gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:26:55 24 4
gpt4 key购买 nike

我的教授指示我们手动制作 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com