I’m trying to create a program that will be able create and send tcp packages, but keep getting these errors when compiling.
error: expected identifier or ‘(’ before ‘,’ token
and
error: ‘sin’ undeclared (first use in this function)
error: ‘din’ undeclared (first use in this function)
Now I’m sure these are the result of something really obvious, but I’ve stared myself blind on this piece of code and can’t get my head around it. The function is as follows:
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);
}
I was under the impression that:
struct sockaddr_in, sin, din;
would be sufficient, but it’s obiously not. This is also the line the expected identifier error message points to. What am I missing?
Remove the first comma
sockaddr_inis the name of a struct so I am assuming with the above line you are trying to declare two variables of typesockadddr_incalledsinanddin. If so you need to remove that comma as a variable name is expected afterstruct sockaddr_in. What you have done is the same as trying the following:which would also not compile as immediately after the typename
intthe compiler expects a variable name.Remove that comma and you should no longer get your compilation error.