我正在尝试创建一个能够创建和发送 tcp 包的程序,但在编译时不断收到这些错误。
error: expected identifier or ‘(’ before ‘,’ token
和
error: ‘sin’ undeclared (first use in this function)
error: ‘din’ undeclared (first use in this function)
现在我确信这些是非常明显的结果,但我对这段代码视而不见,无法理解它。功能如下:
int send_tcp()
{
int sock, one = 1;
char buffer[PCKT_LEN];
struct sockaddr_in, sin, din;
const int *val = &one;
sock = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
if (sock < 0)
{
printf("\nError: socket()\n\n");
exit (-1);
}
else
printf ("\nsocket() - Using SOCK_RAW and TCP protocol is OK.\n\n");
/* Size of the headers */
struct ipheader *ip = (struct ipheader *) buffer;
struct tcpheader *tcp = (struct tcpheader *) (buffer + sizeof (struct ipheader));
memset (buffer, 0, PCKT_LEN);
/* IP attributes */
ip->iph_ihl = 5;
ip->iph_ver = 4;
ip->iph_tos = 16;
ip->iph_len = sizeof(struct ipheader) + sizeof(struct tcpheader);
ip->iph_id = htons(54321);
ip->iph_offset = 0;
ip->iph_ttl = 64;
ip->iph_protocol = 6;
ip->iph_chksum = 0;
ip->iph_sourceip = sip;
ip->iph_destip = dip;
/* TCP attributes */
tcp->tcph_sourceport = sport;
tcp->tcph_destport = dport;
tcp->tcph_seqnum = htonl(1);
tcp->tcph_acknum = 0;
tcp->tcph_offset = 5;
tcp->tcph_syn = 1;
tcp->tcph_ack = 0;
tcp->tcph_win = htons(32767);
tcp->tcph_chksum = 0;
tcp->tcph_urgptr = 0;
ip->iph_chksum = checksum ((unsigned short *) buffer, (sizeof (struct ipheader )+ sizeof (struct tcpheader)));
/* Address family */
sin.sin_family = AF_INET;
din.sin_family = AF_INET;
/* Source port */
sin.sin_port = sport;
din.sin_port = dport;
/* Source IP */
sin.sin_addr.s_addr = sip;
din.sin_addr.s_addr = dip;
/* Tell the Kernel we're building our own packet */
if ((setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof (one))) < 0)
{
printf("\nError: Can't set socketoptions\n\n");
return (-1);
}
/* Send */
if (sendto(sock, buffer, ip->iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
printf("\nError: Can't send packet\n\n");
return (-1);
}
else
printf("Packet sent to %s", dip);
close(sock);
}
我的印象是:
struct sockaddr_in, sin, din;
就足够了,但显然不是。这也是预期标识符错误消息指向的行。我错过了什么?