从c代码打印ip地址和udp端口

从c代码打印ip地址和udp端口,c,string,sockets,pointers,C,String,Sockets,Pointers,我使用的是原始udp套接字。我有一个IP地址和一个udp端口,通过终端插入在下面的方式分配。我想以经典的字符串格式打印它们(例如192.168.16.2) 我尝试了printf(“packetsend.iph\u sourceip:%d\n”,ip->iph\u sourceip)但我只得到随机数 我还尝试了printf(“packetsend.iph_sourceip:%c\n”,inet_ntoa(ip->iph_sourceip))这里我看到一个奇怪的标志。它们不起作用 这是完整的代码:(

我使用的是原始udp套接字。我有一个IP地址和一个udp端口,通过终端插入在下面的方式分配。我想以经典的字符串格式打印它们(例如192.168.16.2)

我尝试了
printf(“packetsend.iph\u sourceip:%d\n”,ip->iph\u sourceip)但我只得到随机数

我还尝试了printf(“packetsend.iph_sourceip:%c\n”,inet_ntoa(ip->iph_sourceip))这里我看到一个奇怪的标志。它们不起作用

这是完整的代码:(我对第138行的printf感兴趣)

/----rawudp.c------
//必须由根lol运行!只有数据报,没有有效载荷/数据
#包括
#包括
#包括
#包括
#包括
//数据包长度
#定义PCKT_LEN 8192
//可以为所有头的结构创建单独的头文件(.h)
//IP头的结构
结构ipheader{
未签名字符iph\u ihl:5,iph\u版本:4;
未签名字符iph_tos;
无符号短整数iph_len;
无符号短整数识别码;
无符号字符iph_标志;
无符号短整数iph_偏移量;
无符号字符iph_ttl;
无符号字符iph_协议;
无符号短整数iph_chksum;
未签名的int-iph\u-sourceip;
未签名的int iph_destip;
};
//UDP报头的结构
结构udpheader{
无符号短int-udph\u srcport;
未签名的短int-udph\u destport;
无符号短int-udph_len;
无符号短int-udph_-chksum;
};
//udp报头总长度:8字节(=64位)
//用于校验和计算的函数。来自RFC,
//校验和算法是:
//“校验和字段是1的16位1的补码
//报头中所有16位字的补码和。用于
//计算校验和时,校验和字段的值为零。”
无符号短csum(无符号短*buf,int-nwords){//
无符号长和;
对于(总和=0;nwords>0;nwords--)
总和+=*buf++;
总和=(总和>>16)+(总和&0xffff);
总和+=(总和>>16);
返回值(无符号短)(~sum);
}
//来自命令行参数的源IP、源端口、目标IP、目标端口
int main(int argc,char*argv[]){
国际标准差;
//没有数据/有效载荷,只有数据报
字符缓冲区[PCKT_LEN];
//我们自己的头结构
结构ipheader*ip=(结构ipheader*)缓冲区;
结构udpheader*udp=(结构udpheader*)(缓冲区
+sizeof(struct ipheader));
//源和目标地址:IP和端口
sin中的结构sockaddr_,din;
int-one=1;
常量int*val=&one;
memset(缓冲区,0,PCKT_LEN);
如果(argc!=5){
printf(“-无效参数!!!\n”);
printf(
“-用法%s\n”,
argv[0]);
出口(-1);
}
//使用UDP协议创建原始套接字
sd=套接字(PF_INET、SOCK_RAW、IPPROTO_UDP);
如果(sd<0){
perror(“套接字()错误”);
//如果有什么不对劲就退出
出口(-1);
}否则
printf(“socket()-使用SOCK_原始套接字和UDP协议是可以的。\n”);
//源是冗余的,如果需要,可以稍后使用
//地址家庭
sin.sin_family=AF_INET;
din.sin_系列=AF_系列;
//端口号
sin.sin_port=htons(atoi(argv[2]);
din.sin_端口=htons(atoi(argv[4]);
//IP地址
sin.sin_addr.s_addr=inet_addr(argv[1]);
din.sin_addr.s_addr=inet_addr(argv[3]);
//制作IP头或者我们可以使用
//标准的头结构,但分配我们自己的值。
ip->iph_ihl=5;
ip->iph\u ver=4;
ip->iph_tos=16;//低延迟
ip->iph_len=sizeof(struct ipheader)+sizeof(struct udpheader);
ip->iph_ident=htons(54321);
ip->iph\u ttl=64;//跳数
ip->iph_协议=17;//UDP
//源IP地址,可以在这里使用伪造地址!!!
ip->iph_sourceip=inet_addr(argv[1]);
//目标IP地址
ip->iph_destip=inet_addr(argv[3]);
//制作UDP标头。源端口号,冗余
udp->udph_srcport=htons(atoi(argv[2]);
//目的地端口号
udp->udph_destport=htons(atoi(argv[4]);
udp->udph_len=htons(sizeof(struct udpheader));
//计算完整性校验和
ip->iph_chksum=csum((无符号短*)缓冲区,
sizeof(struct ipheader)+sizeof(struct udpheader));
//通知内核不要填充数据包结构。我们将建立自己的。。。
如果(设置锁定选项(sd、IPPROTO_IP、IP_HDRINCL、val、sizeof(一个))小于0){
perror(“setsockopt()错误”);
出口(-1);
}否则
printf(“setsockopt()正常。\n”);
//发送循环,每2秒发送100次
printf(“正在尝试…\n”);
printf(“使用原始套接字和UDP协议”);
printf(“使用源IP:%s端口:%u,目标IP:%s端口:%u。\n”,argv[1],
atoi(argv[2])、argv[3]、atoi(argv[4]);
整数计数;
对于(count=1;count iph_len,0,(struct sockaddr*)和sin,
sizeof(sin))<0)
//核实
{
perror(“sendto()错误”);
出口(-1);
}否则{
printf(“计数#%u-sendto()正常。\n”,计数);
printf(“Packet Send.iph_sourceip:%s\n”,inet_ntoa(ip->iph_sourceip));
睡眠(2);
}
}
关闭(sd);
返回0;
}

您需要使用函数将网络顺序二进制数据转换为点十进制表示法,然后必须使用
%s
格式说明符打印结果。

IP地址是字符串,您需要使用%s打印。

我收到一个分段错误(内核转储)错误。我是这样说的:printf(“Packet Send.iph_sourceip:%s\n”,inet_ntoa(ip->iph_sourceip))@Pheonix7现在这是一个单独的问题。你能创建一个新的版本吗?
ip->iph_sourceip = inet_addr(argv[1]);
udp->udph_srcport = htons(atoi(argv[2]));
// ----rawudp.c------
// Must be run by root lol! Just datagram, no payload/data
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

// The packet length
#define PCKT_LEN 8192

// Can create separate header file (.h) for all headers' structure
// The IP header's structure
struct ipheader {
    unsigned char iph_ihl :5, iph_ver :4;
    unsigned char iph_tos;
    unsigned short int iph_len;
    unsigned short int iph_ident;
    unsigned char iph_flag;
    unsigned short int iph_offset;
    unsigned char iph_ttl;
    unsigned char iph_protocol;
    unsigned short int iph_chksum;
    unsigned int iph_sourceip;
    unsigned int iph_destip;
};

// UDP header's structure
struct udpheader {
    unsigned short int udph_srcport;
    unsigned short int udph_destport;
    unsigned short int udph_len;
    unsigned short int udph_chksum;
};
// total udp header length: 8 bytes (=64 bits)
// Function for checksum calculation. From the RFC,
// the checksum algorithm is:
//  "The checksum field is the 16 bit one's complement of the one's
//  complement sum of all 16 bit words in the header.  For purposes of
//  computing the checksum, the value of the checksum field is zero."
unsigned short csum(unsigned short *buf, int nwords) { //
    unsigned long sum;
    for (sum = 0; nwords > 0; nwords--)
        sum += *buf++;
    sum = (sum >> 16) + (sum & 0xffff);
    sum += (sum >> 16);
    return (unsigned short) (~sum);
}
// Source IP, source port, target IP, target port from the command line arguments
int main(int argc, char *argv[]) {
    int sd;
// No data/payload just datagram
    char buffer[PCKT_LEN];
// Our own headers' structures
    struct ipheader *ip = (struct ipheader *) buffer;
    struct udpheader *udp = (struct udpheader *) (buffer
            + sizeof(struct ipheader));
// Source and destination addresses: IP and port
    struct sockaddr_in sin, din;
    int one = 1;
    const int *val = &one;

    memset(buffer, 0, PCKT_LEN);

    if (argc != 5) {
        printf("- Invalid parameters!!!\n");
        printf(
                "- Usage %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n",
                argv[0]);
        exit(-1);
    }

// Create a raw socket with UDP protocol
    sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
    if (sd < 0) {
        perror("socket() error");
// If something wrong just exit
        exit(-1);
    } else
        printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");

// The source is redundant, may be used later if needed
// The address family
    sin.sin_family = AF_INET;
    din.sin_family = AF_INET;
// Port numbers
    sin.sin_port = htons(atoi(argv[2]));
    din.sin_port = htons(atoi(argv[4]));
// IP addresses
    sin.sin_addr.s_addr = inet_addr(argv[1]);
    din.sin_addr.s_addr = inet_addr(argv[3]);

// Fabricate the IP header or we can use the
// standard header structures but assign our own values.
    ip->iph_ihl = 5;
    ip->iph_ver = 4;
    ip->iph_tos = 16; // Low delay
    ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader);
    ip->iph_ident = htons(54321);
    ip->iph_ttl = 64; // hops
    ip->iph_protocol = 17; // UDP
// Source IP address, can use spoofed address here!!!
    ip->iph_sourceip = inet_addr(argv[1]);
// The destination IP address
    ip->iph_destip = inet_addr(argv[3]);

// Fabricate the UDP header. Source port number, redundant
    udp->udph_srcport = htons(atoi(argv[2]));
// Destination port number
    udp->udph_destport = htons(atoi(argv[4]));
    udp->udph_len = htons(sizeof(struct udpheader));
// Calculate the checksum for integrity
    ip->iph_chksum = csum((unsigned short *) buffer,
            sizeof(struct ipheader) + sizeof(struct udpheader));
// Inform the kernel do not fill up the packet structure. we will build our own...
    if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0) {
        perror("setsockopt() error");
        exit(-1);
    } else
        printf("setsockopt() is OK.\n");

// Send loop, send for every 2 second for 100 count
    printf("Trying...\n");
    printf("Using raw socket and UDP protocol\n");
    printf("Using Source IP: %s port: %u, Target IP: %s port: %u.\n", argv[1],
            atoi(argv[2]), argv[3], atoi(argv[4]));

    int count;
    for (count = 1; count <= 20; count++) {
        if (sendto(sd, buffer, ip->iph_len, 0, (struct sockaddr *) &sin,
                sizeof(sin)) < 0)
// Verify
                {
            perror("sendto() error");
            exit(-1);
        } else {
            printf("Count #%u - sendto() is OK.\n", count);
            printf ("Packet Send. iph_sourceip: %s \n" , inet_ntoa(ip->iph_sourceip));
            sleep(2);
        }
    }
    close(sd);
    return 0;
}