Compiling code with TCP_REPAIR socket option on Ubuntu [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Following this article, I am trying to get a C program to compile that uses TPC_REPAIR.
http://oroboro.com/dealing-with-network-port-abuse-in-sockets-in-c/
TCP_REPAIR is defined in
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h
Including this header conflicts with the ones required for other socket APIs.
Here's my code
#include <stdio.h>
#include <unistd.h>
#include <linux/socket.h>
#include <uapi/linux/in.h>
#include <netinet/tcp.h>
#include "/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h"
int main(int argc, char* argv[])
{
int cfd;
struct sockaddr_in addr;
char ch='r';
cfd=socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("10.160.102.40");
addr.sin_port=htons(9988);
if(connect(cfd, (struct sockaddr *)&addr,
sizeof(addr))<0) {
perror("connect error");
return -1;
}
if(write(cfd, &ch, 1)<0) perror("write");
if(read(cfd, &ch, 1)<0) perror("read");
printf("\nReply from Server: %c\n",ch);
//close(cfd);
int aux = 1;
if(setsockopt(cfd, SOL_TCP, TCP_REPAIR, &aux, sizeof( aux )) < 0 )
{
perror( "could not turn on repair mode" );
}
return 0;
}
And here's gcc output
In file included from /usr/include/netinet/tcp.h:56:0,
from new_client.c:5:
/usr/include/x86_64-linux-gnu/sys/types.h:45:18: error: conflicting types for ‘loff_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:45:26: note: previous declaration of ‘loff_t’ was here
/usr/include/x86_64-linux-gnu/sys/types.h:61:17: error: conflicting types for ‘dev_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:15:25: note: previous declaration of ‘dev_t’ was here
/usr/include/x86_64-linux-gnu/sys/types.h:76:19: error: conflicting types for ‘nlink_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:19:17: note: previous declaration of ‘nlink_t’ was here
In file included from /usr/include/x86_64-linux-gnu/sys/types.h:133:0,
from /usr/include/netinet/tcp.h:56,
from new_client.c:5:
/usr/include/time.h:104:19: error: conflicting types for ‘timer_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:25:26: note: previous declaration of ‘timer_t’ was here
In file included from /usr/include/netinet/tcp.h:56:0,
from new_client.c:5:
/usr/include/x86_64-linux-gnu/sys/types.h:198:1: error: conflicting types for ‘int64_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:113:17: note: previous declaration of ‘int64_t’ was here
/usr/include/x86_64-linux-gnu/sys/types.h:204:1: error: conflicting types for ‘u_int64_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:112:17: note: previous declaration of ‘u_int64_t’ was here
In file included from /usr/include/x86_64-linux-gnu/sys/types.h:220:0,
from /usr/include/netinet/tcp.h:56,
from new_client.c:5:
/usr/include/x86_64-linux-gnu/sys/select.h:76:5: error: conflicting types for ‘fd_set’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:14:26: note: previous declaration of ‘fd_set’ was here
In file included from /usr/include/netinet/tcp.h:56:0,
from new_client.c:5:
/usr/include/x86_64-linux-gnu/sys/types.h:236:20: error: conflicting types for ‘blkcnt_t’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/types.h:134:23: note: previous declaration of ‘blkcnt_t’ was here
In file included from /usr/include/x86_64-linux-gnu/sys/uio.h:29:0,
from /usr/include/x86_64-linux-gnu/sys/socket.h:28,
from /usr/include/netinet/tcp.h:57,
from new_client.c:5:
/usr/include/x86_64-linux-gnu/bits/uio.h:44:8: error: redefinition of ‘struct iovec’
/usr/src/linux-headers-3.8.0-29-generic/include/uapi/linux/uio.h:16:8: note: originally defined here
In file included from /usr/include/x86_64-linux-gnu/sys/socket.h:40:0,
from /usr/include/netinet/tcp.h:57,
from new_client.c:5:
/usr/include/x86_64-linux-gnu/bits/socket.h:180:8: error: redefinition of ‘struct sockaddr’
/usr/src/linux-headers-3.8.0-29-generic/include/uapi/linux/socket.h:19:27: note: originally defined here
/usr/include/x86_64-linux-gnu/bits/socket.h:193:8: error: redefinition of ‘struct __kernel_sockaddr_storage’
/usr/src/linux-headers-3.8.0-29-generic/include/uapi/linux/socket.h:13:8: note: originally defined here
/usr/include/x86_64-linux-gnu/bits/socket.h:204:5: error: expected identifier before numeric constant
/usr/include/x86_64-linux-gnu/bits/socket.h:253:8: error: redefinition of ‘struct msghdr’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/socket.h:47:8: note: originally defined here
/usr/include/x86_64-linux-gnu/bits/socket.h:280:8: error: redefinition of ‘struct cmsghdr’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/socket.h:69:8: note: originally defined here
/usr/include/x86_64-linux-gnu/bits/socket.h:310:24: error: conflicting types for ‘__cmsg_nxthdr’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/socket.h:111:32: note: previous definition of ‘__cmsg_nxthdr’ was here
/usr/include/x86_64-linux-gnu/bits/socket.h:339:5: error: expected identifier before numeric constant
In file included from /usr/include/x86_64-linux-gnu/sys/socket.h:40:0,
from /usr/include/netinet/tcp.h:57,
from new_client.c:5:
/usr/include/x86_64-linux-gnu/bits/socket.h:417:8: error: redefinition of ‘struct linger’
/usr/src/linux-headers-3.8.0-29-generic/include/linux/socket.h:34:8: note: originally defined here
In file included from new_client.c:6:0:
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:24:8: error: redefinition of ‘struct tcphdr’
/usr/include/netinet/tcp.h:92:8: note: originally defined here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:135:6: error: nested redefinition of ‘enum tcp_ca_state’
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:135:6: error: redeclaration of ‘enum tcp_ca_state’
/usr/include/netinet/tcp.h:179:6: note: originally defined here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:136:2: error: redeclaration of enumerator ‘TCP_CA_Open’
/usr/include/netinet/tcp.h:181:3: note: previous definition of ‘TCP_CA_Open’ was here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:138:2: error: redeclaration of enumerator ‘TCP_CA_Disorder’
/usr/include/netinet/tcp.h:182:3: note: previous definition of ‘TCP_CA_Disorder’ was here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:140:2: error: redeclaration of enumerator ‘TCP_CA_CWR’
/usr/include/netinet/tcp.h:183:3: note: previous definition of ‘TCP_CA_CWR’ was here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:142:2: error: redeclaration of enumerator ‘TCP_CA_Recovery’
/usr/include/netinet/tcp.h:184:3: note: previous definition of ‘TCP_CA_Recovery’ was here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:144:2: error: redeclaration of enumerator ‘TCP_CA_Loss’
/usr/include/netinet/tcp.h:185:3: note: previous definition of ‘TCP_CA_Loss’ was here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:148:8: error: redefinition of ‘struct tcp_info’
/usr/include/netinet/tcp.h:188:8: note: originally defined here
/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h:193:8: error: redefinition of ‘struct tcp_md5sig’
/usr/include/netinet/tcp.h:235:8: note: originally defined here

This is really not useful
#include "/usr/src/linux-headers-3.8.0-29/include/uapi/linux/tcp.h"
Because either the kernel supports it or it does not. You can check for it like this:
find /usr/include -type f -exec grep TCP_REPAIR {} /dev/null \;
/usr/include/linux/tcp.h:#define TCP_REPAIR 19 /* TCP sock is under repair right now */
/usr/include/linux/tcp.h:#define TCP_REPAIR_QUEUE 20
/usr/include/linux/tcp.h:#define TCP_REPAIR_OPTIONS 22
if it's there, great, otherwise you'll have to use the workaround.
Here's an Ubuntu that has it
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 12.10
Release: 12.10
Codename: quantal
It looks like SOL_TCP is only defined in /usr/include/netinet/tcp.h
find /usr/include -type f -exec grep SOL_TCP {} /dev/null \;
/usr/include/netinet/tcp.h:# define SOL_TCP 6 /* TCP level */
To overcome the unfortunate absence of SOL_TCP in /usr/include/linux/tcp.h. You can try to simply copy it from netinet/tcp.h, it's just a number after all.
#ifndef SOL_TCP
#define SOL_TCP 6
#endif
However, the man page of setsockopt offers an even better approach:
To manipulate options at any other level the protocol number of the appropriate protocol controlling the option is supplied. For example, to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP; see getprotoent(3).

Related

error: conflicting types for in "function" in c

In file included from
netscaler/libccauth/ccauth/platform.h:28,
from netscaler/libccauth/ccauth.h:14,
from sys/ccauth/Just_Check.h:1,
from sys/nwaas/ns_nws.c:79:
include/unistd.h:357: error: conflicting types for 'pause'
sys/sys/systm.h:383: error: previous declaration of 'pause' was
here
I am getting these kind of conflict error, How should I approach to resolve such conflict ?
int pause(void); — defined in include/unistd.h
int pause(const char *wmesg, int timo); — defined in
sys/sys/systm.h

Using nano_usb_programmer with Sensinodes

I am trying to use some old devices called sensinodes . There are a couple of pieces of software needed for this, contiki, sdcc and the some nano_usb_programmer
I'm working from these instruction and I can't seem to make the nano_usb_programmer on ubuntu 16.04 LTS as this pretty old now and things have moved on.
https://github.com/avian2/contiki
When making the nano element I get the error *Edited
In file included from main.c:17:0:
opts.h:25:2: error: redefinition of typedef ‘mode_t’ with different type
}mode_t;
^
In file included from /usr/include/stdlib.h:314:0,
from main.c:10:
/usr/include/x86_64-linux-gnu/sys/types.h:70:18: note: previous declaration of ‘mode_t’ was here
typedef __mode_t mode_t;
^
main.c: In function ‘main’:
main.c:57:6: warning: implicit declaration of function ‘opts_parse’ [-Wimplicit-function-declaration]
if (opts_parse(argc, argv) < 0)
^
main.c:77:4: warning: implicit declaration of function ‘prog_scan’ [-Wimplicit-function-declaration]
prog_scan();
^
main.c:178:7: warning: implicit declaration of function ‘hexfile_out’ [-Wimplicit-function-declaration]
hexfile_out(line_data, 0x04, 0, ptr, 2);
^
main.c:220:15: warning: implicit declaration of function ‘hexfile_build_tables’ [-Wimplicit-function-declaration]
if((rval = hexfile_build_tables(opts.filename, page_buffer, page_table)) == -1)
^
main.c:231:4: warning: implicit declaration of function ‘hexfile_program’ [-Wimplicit-function-declaration]
hexfile_program(page_buffer, page_table);
^
Makefile:25: recipe for target 'main.o' failed
make: *** [main.o] Error 1
It is pointing to line 25 the following file, my c knowledge just isn't any where near good enough to fix this. Any help would be great.
/*
* opts.h
*
* Created on: Dec 21, 2009
* Author: martti
*/
#ifndef OPTS_H_
#define OPTS_H_
#include "cdi.h"
typedef enum
{
USAGE,
VERSION,
SCAN,
WRITE,
WRITE_FAST,
READ,
WRITE_MAC,
READ_MAC,
LOCK,
ERASE
}mode_t;
typedef struct opts_t
{
int port;
chip_t chip;
mode_t mode;
uint8_t write_mac[8];
ramcode_t code_in_ram;
char *filename;
}opts_t;
extern opts_t opts;
#endif /* OPTS_H_ */

Enumerations in C head files shared across multiple files

I want to define an enumeration type ONCE and have that type be shared across all the other files when I include the file, however I keep getting the following errors:
$ gcc -std=c99 main.c invoc.h invoc.c
main.c: In function ‘main’:
main.c:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘pr_alg’
main.c:12: error: ‘pr_alg’ undeclared (first use in this function)
main.c:12: error: (Each undeclared identifier is reported only once
main.c:12: error: for each function it appears in.)
main.c:13: error: ‘FIFO’ undeclared (first use in this function)
invoc.c:7: error: expected ‘)’ before ‘myalg’
The code is as follows:
invoc.h:
#define INVOC_H
#ifndef INVOC_H
typedef enum {FIFO, SECOND_CHANCE, RANDOM, NRU, CLOCK, AGING} alg_t;
void func1(alg_t myalg);
#endif
invoc.c:
#include "invoc.h"
void func1(alg_t myalg) {
myalg = NRU;
}
main.c:
#include "invoc.h"
int main(int argc, char **argv) {
extern alg_t pr_alg;
pr_alg = FIFO;
printf("PR_ALG: %d\n", pr_alg);
return 0;
}
Is there any way that I can define an enumeration in a .h file, and include it in all other files so that I can both create different variables of that type and pass it to functions?
You have an error in your invoc.h file:
#define INVOC_H
#ifndef INVOC_H
...
#endif
You first define a macro INVOC_H, then check if it does not exists (it does), so the code inside is removed by the preprocessor and not parsed by the compiler.
It should be:
#ifndef INVOC_H
#define INVOC_H
...
#endif
After this change your code will work fine.
You don't compile .h files, only .c files. That's why we put all definitions in .c files, and only declarations in .h files. To compile, just do:
gcc -std=c99 mmu.c invoc.c
You declare pr_alg in main() as extern variable. If the line you provided is the whole compilation line, the compile will issue linker error as variable pr_alg is nowhere defined. Remove extern or define variable pr_alg with global storage duration in one of the .c files.

X509_NAME macro in C won't compile

This is a silly compilation error, but for the life of me I can't find out what's wrong. I have spent hours on it, but I have made no progress at all. And I certainly don't understand enough about OpenSSL to understand what an X509_NAME is.
I'm compiling a small C file whose function it is to print out an error message if anyone calls one of the 30 or so functions in the file, to indicate that the 30 SSL functions are not supported by the C program. Each function is about 3 lines long (see below). The calling interface is copied from the OpenSSL openssl/X509.h file for win32. (I'm running Windows10 x64, but compiling with VStudio 2017 command line set up for 32bits).
Here is the original interface from the openssl/x509.h include file:
int X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len);
Since the original x509.h include file says inside that "the definition for X509_NAME on Win32 is located in wincrypt.h," I copied the definition for the X509_NAME macro directly into my source file as shown below.
The code for my simple error message function follows. I just copied the interface definition from the openssl/x509.h file and made it into a function:
#define X509_NAME ((LPCSTR) 7) /* copied from wincrypt.h */
int X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len) {
print_error (E_NO_SSL_SUPPORT);
return (-1);
}
When I try to compile the code with the VS 2017 command line compiler for 32bits, I get these error messages. Line 236 contains the troublesome code line int X509_NAME_....
cl /c /Od /nologo /MT /I. "-I..\s" ..\s\stubssl.c
stubssl.c
..\s\stubssl.c(236): error C2143: syntax error: missing ')' before '('
..\s\stubssl.c(236): error C2091: function returns function
..\s\stubssl.c(236): error C2059: syntax error: ')'
..\s\stubssl.c(236): error C2143: syntax error: missing ')' before 'constant'
..\s\stubssl.c(236): error C2143: syntax error: missing '{' before 'constant'
..\s\stubssl.c(236): error C2059: syntax error: 'constant'
..\s\stubssl.c(242): error C2059: syntax error: '<parameter-list>'
c:\stubssl\winxp\make.exe: *** [stubssl.obj] Error 2
I get the same kind of error with another function that is a bit simpler (but that also uses the X509_NAME macro). It starts on line 242 (see the error messages above).
X509_NAME *X509_get_subject_name(X509 *a) {
hio_oerror (E_NO_SSL_SUPPORT);
return (NULL);
}
Does anyone have any idea how I might try to resolve this problem? It seems so simple, yet it has been very elusive for me to solve. I can't imagine how I can be missing ')' before '(' in those lines.
EDIT: My include files in stubssl.c:
#define WIN32_LEAN_AND_MEAN
#include "openssl/ssl.h"
#include "openssl/x509.h"
#include "openssl/bio.h"
#include "openssl/pem.h"
In openSSL, X509_NAME is a type, but <wincrypt.h> defines it as a value (((LPCSTR) 7)). Don't define it yourself, but use the openSSL header, don't include <wincrypt.h> and #define WIN32_LEAN_AND_MEAN to avoid it being included through other windows headers.

C compile error

What does the following error mean when compiling.
Tilemap.h:21: error: conflicting types for ‘ThreeDWorld’
Tilemap.h:21: error: previous declaration of ‘ThreeDWorld’ was here
Tilemap.h:29: error: conflicting types for ‘CGPoint’
Tilemap.h:29: error: previous declaration of ‘CGPoint’ was here
Tilemap.h:31: error: conflicting types for ‘tileForCoordinates’
Tilemap.h:31: error: previous declaration of ‘tileForCoordinates’ was here
Why is it giving an error for what was there?My source file has one instance of it as such
typedef struct
{
int xPosition;
int yPosition;
}
CGPoint;
Are you including the header file from more than one place? Use a guard in the header file, if so.
For example, in Tilemap.h:
#ifndef TILEMAP_H
#define TILEMAP_H
// header file contents
#endif /* TILEMAP_H */
Stick some inclusion guards on your headers.
Your type definition is appearing more than once in your compilation unit.
You included the header file twice.
In my own code, I wrapped all header files with
#ifndef HEADER_FILE_NAME
#define HEADER_FILE_NAME
#endif
to avoid such errors.

Resources