[Help] libnids compilation error-Linux general technology-Linux programming and kernel information. The following is a detailed description. I use fedora 7. After libpcap libnet libnids is installed,
The following code compilation error is executed on the terminal
# Gcc-o ip. c-l nids-l pcap-l net
Error message:
Ip. c: 26: Error: expected ':', '}' or '_ attribute _ 'before'. 'token
Ip. c: In the 'ICMP _ protocol_packet_callback' function:
Ip. c: 108: Error: 'struct icmp_header 'does not have a member named 'ICMP _ sequence'
Ip. c: 109: Error: 'struct icmp_header 'does not have a member named 'hun'
Ip. c: 115: Error: 'struct icmp_header 'does not have a member named 'hun'
Ip. c: 116: Error: 'struct icmp_header 'does not have a member named 'ICMP _ sequence'
Ip. c: In the 'IP _ callback' function:
Ip. c: 313: Warning: transfer parameter 1 (which belongs to 'IP _ protocol_packet_callback') to convert between incompatible pointer types
Ip. c: In the 'main' function:
Ip. c: 322: Warning: The return type of 'main' is not 'int'
I am surprised that the hun variable is not displayed in the code, and the icmp_sequence variable is also included in the icmp_header structure defined at the beginning.
The other 26 lines are correct syntax, but the error prompt says there is a missing front:; and so on, let me feel helpless...
Help everyone !!
The ip. c code is as follows:
(This code can be downloaded and changed to ip. c .)
CODE: # include "nids. h"
# Include "pcap. h"
# Include "libnet. h"
/*
Bytes -----------------------------------------------------------------------------------------------------------------------
Data Structure of the UDP protocol header
Bytes -----------------------------------------------------------------------------------------------------------------------
*/
Struct udp_header
{
Unsigned short udp_source_port;
Unsigned short udp_destination_port;
Unsigned short udp_length;
Unsigned short udp_checksum;
};
/*
Bytes -----------------------------------------------------------------------------------------------------------------------
Data Structure of the ICMP protocol header
Bytes -----------------------------------------------------------------------------------------------------------------------
*/
Struct icmp_header
{
Unsigned int icmp_type;
Unsigned int icmp_code;
Unsigned char icmp_checksum;
Unsigned char icmp_id;
Unsigned char icmp_sequence;
};
/*
Bytes -----------------------------------------------------------------------------------------------------------------------
Data Structure of the IP protocol header
Bytes -----------------------------------------------------------------------------------------------------------------------
*/
Struct ip_header
{
# If defined (WORDS_BIGENDIAN)
Unsigned char ip_version: 4,/* version */
Ip_header_length: 4;/* Header Length */
# Else
Unsigned char ip_header_length: 4, ip_version: 4;
# Endif
Unsigned char ip_tos;/* service type */
Unsigned short ip_length;/* total length */
Unsigned short ip_id;/* ID */
Unsigned short ip_off;/* flag and offset */
Unsigned char ip_ttl;/* survival time */
Unsigned char ip_protocol;/* protocol type */
Unsigned short ip_checksum;/* checksum */
Struct in_addr ip_souce_address;/* Source IP Address */
Struct in_addr ip_destination_address;/* destination IP address */
};
/*
Bytes -----------------------------------------------------------------------------------------------------------------------
TCP protocol header
Bytes -----------------------------------------------------------------------------------------------------------------------
*/
Struct tcp_header
{
Unsigned char tcp_source_port;/* Source Port Number */
Unsigned char tcp_destination_port;/* destination port number */
Unsigned short tcp_sequence;/* learn the column Code */
Unsigned short tcp_acknowledgement;/* confirmation number */
# Ifdef WORDS_BIGENDIAN
Unsigned int tcp_offset: 4,/* Data offset */
Tcp_reserved: 4;/* Reserved */
# Else
Unsigned int tcp_reserved: 4,/* Reserved */
Tcp_offset: 4;/* Data offset */
# Endif
Unsigned int tcp_flags;/* flag */
Unsigned char tcp_windows;/* window size */
Unsigned char tcp_checksum;/* checksum */
Unsigned char tcp_urgent_pointer;/* Emergency pointer */
};
Char ascii_string [10000];
Char * char_to_ascii (char ch)
{
Char * string;
Ascii_string [0] = 0;
String = ascii_string;
If (isgraph (ch ))
* String ++ = ch;
Else if (ch = '')
* String ++ = ch;
Else if (ch = '\ n' | ch =' \ R ')
* String ++ = ch;
Else
* String ++ = '.';
* String = 0;
Return ascii_string;
}
/*
========================================================== ========================================================== ========================================================
The following describes the ICMP functions.
========================================================== ========================================================== ========================================================
*/
Void icmp_protocol_packet_callback (const u_char * packet_content)
{
Struct icmp_header * icmp_protocol;
Icmp_protocol = (struct icmp_header *) (packet_content + 14 + 20 );
Printf ("---------- ICMP protocol ---------- \ n ");
Printf ("ICMP Type: % d \ n", icmp_protocol-> icmp_type );
Switch (icmp_protocol-> icmp_type)/* ICMP type */
{
Case 8:
Printf ("ICMP Echo Request Protocol \ n ");
Printf ("ICMP code: % d \ n", icmp_protocol-> icmp_code );
Printf ("sequence code: % d \ n", icmp_protocol-> icmp_sequence );
Printf ("identifier: % d \ n", icmp_protocol-> icmp_id );
Break;
Case 0:
Printf ("ICMP echo response protocol \ n ");
Printf ("ICMP code: % d \ n", icmp_protocol-> icmp_code );
Printf ("identifier: % d \ n", icmp_protocol-> icmp_id );
Printf ("sequence code: % d \ n", icmp_protocol-> icmp_sequence );
Break;
Default:
Break;
}
Printf ("ICMP checksum: % d \ n", ntohs (icmp_protocol-> icmp_checksum);/* Get the checksum */
Return;
}
/*
========================================================== ========================================================== ========================================================
The following is a function for analyzing the TCP protocol.
========================================================== ========================================================== ========================================================
*/
Void tcp_protocol_packet_callback (const u_char * packet_content)
{
Struct tcp_header * tcp_protocol;
U_char flags;
Int header_length;
U_short source_port;
U_short destination_port;
U_short windows;
U_short urgent_pointer;
U_int sequence;
U_int acknowledgement;
Unsigned char checksum;
Tcp_protocol = (struct tcp_header *) (packet_content + 14 + 20 );
Source_port = ntohs (tcp_protocol-> tcp_source_port );
/* Obtain the source port number */
Destination_port = ntohs (tcp_protocol-> tcp_destination_port );
/* Obtain the destination port number */
Header_length = tcp_protocol-> tcp_offset * 4;
/* Obtain the header length */
Sequence = ntohl (tcp_protocol-> tcp_sequence );
/* Obtain the sequence code */
Acknowledgement = ntohl (tcp_protocol-> tcp_acknowledgement );
/* Get the confirmation number */
Windows = ntohs (tcp_protocol-> tcp_windows );
/* Obtain the window size */
Urgent_pointer = ntohs (tcp_protocol-> tcp_urgent_pointer );
/* Get the emergency pointer */
Flags = tcp_protocol-> tcp_flags;
Checksum = ntohs (tcp_protocol-> tcp_checksum );
Printf ("------- TCP protocol ------- \ n ");
Printf ("Source Port: % d \ n", source_port );
Printf ("Destination Port: % d \ n", destination_port );
Switch (destination_port)
{
Case 80:
Printf ("the upper layer protocol is HTTP \ n ");
Break;
Case 21:
Printf ("the upper-layer protocol is the FTP protocol \ n ");
Break;
Case 23:
Printf ("the upper-layer protocol is the TELNET protocol \ n ");
Break;
Case 25:
Printf ("the upper-layer protocol is SMTP protocol \ n ");
Break;
Case 110:
Printf ("POP3 protocol \ n ");
Break;
Default:
Break;
}
Printf ("sequence code: % u \ n", sequence );
Printf ("Confirmation Number: % u \ n", acknowledgement );
Printf ("Header Length: % d \ n", header_length );
Printf ("retain: % d \ n", tcp_protocol-> tcp_reserved );
Printf ("tag :");
If (flags & 0x08)
Printf ("PSH ");
If (flags & 0x10)
Printf ("ACK ");
If (flags & 0x02)
Printf ("SYN ");
If (flags & 0x20)
Printf ("URG ");
If (flags & 0x01)
Printf ("FIN ");
If (flags & 0x04)
Printf ("RST ");
Printf ("\ n ");
Printf ("window size: % d \ n", windows );
Printf ("checksum: % d \ n", checksum );
Printf ("Emergency pointer: % d \ n", urgent_pointer );
}
/*
========================================================== ========================================================== ========================================================
The following describes the UPD protocol functions.
========================================================== ========================================================== ========================================================
*/
Void udp_protocol_packet_callback (u_char * packet_content)
{
Struct udp_header * udp_protocol;
U_short source_port;
U_short destination_port;
U_short length;
Udp_protocol = (struct udp_header *) (packet_content + 20 );
Source_port = ntohs (udp_protocol-> udp_source_port );
/* Obtain the source port number */
Destination_port = ntohs (udp_protocol-> udp_destination_port );
/* Obtain the destination port number */
Length = ntohs (udp_protocol-> udp_length );
Printf ("---------- UDP protocol header ---------- \ n ");
Printf ("Source Port: % d \ n", source_port );
Printf ("Destination Port: % d \ n", destination_port );
Switch (destination_port)
{
Case 138:
Printf ("NETBIOS datainservice \ n ");
Break;
Case 137:
Printf ("NETBIOS Name Service \ n ");
Break;
Case 139:
Printf ("NETBIOS session service \ n ");
Break;
Case 53:
Printf ("name-domain server \ n ");
Break;
Default:
Break;
}
Printf ("length: % d \ n", length );
Printf ("checksum: % d \ n", ntohs (udp_protocol-> udp_checksum ));
}
/*
========================================================== ========================================================== ========================================================
The following is a function for analyzing the IP protocol.
========================================================== ========================================================== ========================================================
*/
Void ip_protocol_packet_callback (u_char * packet_content)
{
Struct ip_header * ip_protocol;
U_int header_length;
U_int offset;
U_char tos;
Unsigned short checksum;
Printf ("---------- IP protocol header ---------- \ n ");
Ip_protocol = (struct ip_header *) (packet_content );
Checksum = ntohs (ip_protocol-> ip_checksum );
/* Get the checksum */
Header_length = ip_protocol-> ip_header_length * 4;
/* Obtain the header length */
Tos = ip_protocol-> ip_tos;
Offset = ntohs (ip_protocol-> ip_off );
Printf ("IP version: % d \ n", ip_protocol-> ip_version );
Printf ("Header Length: % d \ n", header_length );
Printf ("TOS: % d \ n", tos );
Printf ("total length: % d \ n", ntohs (ip_protocol-> ip_length ));
Printf ("identifier: % d \ n", ntohs (ip_protocol-> ip_id ));
Printf ("offset: % d \ n", (offset & 0x1fff) * 8 );
Printf ("survival time: % d \ n", ip_protocol-> ip_ttl );
Printf ("Protocol: % d \ n", ip_protocol-> ip_protocol );
Switch (ip_protocol-> ip_protocol)/* determine the upper-layer protocol type */
{
Case 6:
Printf ("the upper layer protocol is TCP \ n ");
Break;
Case 17:
Printf ("UDP \ n ");
Break;
Case 1:
Printf ("the upper layer protocol is ICMP \ n ");
Break;
Default:
Break;
}
Printf ("checksum: % d \ n", checksum );
Printf ("Source IP Address: % s \ n", inet_ntoa (ip_protocol-> ip_souce_address ));
Printf ("Destination IP Address: % s \ n", inet_ntoa (ip_protocol-> ip_destination_address ));
Switch (ip_protocol-> ip_protocol)
{
Case 17:
Udp_protocol_packet_callback (packet_content );
Break;
/* When the upper-layer protocol is UDP, call the analysis function of UDP */
Case 6:
Tcp_protocol_packet_callback (packet_content );
Break;
/* When the upper-layer protocol is TCP, call and analyze the functions of TCP */
Case 1:
Icmp_protocol_packet_callback (packet_content );
Break;
/* When the upper-layer protocol is ICMP, call the function for analyzing ICMP */
Default:
Break;
}
}
/*
========================================================== ========================================================== ========================================================
The following is the callback function.
========================================================== ========================================================== ========================================================
*/
Void ip_callback (struct ip * a_packet, int len)
{
Ip_protocol_packet_callback (a_packet );
/* Call the function for analyzing the IP protocol */
}
/*
========================================================== ========================================================== ========================================================
Main Function
========================================================== ========================================================== ========================================================
*/
Void main ()
{
If (! Nids_init ())
/* Initialize Libnids */
{
Printf ("error: % s \ n", nids_errbuf );
Exit (1 );
}
Nids_register_ip_frag (ip_callback );
/* Register and analyze the callback function of the IP protocol */
Nids_run ();
/* Enter the cyclic packet capture status */
} |