'asm' or '__attribute__' before 'int' - c

I am getting the error below while doing Linux migration. Line no 25 from the file is below. Please suggest, added my_val.h
In file included from ddtc.c:36:
ddtc.h:25: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'int'
make: *** [ddtc.o] Error 1
ddtc.h
18 #pragma ident "$Id: ddtc.h 2 28-JUN-2014 09:31:15 DDAC$"
19
20 #ifndef _INCL_STRING_FDS_
21 #define _INCL_STRING_FDS_
22
23 #include "my_val.h"
24
25 int stringFDS(char ** s, char * myVar); /* String feeder*/
26
27 #endif
my_val.h
#ifndef _TYPE_VALUE_INC_
#define _TYPE_VALUE_INC_
/* Return value */
#ifndef RETURN_PASS
#define RETURN_PASS 0
#endif
#ifndef RETURN_DENY
#define RETURN_DENY -1
#endif
#define RETURN_EOF 1 /* Processing done due to EOF */
#ifndef WIN32
#ifndef TYPEDEF_BOOL_FOR_C
#define TYPEDEF_BOOL_FOR_C
#ifndef bool
typedef int bool
#endif
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
#endif /* ndefined TYPEDEF_BOOL_FOR_C */
#endif /* ndefined WIN32 */
#endif /* ndefined _TYPE_VALUE_INC_ */

Related

Xcode duplicate symbol '_main'

I have a bunch of .O files that I generated from a makefile.u which compiles some .C files. I put all of the .O files into my Xcode project and I receive the below error when it builds:
duplicate symbol '_main' in:
/Users/.../Objects-normal/arm64/AppDelegate.o
/Users/.../LIBF2C/main.o
ld: 1 duplicate symbol for architecture arm64
I think I am bit confused because the main.o file is needed for my library, but the error seems to be telling me that I cannot have _main declared twice. So is there a solution for this, am I supposed to rename something to get it to build or any other ideas for how this can get resolved?
For some added context, this is specifically for the f2c libraries. So main.c(->main.o) from libf2c is:
/* STARTUP PROCEDURE FOR UNIX FORTRAN PROGRAMS */
#include "stdio.h"
#include "signal1.h"
#ifndef SIGIOT
#ifdef SIGABRT
#define SIGIOT SIGABRT
#endif
#endif
#ifndef KR_headers
#undef VOID
#include "stdlib.h"
#ifdef __cplusplus
extern "C" {
#endif
#endif
#ifndef VOID
#define VOID void
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef NO__STDC
#define ONEXIT onexit
extern VOID f_exit();
#else
#ifndef KR_headers
extern void f_exit(void);
#ifndef NO_ONEXIT
#define ONEXIT atexit
extern int atexit(void (*)(void));
#endif
#else
#ifndef NO_ONEXIT
#define ONEXIT onexit
extern VOID f_exit();
#endif
#endif
#endif
#ifdef KR_headers
extern VOID f_init(), sig_die();
extern int MAIN__();
#define Int /* int */
#else
extern void f_init(void), sig_die(const char*, int);
extern int MAIN__(void);
#define Int int
#endif
static VOID sigfdie(Sigarg)
{
Use_Sigarg;
sig_die("Floating Exception", 1);
}
static VOID sigidie(Sigarg)
{
Use_Sigarg;
sig_die("IOT Trap", 1);
}
#ifdef SIGQUIT
static VOID sigqdie(Sigarg)
{
Use_Sigarg;
sig_die("Quit signal", 1);
}
#endif
static VOID sigindie(Sigarg)
{
Use_Sigarg;
sig_die("Interrupt", 0);
}
static VOID sigtdie(Sigarg)
{
Use_Sigarg;
sig_die("Killed", 0);
}
#ifdef SIGTRAP
static VOID sigtrdie(Sigarg)
{
Use_Sigarg;
sig_die("Trace trap", 1);
}
#endif
int xargc;
char **xargv;
#ifdef __cplusplus
}
#endif
int
#ifdef KR_headers
main(argc, argv) int argc; char **argv;
#else
main(int argc, char **argv)
#endif
{
xargc = argc;
xargv = argv;
signal1(SIGFPE, sigfdie); /* ignore underflow, enable overflow */
#ifdef SIGIOT
signal1(SIGIOT, sigidie);
#endif
#ifdef SIGTRAP
signal1(SIGTRAP, sigtrdie);
#endif
#ifdef SIGQUIT
if(signal1(SIGQUIT,sigqdie) == SIG_IGN)
signal1(SIGQUIT, SIG_IGN);
#endif
if(signal1(SIGINT, sigindie) == SIG_IGN)
signal1(SIGINT, SIG_IGN);
signal1(SIGTERM,sigtdie);
#ifdef pdp11
ldfps(01200); /* detect overflow as an exception */
#endif
f_init();
#ifndef NO_ONEXIT
ONEXIT(f_exit);
#endif
MAIN__();
#ifdef NO_ONEXIT
f_exit();
#endif
exit(0); /* exit(0) rather than return(0) to bypass Cray bug */
return 0; /* For compilers that complain of missing return values; */
/* others will complain that this is unreachable code. */
}
#ifdef __cplusplus
}
#endif

Convert <netinet/in.h> protocols to names?

There is a long list of protocols defined in <netinet/in.h>:
/*
* Protocols (RFC 1700)
*/
#define IPPROTO_IP 0 /* dummy for IP */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define IPPROTO_HOPOPTS 0 /* IP6 hop-by-hop options */
#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
#define IPPROTO_ICMP 1 /* control message protocol */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define IPPROTO_IGMP 2 /* group mgmt protocol */
#define IPPROTO_GGP 3 /* gateway^2 (deprecated) */
#define IPPROTO_IPV4 4 /* IPv4 encapsulation */
#define IPPROTO_IPIP IPPROTO_IPV4 /* for compatibility */
#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
#define IPPROTO_TCP 6 /* tcp */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define IPPROTO_ST 7 /* Stream protocol II */
/* ... */
Is there a default function/call to translate protocols defined in <netinet/in.h> to names? Ideal to have chars[] in return:
ipproto2str(IPPROTO_ICMP): "Control Message Protocol (ICMP)"
You're looking for getprotobynumber().
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
int main(void) {
struct protoent *p = getprotobynumber(IPPROTO_ICMP);
if (p) {
printf("IPPROTO_ICMP (%d) = %s\n", p->p_proto, p->p_name);
} else {
puts("Couldn't find IPPROTO_ICMP");
}
}

How can I choose function behavior at compile time?

I have 4 files in the following example ; a header, a default implementation file, and 2 platform-specific implementation files.I define two functions; get_value_1 and get_value_2. The 'default' behavior is to return -1, but some of these functions have special implementations. I would like each function to return -1 only if another file didn't implement it.
/* interface.h *
***************/
int get_value_1();
int get_value_2();
/* default.c *
*************/
#include "interface.h"
#ifndef GET_VALUE_1
int get_value_1() { return -1; }
#endif
#ifndef GET_VALUE_2
int get_value_2() { return -1; }
#endif
/* platform1.c *
***************/
#include "interface.h"
#ifndef GET_VALUE_1
#define GET_VALUE_1
int get_value_1() { return 1; }
#endif
/* platform2.c *
***************/
#include "interface.h"
#ifndef GET_VALUE_2
#define GET_VALUE_2
int get_value_2() { return 2; }
#endif
But when I run the command gcc default.c platform1.c -shared -fpic -o platform1.so, it tells me that I've multiply defined the get_value_1 function, and that it was originally defined in platform1.c.
So how can I have a set of functions where a subset of those functions can have their behavior chosen at compile time?
make them weak in the default.c and "normal" in your platform files. So if the platform.c file implements this function as not weak, the weak one dfrom default.c will be replaced link time.
It is not the part of the standard but most compilers support it (it can be pragma, attribute or something else - you need to check in the compiler documentation)
gcc version:
#define __weak __attribute__((weak))
/* interface.h *
***************/
int get_value_1();
int get_value_2();
/* default.c *
*************/
#include "interface.h"
#ifndef GET_VALUE_1
__weak int get_value_1() { return -1; }
#endif
#ifndef GET_VALUE_2
__weak int get_value_2() { return -1; }
#endif
/* platform1.c *
***************/
#include "interface.h"
#ifndef GET_VALUE_1
#define GET_VALUE_1
int get_value_1() { return 1; }
#endif
/* platform2.c *
***************/
#include "interface.h"
#ifndef GET_VALUE_2
#define GET_VALUE_2
int get_value_2() { return 2; }
#endif

GCC won't compile program

Alright, so I'm going crazy over this one. I'm currently trying to go through Hacking: Art of Exploitation and I'm on the part about socket programming. So here is the code and the compiler output below it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "hacking.h"
#define PORT 7890 // the port users will be connecting to
int main(void) {
int sockfd, new_sockfd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in host_addr, client_addr; // my address information
socklen_t sin_size;
int recv_length=1, yes=1;
char buffer[1024];
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
fatal("in socket");
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
fatal("setting socket option SO_REUSEADDR");
host_addr.sin_family = AF_INET; // host byte order
host_addr.sin_port = htons(PORT); // short, network byte order
host_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(host_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) == -1)
fatal("binding to socket");
if (listen(sockfd, 5) == -1)
fatal("listening on socket");
while(1) { // Accept loop
sin_size = sizeof(struct sockaddr_in);
new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
if(new_sockfd == -1)
fatal("accepting connection");
printf("server: got connection from %s port %d\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
send(new_sockfd, "Hello World!\n", 13, 0);
recv_length = recv(new_sockfd, &buffer, 1024, 0);
while(recv_length > 0) {
printf("RECV: %d bytes\n", recv_length);
dump(buffer, recv_length);
recv_length = recv(new_sockfd, &buffer, 1024, 0);
}
close(new_sockfd);
}
return 0;
}
Compiler output:
root#root-laptop:~ $ gcc simple_server.c
In file included from /usr/include/sys/socket.h:35,
from simple_server.c:7:
/usr/include/bits/socket.h:122: error: syntax error before "define"
In file included from /usr/include/sys/socket.h:35,
from simple_server.c:7:
/usr/include/bits/socket.h:147: error: syntax error before "sa_family_t"
/usr/include/bits/socket.h:149: error: syntax error before '}' token
/usr/include/bits/socket.h:164: error: syntax error before "sa_family_t"
/usr/include/bits/socket.h:167: error: syntax error before '}' token
In file included from simple_server.c:9:
/usr/include/netinet/in.h:221: error: syntax error before "sa_family_t"
/usr/include/netinet/in.h:226: error: invalid application of `sizeof' to an incomplete type
/usr/include/netinet/in.h:229: error: size of array `sin_zero' is too large
/usr/include/netinet/in.h:230: error: syntax error before '}' token
/usr/include/netinet/in.h:235: error: syntax error before "sa_family_t"
/usr/include/netinet/in.h:240: error: syntax error before '}' token
/usr/include/netinet/in.h:283: error: field `gr_group' has incomplete type
/usr/include/netinet/in.h:292: error: field `gsr_group' has incomplete type
/usr/include/netinet/in.h:295: error: field `gsr_source' has incomplete type
/usr/include/netinet/in.h:327: error: field `gf_group' has incomplete type
/usr/include/netinet/in.h:335: error: field `gf_slist' has incomplete type
In file included from simple_server.c:9:
/usr/include/netinet/in.h:465: error: field `ip6m_addr' has incomplete type
simple_server.c: In function `main':
simple_server.c:17: error: storage size of `host_addr' isn't known
simple_server.c:17: error: storage size of `client_addr' isn't known
simple_server.c:34: error: invalid application of `sizeof' to an incomplete type
simple_server.c:41: error: invalid application of `sizeof' to an incomplete type
simple_server.c: At top level:
/usr/include/netinet/in.h:229: error: storage size of `sin_zero' isn't known
For some reason GCC isn't finding the predefined struct in the header files. I've tried everything to fix this, from removing header files to adding only certain ones.
Also I should note that I'm using the VM image that came with the book. It's an old version of Ubuntu and GCC.
Edit:
Here are the requested files.
socket.h
#ifndef __BITS_SOCKET_H
#define __BITS_SOCKET_H
#if !defined _SYS_SOCKET_H && !defined _NETINET_IN_H
# error "Never include <bits/socket.h> directly; use <sys/socket.h> instead."
#endif
#define __need_size_t
#define __need_NULL
#include <stddef.h>
#include <limits.h>
#include <sys/types.h>
/* Type for length arguments in socket calls. */
#ifndef __socklen_t_defined
typedef __socklen_t socklen_t;
# define __socklen_t_defined
#endif
/* Types of sockets. */
enum __socket_type
{
SOCK_STREAM = 1, /* Sequenced, reliable, connection-based
byte streams. */
#define SOCK_STREAM SOCK_STREAM
SOCK_DGRAM = 2, /* Connectionless, unreliable datagrams
of fixed maximum length. */
#define SOCK_DGRAM SOCK_DGRAM
SOCK_RAW = 3, /* Raw protocol interface. */
#define SOCK_RAW SOCK_RAW
SOCK_RDM = 4, /* Reliably-delivered messages. */
#define SOCK_RDM SOCK_RDM
SOCK_SEQPACKET = 5, /* Sequenced, reliable, connection-based,
datagrams of fixed maximum length. */
#define SOCK_SEQPACKET SOCK_SEQPACKET
SOCK_PACKET = 10 /* Linux specific way of getting packets
at the dev level. For writing rarp and
other similar things on the user level. */
#define SOCK_PACKET SOCK_PACKET
};
/* Protocol families. */
#define PF_UNSPEC 0 /* Unspecified. */
#define PF_LOCAL 1 /* Local to host (pipes and file-domain). */
#define PF_UNIX PF_LOCAL /* Old BSD name for PF_LOCAL. */
#define PF_FILE PF_LOCAL /* Another non-standard name for PF_LOCAL. */
#define PF_INET 2 /* IP protocol family. */
#define PF_AX25 3 /* Amateur Radio AX.25. */
#define PF_IPX 4 /* Novell Internet Protocol. */
#define PF_APPLETALK 5 /* Appletalk DDP. */
#define PF_NETROM 6 /* Amateur radio NetROM. */
#define PF_BRIDGE 7 /* Multiprotocol bridge. */
#define PF_ATMPVC 8 /* ATM PVCs. */
#define PF_X25 9 /* Reserved for X.25 project. */
#define PF_INET6 10 /* IP version 6. */
#define PF_ROSE 11 /* Amateur Radio X.25 PLP. */
#define PF_DECnet 12 /* Reserved for DECnet project. */
#define PF_NETBEUI 13 /* Reserved for 802.2LLC project. */
#define PF_SECURITY 14 /* Security callback pseudo AF. */
#define PF_KEY 15 /* PF_KEY key management API. */
#define PF_NETLINK 16
#define PF_ROUTE PF_NETLINK /* Alias to emulate 4.4BSD. */
#define PF_PACKET 17 /* Packet family. */
#define PF_ASH 18 /* Ash. */
#define PF_ECONET 19 /* Acorn Econet. */
#define PF_ATMSVC 20 /* ATM SVCs. */
#define PF_SNA 22 /* Linux SNA Project */
#define PF_IRDA 23 /* IRDA sockets. */
#define PF_PPPOX 24 /* PPPoX sockets. */
#define PF_WANPIPE 25 /* Wanpipe API sockets. */
#define PF_BLUETOOTH 31 /* Bluetooth sockets. */
#define PF_MAX 32 /* For now.. */
/* Address families. */
#define AF_UNSPEC PF_UNSPEC
#define AF_LOCAL PF_LOCAL
#define AF_UNIX PF_UNIX
#define AF_FILE PF_FILE
#define AF_INET PF_INET
#define AF_AX25 PF_AX25
#define AF_IPX PF_IPX
#define AF_APPLETALK PF_APPLETALK
#define AF_NETROM PF_NETROM
#define AF_BRIDGE PF_BRIDGE
#define AF_ATMPVC PF_ATMPVC
#define AF_X25 PF_X25
#define AF_INET6 PF_INET6
#define AF_ROSE PF_ROSE
#define AF_DECnet PF_DECnet
#define AF_NETBEUI PF_NETBEUI
#define AF_SECURITY PF_SECURITY
#define AF_KEY PF_KEY
#define AF_NETLINK PF_NETLINK
#define AF_ROUTE PF_ROUTE
#define AF_PACKET PF_PACKET
#define AF_ASH PF_ASH
#define AF_ECONET PF_ECONET
#define AF_ATMSVC PF_ATMSVC
#define AF_SNA PF_SNA
#define AF_IRDA PF_IRDA
#define AF_PPPOX PF_PPPOX
o define AF_WANPIPE PF_WANPIPE
#define AF_BLUETOOTH PF_BLUETOOTH
#define AF_MAX PF_MAX
/* Socket level values. Others are defined in the appropriate headers.
XXX These definitions also should go into the appropriate headers as
far as they are available. */
#define SOL_RAW 255
#define SOL_DECNET 261
#define SOL_X25 262
#define SOL_PACKET 263
#define SOL_ATM 264 /* ATM layer (cell level). */
#define SOL_AAL 265 /* ATM Adaption Layer (packet level). */
#define SOL_IRDA 266
/* Maximum queue length specifiable by listen. */
#define SOMAXCONN 128
/* Get the definition of the macro to define the common sockaddr members. */
#include <bits/sockaddr.h>
/* Structure describing a generic socket address. */
struct sockaddr
{
__SOCKADDR_COMMON (sa_); /* Common data: address family and length. */
char sa_data[14]; /* Address data. */
};
/* Structure large enough to hold any socket address (with the historical
exception of AF_UNIX). We reserve 128 bytes. */
#if ULONG_MAX > 0xffffffff
# define __ss_aligntype __uint64_t
#else
# define __ss_aligntype __uint32_t
#endif
#define _SS_SIZE 128
#define _SS_PADSIZE (_SS_SIZE - (2 * sizeof (__ss_aligntype)))
struct sockaddr_storage
{
__SOCKADDR_COMMON (ss_); /* Address family, etc. */
__ss_aligntype __ss_align; /* Force desired alignment. */
char __ss_padding[_SS_PADSIZE];
};
/* Bits in the FLAGS argument to `send', `recv', et al. */
enum
{
MSG_OOB = 0x01, /* Process out-of-band data. */
#define MSG_OOB MSG_OOB
MSG_PEEK = 0x02, /* Peek at incoming messages. */
#define MSG_PEEK MSG_PEEK
MSG_DONTROUTE = 0x04, /* Don't use local routing. */
#define MSG_DONTROUTE MSG_DONTROUTE
#ifdef __USE_GNU
/* DECnet uses a different name. */
MSG_TRYHARD = MSG_DONTROUTE,
# define MSG_TRYHARD MSG_DONTROUTE
#endif
MSG_CTRUNC = 0x08, /* Control data lost before delivery. */
#define MSG_CTRUNC MSG_CTRUNC
MSG_PROXY = 0x10, /* Supply or ask second address. */
#define MSG_PROXY MSG_PROXY
MSG_TRUNC = 0x20,
#define MSG_TRUNC MSG_TRUNC
MSG_DONTWAIT = 0x40, /* Nonblocking IO. */
#define MSG_DONTWAIT MSG_DONTWAIT
MSG_EOR = 0x80, /* End of record. */
#define MSG_EOR MSG_EOR
MSG_WAITALL = 0x100, /* Wait for a full request. */
#define MSG_WAITALL MSG_WAITALL
MSG_FIN = 0x200,
#define MSG_FIN MSG_FIN
MSG_SYN = 0x400,
#define MSG_SYN MSG_SYN
MSG_CONFIRM = 0x800, /* Confirm path validity. */
#define MSG_CONFIRM MSG_CONFIRM
MSG_RST = 0x1000,
#define MSG_RST MSG_RST
MSG_ERRQUEUE = 0x2000, /* Fetch message from error queue. */
#define MSG_ERRQUEUE MSG_ERRQUEUE
MSG_NOSIGNAL = 0x4000, /* Do not generate SIGPIPE. */
#define MSG_NOSIGNAL MSG_NOSIGNAL
MSG_MORE = 0x8000 /* Sender will send more. */
#define MSG_MORE MSG_MORE
};
/* Structure describing messages sent by
`sendmsg' and received by `recvmsg'. */
struct msghdr
{
void *msg_name; /* Address to send to/receive from. */
socklen_t msg_namelen; /* Length of address data. */
struct iovec *msg_iov; /* Vector of data to send/receive into. */
size_t msg_iovlen; /* Number of elements in the vector. */
void *msg_control; /* Ancillary data (eg BSD filedesc passing). */
size_t msg_controllen; /* Ancillary data buffer length.
!! The type should be socklen_t but the
definition of the kernel is incompatible
with this. */
int msg_flags; /* Flags on received message. */
};
/* Structure used for storage of ancillary data object information. */
struct cmsghdr
{
size_t cmsg_len; /* Length of data in cmsg_data plus length
of cmsghdr structure.
!! The type should be socklen_t but the
definition of the kernel is incompatible
with this. */
int cmsg_level; /* Originating protocol. */
int cmsg_type; /* Protocol specific type. */
#if (!defined __STRICT_ANSI__ && __GNUC__ >= 2) || __STDC_VERSION__ >= 199901L
__extension__ unsigned char __cmsg_data __flexarr; /* Ancillary data. */
#endif
};
/* Ancillary data object manipulation macros. */
#if (!defined __STRICT_ANSI__ && __GNUC__ >= 2) || __STDC_VERSION__ >= 199901L
# define CMSG_DATA(cmsg) ((cmsg)->__cmsg_data)
#else
# define CMSG_DATA(cmsg) ((unsigned char *) ((struct cmsghdr *) (cmsg) + 1))
#endif
#define CMSG_NXTHDR(mhdr, cmsg) __cmsg_nxthdr (mhdr, cmsg)
#define CMSG_FIRSTHDR(mhdr) \
((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) \
? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) NULL)
#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) \
& (size_t) ~(sizeof (size_t) - 1))
#define CMSG_SPACE(len) (CMSG_ALIGN (len) \
+ CMSG_ALIGN (sizeof (struct cmsghdr)))
#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr,
struct cmsghdr *__cmsg) __THROW;
#ifdef __USE_EXTERN_INLINES
# ifndef _EXTERN_INLINE
# define _EXTERN_INLINE extern __inline
# endif
_EXTERN_INLINE struct cmsghdr *
__NTH (__cmsg_nxthdr (struct msghdr *__mhdr, struct cmsghdr *__cmsg))
{
if ((size_t) __cmsg->cmsg_len < sizeof (struct cmsghdr))
/* The kernel header does this so there may be a reason. */
return 0;
__cmsg = (struct cmsghdr *) ((unsigned char *) __cmsg
+ CMSG_ALIGN (__cmsg->cmsg_len));
if ((unsigned char *) (__cmsg + 1) > ((unsigned char *) __mhdr->msg_control
+ __mhdr->msg_controllen)
|| ((unsigned char *) __cmsg + CMSG_ALIGN (__cmsg->cmsg_len)
> ((unsigned char *) __mhdr->msg_control + __mhdr->msg_controllen)))
/* No more entries. */
return 0;
return __cmsg;
}
#endif /* Use `extern inline'. */
/* Socket level message types. This must match the definitions in
<linux/socket.h>. */
enum
{
SCM_RIGHTS = 0x01 /* Transfer file descriptors. */
#define SCM_RIGHTS SCM_RIGHTS
#ifdef __USE_BSD
, SCM_CREDENTIALS = 0x02 /* Credentials passing. */
# define SCM_CREDENTIALS SCM_CREDENTIALS
#endif
};
/* User visible structure for SCM_CREDENTIALS message */
struct ucred
{
pid_t pid; /* PID of sending process. */
uid_t uid; /* UID of sending process. */
gid_t gid; /* GID of sending process. */
};
/* Get socket manipulation related informations from kernel headers. */
#include <asm/socket.h>
/* Structure used to manipulate the SO_LINGER option. */
struct linger
{
int l_onoff; /* Nonzero to linger on close. */
int l_linger; /* Time to linger. */
};
#endif /* bits/socket.h */
hacking.h
#include <stdlib.h>
//This function is used to display packet data by the server program.
void dump(const unsigned char *data_buffer, const unsigned int length){
unsigned char byte;
unsigned int i, j;
for(i = 0; i < length; i++){
byte = data_buffer[i];
printf("%02x ", data_buffer[i]); //Display byte in hex
if((( i % 16) == 15 ) || ( i == length - 1)) {
for( j = 0; j < 15 - ( i % 16 ); j++ )
printf(" ");
printf("| ");
for(j = ( i - ( i % 16 )); j <= i; j++) { //Display printable bytes from line.
byte = data_buffer[j];
if((byte > 31) && (byte < 127))//Outside printable char range
printf("%c", byte);
else
printf(".");
}
printf("\n"); // End of the dump line (each line is 16 bytes)
}//End if
}//End for
}
void fatal(char *message){
char error_message[100];
strcpy(error_message, "[!!] Fatal Error");
strncat(error_message, message, 83);
perror(error_message);
exit(-1);
}
void *ec_malloc(unsigned int size){
void *ptr;
ptr = malloc(size);
if(ptr== NULL)
fatal("in ec_malloc() on memory allocation");
return ptr;
}
in.h
https://pastebin.com/RTjSdA2e
Ok. So the problem was in socket.h. Basically this line
o define AF_WANPIPE PF_WANPIPE
needed to be changed to this
#define AF_WANPIPE PF_WANPIPE
Thanks David Wohlferd.
the contents of the hacking.h header file are not correct.
The contents should be the 'prototypes' for those functions and the actual functions should be in a separate .c file, probably named hacking.c
I.E. the contents of the hacking.h file should be:
#ifndef HACKING_H
#define HACKING_H
//This function is used to display packet data by the server program.
void dump(const unsigned char *data_buffer, const unsigned int length);
void fatal(char *message);
void *ec_malloc(unsigned int size);
#endif // HACKING_H
Notice the 'include only once' wrapper; consisting of the #ifndef, #define, and the trailing #endif statements
notice the file only contains the prototypes (it could also contain any other #define statements and/or data definitions and/or macro definitions, although in this case there are none.)
Then there should be a separate file hacking.c that contains the actual functions (and a statement: #include "hacking.h")
in general, neither function bodies nor data declarations should be in a header file. (later on, you will learn of certain exceptions to this statement, like when declaring inline functions

Compile time check with C preprocessor directives

Is there a way to make the log-level checking in my LOGGING-macro (shown below) to happen during compile-time? It should be possible as it is already known during compile time whether the condition (if (pLogLevel <= LOG_LEVEL)) is true or false. Here is an example of how the macro could be used:
LOGGING(LOG_DEBUG, "FALSE - Wrong Marker\n");
Originally I thought that #ifdirectives within a #define could help me, but this article showed me that this would not be possible. This is the source code to my problem:
#ifndef __LOGGING_H__
#define __LOGGING_H__ 1
#include <time.h>
#define LOG_EMERG 0
#define LOG_ALERT 1
#define LOG_CRIT 2
#define LOG_ERROR 3
#define LOG_WARN 4
#define LOG_NOTICE 5
#define LOG_INFO 6
#define LOG_DEBUG 7
#define LOG_NONE 8
/* set the global logging level here */
#define LOG_LEVEL LOG_INFO
void print_timestamp(void);
#if LOG_LEVEL == LOG_DEBUG
#define _LOG_PREAMBLE \
fprintf(stdout, "%s:%d ", __FILE__, __LINE__);
#else
#define _LOG_PREAMBLE \
print_timestamp();
#endif
#if LOG_LEVEL == LOG_EMERG
#define _LOG_LEVEL fprintf(stdout, "EMERG ");
#elif LOG_LEVEL == LOG_ALERT
#define _LOG_LEVEL fprintf(stdout, "ALERT ");
#elif LOG_LEVEL == LOG_CRIT
#define _LOG_LEVEL fprintf(stdout, "CRIT ");
#elif LOG_LEVEL == LOG_ERROR
#define _LOG_LEVEL fprintf(stdout, "ERROR ");
#elif LOG_LEVEL == LOG_WARN
#define _LOG_LEVEL fprintf(stdout, "WARN ");
#elif LOG_LEVEL == LOG_NOTICE
#define _LOG_LEVEL fprintf(stdout, "NOTICE ");
#elif LOG_LEVEL == LOG_INFO
#define _LOG_LEVEL fprintf(stdout, "INFO ");
#elif LOG_LEVEL == LOG_INFO
#define _LOG_LEVEL fprintf(stdout, "DEBUG ");
#else
#define _LOG_LEVEL
#endif
#define LOGGING(pLogLevel, ...) \
/* change this to compile time conditional if possible */ \
if (pLogLevel <= LOG_LEVEL) \
{ \
_LOG_PREAMBLE \
_LOG_LEVEL \
fprintf(stdout, ##__VA_ARGS__); \
}
#endif /* __LOGGING_H__ */
One approach would be to combine the log level into the logging macro.
This would create a set of macros that would be turned on or off depending on the log level.
You would use these macros and they would appear or not depending on the log level at the time of the compile. So they would be used like the following.
LOGGING_LEVEL_DEBUG("a Debug log.");
//.... some code
LOGGING_LEVEL_EMERG("a Emerge log.");
So the macros definitions would look something like the following:
#define LOG_EMERG 0
#define LOG_ALERT 1
#define LOG_CRIT 2
#define LOG_ERROR 3
#define LOG_WARN 4
#define LOG_NOTICE 5
#define LOG_INFO 6
#define LOG_DEBUG 7
#define LOG_NONE 8
/* set the global logging level here */
#define LOG_LEVEL LOG_INFO
void print_timestamp(void);
#if LOG_LEVEL >= LOG_DEBUG
#define _LOG_PREAMBLE \
fprintf(stdout, "%s:%d ", __FILE__, __LINE__);
#else
#define _LOG_PREAMBLE \
print_timestamp();
#endif
#if LOG_LEVEL >= LOG_EMERG
#define LOGGING_LEVEL_EMERG(...) \
{ \
_LOG_PREAMBLE \
fprintf(stdout, "EMERG "); \
fprintf(stdout, ##__VA_ARGS__); \
}
#else
#define LOGGING_LEVEL_EMERG(...)
#endif
#if LOG_LEVEL >= LOG_ALERT
#define LOGGING_LEVEL_ALERT(...) \
{ \
_LOG_PREAMBLE \
fprintf(stdout, "ALERT "); \
fprintf(stdout, ##__VA_ARGS__); \
}
#else
#define LOGGING_LEVEL_ALERT(...)
#endif
You could define the macros themselves conditionally:
#if LOG_LEVEL > 0
# define LOG(...) printf(__VA_ARGS__)
#else
# define LOG(...)
#endif
#if LOG_LEVEL > 1
// and so forth

Resources