PDA

View Full Version : Problems compiling a .c file with GCC (worked great in FC6)



somafm
February 27th, 2007, 08:28 PM
Hello,
I WAS using FC6, installed GCC, and compiled a .c file. It worked fine and I was able to run my application in console. The command I used in terminal was: gcc -o final gsmsdisc.c.

After installing ubuntu 6.10 and updating everything, I tried to compile the same .C file the same way, and I get a TON of errors. Could it have to do with the GCC versions, or should I be compiling it a different way? I'm a big noob when it comes to C programming, and this isn't my code. I just want to get it compiled so I can use it in Ubuntu. It's a single .C file, and I usually run it in console like so: ./final argumentshere

The gcc version I have running on FC6 is: 4.1.1-51.fc6.i386.
The gcc version on Ubuntu 6.10 is the default that came with it, and it's updated to the fullest.

I have FC6 on my laptop and Ubuntu6.10 on my desktop, so I can still access both if you guys need me to. Thanks for the help in advance :)

Here is the full .C code:

/*

by Luigi Auriemma

*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include "winerr.h"

#define close closesocket
#define sleep Sleep
#define ONESEC 1000
#else
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/param.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>

#define ONESEC 1
#endif



#define VER "0.1.2"
#define MSHOST "master.gamespy.com"
#define MSPORT 27900
#define BUFFSZ 2048
#define DISC "\\heartbeat\\%hu" \
"\\gamename\\%s" \
"\\statechanged\\2"
#define IPSZ sizeof(struct iphdr)
#define UDPSZ sizeof(struct udphdr)
#define PSEUDOSZ sizeof(struct pseudohdr)
#define PCKSIZE (IPSZ + UDPSZ + pcklen)
#define PSSIZE (PSEUDOSZ + UDPSZ + pcklen)



struct iphdr {
uint8_t ihl_ver;
uint8_t tos;
uint16_t tot_len;
uint16_t id;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t check;
uint32_t saddr;
uint32_t daddr;
};

struct udphdr {
uint16_t source;
uint16_t dest;
uint16_t len;
uint16_t check;
};

struct pseudohdr {
uint32_t saddr;
uint32_t daddr;
uint8_t zero;
uint8_t protocol;
uint16_t len;
};



uint16_t in_cksum(void *data, int len);
uint32_t resolv(char *host);
void std_err(void);



int main(int argc, char *argv[]) {
struct sockaddr_in peer;
struct iphdr *ip;
struct udphdr *udp;
struct pseudohdr *pseudo;
uint32_t ip_src,
ip_dst;
int sd,
i,
pcklen,
on = 1,
timeout = 0;
uint16_t port_src,
port_dst;
uint8_t buff[BUFFSZ],
*data;

#ifdef WIN32
WSADATA wsadata;
WSAStartup(MAKEWORD(1,0), &wsadata);
#endif

setbuf(stdout, NULL);

fputs("\n"
"GS master server disconnector "VER"\n"
"by Luigi Auriemma\n"
"e-mail: aluigi@autistici.org\n"
"web: aluigi.org\n"
"\n", stdout);

if(argc < 4) {
printf("\n"
"Usage: %s <gamename> <server> <port> [timeout]\n"
"\n"
" timeout is the amount of time in seconds before resending the packet\n"
" by default is sent only one packet and then exits\n"
" for more informations: http://aluigi.org/papers/msdisc.txt\n"
"\n", argv[0]);
exit(1);
}

#ifndef WIN32
if(getuid()) {
printf("\nError: you must be root to use raw sockets, I try to continue\n\n");
}
#endif


if(argc > 4) timeout = atoi(argv[4]);

ip = (struct iphdr *)buff;
udp = (struct udphdr *)(buff + IPSZ);
data = (uint8_t *)(buff + IPSZ + UDPSZ);
pseudo = (struct pseudohdr *)(buff + IPSZ - PSEUDOSZ);

printf("- resolv hostnames/IPs:\n");
ip_src = resolv(argv[2]);
ip_dst = resolv(MSHOST);
port_src = htons(atoi(argv[3]));
port_dst = htons(MSPORT);

pcklen = sprintf(
data,
DISC,
ntohs(port_src),
argv[1]);

pseudo->saddr = ip_src;
pseudo->daddr = ip_dst;
pseudo->zero = 0;
pseudo->protocol = IPPROTO_UDP;
pseudo->len = htons(UDPSZ + pcklen);

udp->source = port_src;
udp->dest = port_dst;
udp->check = 0;
udp->len = pseudo->len;
udp->check = htons(in_cksum(pseudo, PSSIZE));

ip->ihl_ver = (4 << 4) | (sizeof(struct iphdr) >> 2);
ip->tos = 0x10;
ip->tot_len = htons(PCKSIZE);
ip->id = htons(1);
ip->frag_off = htons(0);
ip->ttl = 128;
ip->protocol = IPPROTO_UDP;
ip->check = 0;
ip->saddr = ip_src;
ip->daddr = ip_dst;
ip->check = htons(in_cksum(ip, IPSZ));

peer.sin_addr.s_addr = ip_dst;
peer.sin_port = port_dst;
peer.sin_family = AF_INET;

printf("- from %15s : %hu\n",
inet_ntoa(*(struct in_addr *)&(ip->saddr)), ntohs(udp->source));
printf("- to %15s : %hu\n",
inet_ntoa(peer.sin_addr), ntohs(udp->dest));

sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if(sd < 0) std_err();
if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, (void *)&on, sizeof(on))
< 0) std_err();

for(;;) {
if(sendto(sd, buff, PCKSIZE, 0, (struct sockaddr *)&peer, sizeof(peer))
< 0) std_err();
fputc('.', stdout);

if(!timeout) break;
for(i = timeout; i; i--) {
printf("%5d\b\b\b\b\b", i);
sleep(ONESEC);
}
}
close(sd);

printf("\nDone\n");
return(0);
}



uint16_t in_cksum(void *data, int len) {
uint32_t sum = 0;
int i = len >> 1,
endian = 1; // big endian
uint16_t crc,
*p = (uint16_t *)data;

if(*(char *)&endian) endian = 0;
while(i--) sum += *p++;
if(len & 1) sum += *p & (endian ? 0xff00 : 0xff);
crc = sum = (sum >> 16) + (sum & 0xffff);
if(sum >>= 16) crc += sum;
if(!endian) crc = (crc >> 8) | (crc << 8);
return(~crc);
}



uint32_t resolv(char *host) {
struct hostent *hp;
uint32_t host_ip;

host_ip = inet_addr(host);
if(host_ip == INADDR_NONE) {
hp = gethostbyname(host);
if(!hp) {
printf("\nError: Unable to resolv hostname (%s)\n", host);
exit(1);
} else host_ip = *(uint32_t *)hp->h_addr;
}
return(host_ip);
}



#ifndef WIN32
void std_err(void) {
perror("\nError");
exit(1);
}
#endif



Here are the errors:

psc@backuplinux:~/Desktop$ gcc -o final gsmsdisc.c
gsmsdisc.c:7:19: error: stdio.h: No such file or directory
gsmsdisc.c:8:20: error: stdlib.h: No such file or directory
gsmsdisc.c:9:20: error: stdint.h: No such file or directory
gsmsdisc.c:20:24: error: unistd.h: No such file or directory
gsmsdisc.c:21:28: error: sys/socket.h: No such file or directory
gsmsdisc.c:22:27: error: sys/types.h: No such file or directory
gsmsdisc.c:23:27: error: sys/param.h: No such file or directory
gsmsdisc.c:24:27: error: arpa/inet.h: No such file or directory
gsmsdisc.c:25:28: error: netinet/in.h: No such file or directory
gsmsdisc.c:26:23: error: netdb.h: No such file or directory
gsmsdisc.c:49: error: expected specifier-qualifier-list before ‘uint8_t’
gsmsdisc.c:62: error: expected specifier-qualifier-list before ‘uint16_t’
gsmsdisc.c:69: error: expected specifier-qualifier-list before ‘uint32_t’
gsmsdisc.c:78: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘in_cksum’
gsmsdisc.c:79: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘resolv’
gsmsdisc.c: In function ‘main’:
gsmsdisc.c:85: error: storage size of ‘peer’ isn’t known
gsmsdisc.c:89: error: ‘uint32_t’ undeclared (first use in this function)
gsmsdisc.c:89: error: (Each undeclared identifier is reported only once
gsmsdisc.c:89: error: for each function it appears in.)
gsmsdisc.c:89: error: expected ‘;’ before ‘ip_src’
gsmsdisc.c:96: error: ‘uint16_t’ undeclared (first use in this function)
gsmsdisc.c:96: error: expected ‘;’ before ‘port_src’
gsmsdisc.c:98: error: ‘uint8_t’ undeclared (first use in this function)
gsmsdisc.c:98: error: expected ‘;’ before ‘buff’
gsmsdisc.c:106: error: ‘stdout’ undeclared (first use in this function)
gsmsdisc.c:106: error: ‘NULL’ undeclared (first use in this function)
gsmsdisc.c:116: warning: incompatible implicit declaration of built-in function ‘printf’
gsmsdisc.c:123: warning: incompatible implicit declaration of built-in function ‘exit’
gsmsdisc.c:128: warning: incompatible implicit declaration of built-in function ‘printf’
gsmsdisc.c:135: error: ‘buff’ undeclared (first use in this function)
gsmsdisc.c:137: error: ‘data’ undeclared (first use in this function)
gsmsdisc.c:137: error: expected expression before ‘)’ token
gsmsdisc.c:140: warning: incompatible implicit declaration of built-in function ‘printf’
gsmsdisc.c:141: error: ‘ip_src’ undeclared (first use in this function)
gsmsdisc.c:142: error: ‘ip_dst’ undeclared (first use in this function)
gsmsdisc.c:143: error: ‘port_src’ undeclared (first use in this function)
gsmsdisc.c:144: error: ‘port_dst’ undeclared (first use in this function)
gsmsdisc.c:146: warning: incompatible implicit declaration of built-in function ‘sprintf’
gsmsdisc.c:152: error: ‘struct pseudohdr’ has no member named ‘saddr’
gsmsdisc.c:153: error: ‘struct pseudohdr’ has no member named ‘daddr’
gsmsdisc.c:154: error: ‘struct pseudohdr’ has no member named ‘zero’
gsmsdisc.c:155: error: ‘struct pseudohdr’ has no member named ‘protocol’
gsmsdisc.c:155: error: ‘IPPROTO_UDP’ undeclared (first use in this function)
gsmsdisc.c:156: error: ‘struct pseudohdr’ has no member named ‘len’
gsmsdisc.c:158: error: ‘struct udphdr’ has no member named ‘source’
gsmsdisc.c:159: error: ‘struct udphdr’ has no member named ‘dest’
gsmsdisc.c:160: error: ‘struct udphdr’ has no member named ‘check’
gsmsdisc.c:161: error: ‘struct udphdr’ has no member named ‘len’
gsmsdisc.c:161: error: ‘struct pseudohdr’ has no member named ‘len’
gsmsdisc.c:162: error: ‘struct udphdr’ has no member named ‘check’
gsmsdisc.c:164: error: ‘struct iphdr’ has no member named ‘ihl_ver’
gsmsdisc.c:165: error: ‘struct iphdr’ has no member named ‘tos’
gsmsdisc.c:166: error: ‘struct iphdr’ has no member named ‘tot_len’
gsmsdisc.c:167: error: ‘struct iphdr’ has no member named ‘id’
gsmsdisc.c:168: error: ‘struct iphdr’ has no member named ‘frag_off’
gsmsdisc.c:169: error: ‘struct iphdr’ has no member named ‘ttl’
gsmsdisc.c:170: error: ‘struct iphdr’ has no member named ‘protocol’
gsmsdisc.c:171: error: ‘struct iphdr’ has no member named ‘check’
gsmsdisc.c:172: error: ‘struct iphdr’ has no member named ‘saddr’
gsmsdisc.c:173: error: ‘struct iphdr’ has no member named ‘daddr’
gsmsdisc.c:174: error: ‘struct iphdr’ has no member named ‘check’
gsmsdisc.c:178: error: ‘AF_INET’ undeclared (first use in this function)
gsmsdisc.c:181: error: ‘struct iphdr’ has no member named ‘saddr’
gsmsdisc.c:181: error: ‘struct udphdr’ has no member named ‘source’
gsmsdisc.c:183: error: ‘struct udphdr’ has no member named ‘dest’
gsmsdisc.c:185: error: ‘SOCK_RAW’ undeclared (first use in this function)
gsmsdisc.c:185: error: ‘IPPROTO_RAW’ undeclared (first use in this function)
gsmsdisc.c:187: error: ‘IPPROTO_IP’ undeclared (first use in this function)
gsmsdisc.c:187: error: ‘IP_HDRINCL’ undeclared (first use in this function)
gsmsdisc.c: At top level:
gsmsdisc.c:209: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘in_cksum’
gsmsdisc.c:227: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘resolv’
gsmsdisc.c: In function ‘std_err’:
gsmsdisc.c:247: warning: incompatible implicit declaration of built-in function ‘exit’

lnostdal
February 27th, 2007, 08:32 PM
in short:
sudo aptitude install build-essential

for more details see: http://nostdal.org/~lars/writings/

edit: minor correction; thanks WW :)

somafm
February 27th, 2007, 08:41 PM
Wahoo thanks! Got it to work now.

I ended up using:

sudo aptitude install build-essential manpages manpages-dev manpages-posix-dev
.. the one you posted didn't want to install anything :confused:

Working fine though, thanks again!

WW
February 27th, 2007, 08:42 PM
Stage whisper: Inostdal... no s at the end of build-essential