- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我运行时
cc -c -o rawudp.o rawudp.c
我明白了
在 rawudp.c:1:0 包含的文件中:
rawudp.h:48:23: 错误:未知类型名称‘sockaddr’
make: * [rawudp.o] 错误 1
这是我使用的头文件。
#ifndef RAWUDP_H_
#define RAWUDP_H_ value
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
// The packet length
#define PCKT_LEN 8192
//IP Header Constants
#define IP_HEADER_IHL 5;
#define IP_HEADER_VERSION 4;
#define IP_HEADER_TOS 16;
#define IP_HEADER_TTL 64;
#define IP_HEADER_PROTOCOL 17;
typedef 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;
} ipheader;
// UDP header's structure
typedef struct udpheader {
unsigned short int udph_srcport;
unsigned short int udph_destport;
unsigned short int udph_len;
unsigned short int udph_chksum;
} udpheader;
#endif
#include "rawudp.h"
// 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);
}
// Create a raw socket with UDP protocol
int udp_socket_init(){
int socket_descriptor;
socket_descriptor = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if(socket_descriptor < 0)
{
perror("socket() error");
// If something wrong just exit
exit(-1);
}
else
return socket_descriptor;
//printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");
}
int ip_socket_init(sockaddr_in* udp_socket, char* port, char* ip_address){
udp_socket->sin_family = AF_INET;
//Convert and set port number
if (!(udp_socket->sin_port = htons(atoi(port)))){
printf("Could not set port number\n");
exit(-1);
}
//Convert and set IP Address
if (!(udp_socket->sin_addr.s_addr = inet_addr(ip_address)))
{
printf("Could not set IP address\n");
exit(-1);
}
return 1;
}
int ip_header_init(ipheader* ip, char* source_ip, char* dest_ip, size_t size){
// Fabricate the IP header or we can use the
// standard header structures but assign our own values.
ip->iph_ihl = IP_HEADER_IHL;
ip->iph_ver = IP_HEADER_VERSION;
ip->iph_tos = IP_HEADER_TOS; // Low delay
ip->iph_len = size;
ip->iph_ident = htons(54321);
ip->iph_ttl = IP_HEADER_TTL; // hops
ip->iph_protocol = IP_HEADER_PROTOCOL; // UDP
// Source IP address, can use spoofed address here!!!
ip->iph_sourceip = inet_addr(source_ip);
// The destination IP address
ip->iph_destip = inet_addr(dest_ip);
}
int udp_header_init(udpheader* udp, char* source_port, char* dest_port){
// Fabricate the UDP header. Source port number, redundant
udp->udph_srcport = htons(atoi(source_port));
// Destination port number
udp->udph_destport = htons(atoi(dest_port));
udp->udph_len = htons(sizeof(struct udpheader));
}
// Source IP, source port, target IP, target port from the command line arguments
int main(int argc, char *argv[])
{
int socket_descriptor;
// 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;
struct sockaddr_in din;
socket_descriptor = udp_socket_init();
int one = 1;
const int *val = &one;
bzero(buffer, 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);
}
ip_socket_init(&sin, argv[2], argv[1]);
ip_socket_init(&din, argv[4], argv[3]);
ip_header_init(ip, argv[1], argv[3], sizeof(struct ipheader) + sizeof(struct udpheader));
udp_header_init(udp, argv[2], argv[4]);
// 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(socket_descriptor, 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(socket_descriptor, 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);
sleep(2);
}
}
close(socket_descriptor);
return 0;
}
我相信我已经包含了正确的头文件,但无法找到头文件中未包含 sockaddr 的原因。我也试过 sockaddr_in 但仍然没有骰子。
最佳答案
如您所说,感谢大家的帮助。
使用它时你需要这样指定(也在函数声明中)
struct sockaddr_in foo;
int bar(struct sockaddr_in);
关于c - 未知类型名称 ‘sockaddr’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17309853/
这个问题在这里已经有了答案: C++ Redefinition Header Files (winsock2.h) (15 个答案) 关闭 6 年前。 我使用 Visual Studio 2012
struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_da
当我运行时 cc -c -o rawudp.o rawudp.c 我明白了 在 rawudp.c:1:0 包含的文件中: rawudp.h:48:23: 错误:未知类型名称‘sockaddr’ mak
我想知道确定使用哪个 sockaddr 变体的正确方法。 来自Beej's Guide to Network Programming : if (p->ai_family == AF_INET) {
我目前正在编写一个使用网络结构(例如 sockaddr)的 C 库。在我的代码中,我使用这个三元条件来推断这种结构的大小。 addrlen = sourceaddr->sa_family ==
在尝试以下操作时,第二个 sockaddr 中的地址发生了变化: /*Stuff*/ sockaddr add1, add2; recvfrom(/*socket*/, /*buffer*/, /*c
我正在尝试创建一个 void mksockaddr(int af, int proto, char addr[], struct sockaddr* dst) 来创建一个 sockaddr 结构,这是
我正在尝试学习网络编程,并在此过程中学习 C。我对作为通用地址的结构 sockaddr 和 sockaddr_in 感到困惑。我的书是这样说的: Thus, we can fill in the fi
根据msdn的这篇文章( http://msdn.microsoft.com/en-us/library/windows/desktop/ms740496(v=vs.85).aspx ) 结构因选择的
我正在打开一个 Linux 数据包套接字并尝试将接收到的数据包读入一个结构中: type msg struct { n, oobn, flags int p, oob []byte
我一直在阅读 Beej 的网络编程指南,在他的一个示例中,他将指向 struct sockaddr 的指针转换为 struct sockaddr_in6 指针,如下所示。 void *addr
'operator sockaddr *()'在这里是什么意思?类 Raw 是一个内部类,sockaddr 是一个结构。 struct sockaddr { __SOCKADDR_COMM
var sa_tab:[sockaddr?] = [sockaddr](repeating: sockaddr(), count: Int(RTAX_MAX)) let addr:sockaddr =
我遇到过这段代码: (stuct sockadrr*)&servaddr 这是什么意思?您以某种方式将结构强制转换为 servaddr? 我只是想弄清楚这段代码的语法及其在 C 中的含义。 最佳答案
概览 我正在用 C++ 编写一个简单的测试游戏服务器。目前,我有一个监听任何人的绑定(bind)套接字,当某种数据包类型到达时,我想将 sockaddr 结构保存到一个对象中,该对象将传递给一个单独的
我正在编写一个从命令行提供地址的 UDP 套接字程序。 为了使发送和写入更容易,我使用 getaddrinfo 将地址转换为 sockaddr 结构:sockaddr_in 或 sockaddr_in
手册页未涵盖在发生错误时通过引用传递到 accept() 函数的 sockaddr 变量的状态。 是否可以安全地假设如果在客户端连接和您接受它之间出现问题,导致 accept() 的返回值小于 0,那
目前我正在尝试部署一个用C++编写的网络应用程序,由于网络问题,它看起来陷入了死循环: [isaev@feck-5 ~]$ sudo strace -p 26252 -f -e trace=netwo
我用 C 编写了一个简短的 TCP 服务器示例,它持续监听端口 12701 上的连接,并将对等方的 sockaddr.sa_family 的值输出到标准输出。程序在无限循环中调用 accept(),它
我在 Mac OS X 中做了一些干预(本质上是拦截 C 调用),我注意到 ping 应用程序试图调用 addrlen 值为 16 的 sendto 函数。在 sys/socket.h 中我可以清楚地
我是一名优秀的程序员,十分优秀!