LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-07-2012, 09:26 AM   #1
joyce092130
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Rep: Reputation: Disabled
error: dereferencing pointer to incomplete type


hi everyone!
im working on my client/server chat program and i had "error: dereferencing pointer to incomplete type" during the compilation of my clientchat.c

Here is the code:

#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

int main(int argc,char *argv[])
{
struct sockaddr_in clientaddr;
pid_t pid;
int clientfd,sendbytes;
struct hostent *host;
char *buf,*buf_r;

if(argc < 4)
{
printf("usage:\n");
printf("%s host port name\n",argv[0]);
exit(1);
}

host = gethostbyname(argv[1]);
if((clientfd = socket (AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket\n");
exit(1);
}
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = htons((uint16_t)atoi(argv[2]));
clientaddr.sin_addr = *((struct in_addrr *) host->h_addr);
bzero(&(clientaddr.sin_zero),0);
if(connect(clientfd,(struct sockaddr *)&clientaddr,sizeof(struct sockaddr)) == -1)
{
perror("connect\n");
exit(1);
}

buf=(char *)malloc(120);
memset(buf,0,120);

buf_r=(char *)malloc(100);

if( recv(clientfd,buf,100,0) == -1)
{
perror("recv: ");
exit(1);
}
printf("\n%s\n",buf);

pid = fork();

while(1)
{
if(pid > 0)
{
//get_cur_time(time_str);

strcpy(buf,argv[3]);
strcat(buf,":");
memset(buf_r,0,100);
//gets(buf_r);
fgets(buf_r,100,stdin);
strncat(buf,buf_r,strlen(buf_r)-1);
//strcat(buf,time_str);
//printf("---%s\n",buf);
if((sendbytes = send(clientfd,buf,strlen(buf),0)) == -1)
{
perror("send\n");
exit(1);
}
}
else if(pid == 0)
{
memset(buf,0,1000);
if(recv(clientfd,buf,100,0) <=0)
{
perror("recv:");
close(clientfd);
raise(SIGSTOP);
exit(1);
}
printf("%s\n",buf);
}
else
perror("fork");
}
close(clientfd);
return 0;
}

Please help me with this.
Thank u much.
 
Old 07-07-2012, 09:35 AM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,075

Rep: Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109
Quote:
Originally Posted by joyce092130 View Post
clientaddr.sin_addr = *((struct in_addrr *) host->h_addr);
Is the extra r on that line a typo?

Without that r your code compiles.
 
1 members found this post helpful.
Old 07-07-2012, 09:47 AM   #3
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian jessie/sid; OpenSuSE; Fedora
Posts: 1,591

Rep: Reputation: 162Reputation: 162
One of the header prototypes is probably not coded exactly the way the compiler wants it. I think there is a "no deference" switch for gcc and g++ that you could try. I'm not able to locate the exact code, because I need more context from the compiler, and preferably some debug traces.

I have a hunch uint16 type is a culprit. Try using that header from the kernel headers instead of the libc6 one.
 
Old 07-07-2012, 10:48 AM   #4
joyce092130
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
thank u so much. no more errors on both my clientchat.c and serverchat.c but why is it that there is still no exe file. it is supposed to have a chat program between the client and the server.

when i tried

./clientchat acer-d56ca9a39d 350

the result is -bash: ./clientchat: No such file or directory

same result i got for serverchat
 
Old 07-07-2012, 10:59 AM   #5
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,635

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
how did you compile your code? What was the command line?
 
Old 07-07-2012, 11:02 AM   #6
joyce092130
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
using cygwin terminal i compiled my serverchat as:
gcc -Wall -o server serverchat.c

and my clientchat as
gcc -Wall -o client clientchat.c

there were no errors displayed alr
 
Old 07-07-2012, 11:07 AM   #7
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,635

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
so your server program is server (instead or serverchat), your client program is client (instead of clientchat).
The name of the created program is the string after the -o option. see the man page of gcc...
 
Old 07-07-2012, 11:10 AM   #8
joyce092130
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
where can i check on that?
 
Old 07-07-2012, 01:01 PM   #9
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,635

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
man g++
Quote:
-o file
Write output to file. This is the same as specifying file as the second non-option argument to cpp. gcc has a different interpretation of a second non-option argument, so you must use -o to specify the output file
 
Old 07-07-2012, 08:41 PM   #10
joyce092130
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
so what shall i do to make my program an exe file. so that i can have the expected chat between the server and the client.
 
Old 07-07-2012, 08:44 PM   #11
joyce092130
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
How can i make my codes in hello world program into a single chat program where the client can send messages to the server and the server can send replies to the client?

here are my codes for the hello world program:

server.c


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>

int _GetHostName(char *buffer, int length);

const char MESSAGE[]="Hello, World!\n";
const int BACK_LOG=5;

int main(int argc, char *argv[]){
int serverSocket=0,on=0,port=0,status=0,childPid=0;
struct hostent *hostPtr=NULL;
char hostname[80]="";
struct sockaddr_in serverName={0};

if(2!=argc){
fprintf(stderr,"Usage: %s <port>\n",argv[0]);
exit(1);
}
port=atoi(argv[1]);
serverSocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(-1==serverSocket){
perror("socket()");
exit(1);
}

on=1;
status=setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,
(const char*)&on,sizeof(on));
if(-1==status){
perror("setsockopt(...,SO_REUSEADDR,...)");
}
{
struct linger linger={0};
linger.l_onoff=1;
linger.l_linger=30;
status=setsockopt(serverSocket,SOL_SOCKET,SO_LINGER,
(const char*)&linger,sizeof(linger));
if(-1==status){
perror("setsockopt(...,SO_LINGER,...)");
}
}

status=_GetHostName(hostname,sizeof(hostname));
if(-1==status){
perror("_GetHostName()");
exit(1);
}

hostPtr=gethostbyname(hostname);
if(NULL==hostPtr){
perror("gethostbyname()");
exit(1);
}

(void)memset(&serverName,0,sizeof(serverName));
(void)memcpy(&serverName.sin_addr,hostPtr->h_addr,hostPtr->h_length);

serverName.sin_family=AF_INET;
serverName.sin_port=htons(port);

status=bind(serverSocket,(struct sockaddr*)&serverName,sizeof(serverName));
if(-1==status){
perror("bind()");
exit(1);
}

status=listen(serverSocket,BACK_LOG);
if(-1==status){
perror("listen()");
exit(1);
}

for(;{
struct sockaddr_in clientName={0};
int slaveSocket, clientLength=sizeof(clientName);

(void)memset(&clientName,0,sizeof(clientName));

slaveSocket=accept(serverSocket,
(struct sockaddr*)&clientName,&clientLength);
if(-1==slaveSocket){
perror("accept()");
exit(1);
}

childPid=fork();

switch(childPid){

case -1:/*ERROR */
perror("fork()");
exit(1);
case 0 :/*child process */
close(serverSocket);
if(-1==getpeername(slaveSocket,
(struct sockaddr*)&clientName,&clientLength)){
perror("getpeername()");
}else{
printf("Connection request from %s\n",
inet_ntoa(clientName.sin_addr));
}

write(slaveSocket,MESSAGE,strlen(MESSAGE));
close(slaveSocket);
exit(0);
default:/*parent process */
close(slaveSocket);
}
}
return 0;
}

int _GetHostName(char *buffer,int length){
struct utsname sysname={0};
int status=0;

status=uname(&sysname);
if(-1!=status){
strncpy(buffer,sysname.nodename,length);
}
return(status);
}

------
client.c


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int main(int argc,char *argv[]){
int clientSocket,remotePort,status=0;
struct hostent *hostPtr=NULL;
struct sockaddr_in serverName={0};
char buffer[256]="";
char *remoteHost=NULL;

if(3!=argc){
fprintf(stderr,"Usage: %s <serverHost> <serverPort>\n",argv[0]);
exit(1);
}
remoteHost=argv[1];
remotePort=atoi(argv[2]);
clientSocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(-1==clientSocket){
perror("socket()");
exit(1);
}

hostPtr=gethostbyname(remoteHost);
if(NULL==hostPtr){
hostPtr=gethostbyaddr(remoteHost,strlen(remoteHost),AF_INET);
if(NULL==hostPtr){
perror("Error resolving server address ");
exit(1);
}
}
serverName.sin_family=AF_INET;
serverName.sin_port=htons(remotePort);
(void)memcpy(&serverName.sin_addr,hostPtr->h_addr,hostPtr->h_length);
status=connect(clientSocket,(struct sockaddr*)&serverName,
sizeof(serverName));
if(-1==status){
perror("connect()");
exit(1);
}

while(0<(status=read(clientSocket,buffer,sizeof(buffer)-1)))
printf("%d: %s",status,buffer);
if(-1==status)perror("read()");
close(clientSocket);
return 0;
}

thank u!
 
Old 09-29-2012, 09:06 AM   #12
KernelJay
LQ Newbie
 
Registered: Aug 2012
Posts: 15

Rep: Reputation: Disabled
Hi Joyce,
Please be advised that your posted code sample contains a likely heap-based buffer overflow which an attacker might be able to leverage to exploit the system. Use of strcpy() should be limited to instances where the source buffer is not user-controlled or has already been bounds checked. strncpy(buf, argv[3], 120) would prevent this type of attack in the event that argv[3] does not null terminate within 120 bytes.

For more information on how this could compromise your system, please check out my blog regarding stack-based buffer overflows which are even more dangerous. (Heap based overflows can certainly still lead to code execution and crashes.)

VERT Vuln School: Stack Buffer Overflows 101

Part 1: Introducing the Bug
Part 2: Explaining the Stack
Part 3: Exploiting the Bug

Regards,
Craig
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] error: dereferencing pointer to incomplete type in C Himalay Programming 9 07-07-2012 08:35 AM
dereferencing pointer to incomplete type sweetymobil Programming 7 06-22-2009 07:46 AM
tai64nlocal.c:55: error: dereferencing pointer to incomplete type ExCIA Linux - General 1 03-31-2009 09:49 AM
error: dereferencing pointer to incomplete type ChullDouvre Programming 2 05-02-2007 12:16 AM
Error: dereferencing pointer to incomplete type cynthia_thomas Programming 1 05-01-2006 08:10 AM


All times are GMT -5. The time now is 10:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration