LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Closed Thread
  Search this Thread
Old 08-13-2008, 10:11 AM   #1
soltanihaji
LQ Newbie
 
Registered: Jul 2008
Posts: 24

Rep: Reputation: 15
Unhappy very very important for me! please help me!


Hi ,
I am reading a C source code and I faced with a line about socket in linux.
I know what socket is but I want to ask what task the line in below do?

getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s);

I imagine that above line reads things and leave in info.
but in internet I found that getsockopt only reads options but info is
a user structure and if this function read things for setting info , from where does read this information?
I am confused. please help me!!!!!

Thank's for attention to this thread!

and all source code is in below:
Code:
 TC_HANDLE_T
TC_INIT(const char *tablename)
{
	TC_HANDLE_T h;
	STRUCT_GETINFO info;
	unsigned int tmp;
//typedef unsigned int socklen_t;
	socklefn = TC_INIT;
n_t s;
//static void *iptc_fn = NULL;
	iptc_
	if (strlen(tablename) >= TABLE_MAXNAMELEN) {
		errno = EINVAL;
		return NULL;
	}
//static int sockfd_use = 0;	
//static int sockfd = -1;
	if (sockfd_use == 0) {
		sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
		if (sockfd < 0)
			return NULL;
	}
	sockfd_use++;
retry:
	s = sizeof(info);

	strcpy(info.name, tablename);
	//#define SO_GET_INFO		IPT_SO_GET_INFO
	//#define IPT_SO_GET_INFO			(IPT_BASE_CTL)
	//#define IPT_BASE_CTL		64
	if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
		if (--sockfd_use == 0) {
			close(sockfd);
			sockfd = -1;
		}
		return NULL;
	}

	DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
		info.valid_hooks, info.num_entries, info.size);

	if ((h = alloc_handle(info.name, info.size, info.num_entries))
	    == NULL) {
		if (--sockfd_use == 0) {
			close(sockfd);
			sockfd = -1;
		}
		return NULL;
	}

	/* Initialize current state */
	h->info = info;

	h->entries->size = h->info.size;

	tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;

	if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
		       &tmp) < 0)
		goto error;
// question 3 -> where is /tmp/libiptc-so_get_entries.blob ?
#ifdef IPTC_DEBUG2
	{
		int fd = open("/tmp/libiptc-so_get_entries.blob", 
				O_CREAT|O_WRONLY);
		if (fd >= 0) {
			write(fd, h->entries, tmp);
			close(fd);
		}
	}
#endif

	if (parse_table(h) < 0)
		goto error;

	CHECK(h);
	return h;
error:
	TC_FREE(&h);
	/* A different process changed the ruleset size, retry */
	if (errno == EAGAIN)
		goto retry;
	return NULL;
}

Last edited by reddazz; 08-15-2008 at 06:34 AM. Reason: put code tags
 
Old 08-13-2008, 10:22 AM   #2
immortaltechnique
Member
 
Registered: Oct 2006
Location: Kenya
Distribution: Ubuntu, RHEL, OpenBSD
Posts: 287

Rep: Reputation: 32
Does this in anyway have to do with iptables?
 
Old 08-13-2008, 10:28 AM   #3
soltanihaji
LQ Newbie
 
Registered: Jul 2008
Posts: 24

Original Poster
Rep: Reputation: 15
Yes, this is a piece of libiptc.c one of library of iptables.
can you help me now ?
 
Old 08-13-2008, 10:43 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by soltanihaji View Post
Yes, this is a piece of libiptc.c one of library of iptables.
can you help me now ?
This is very similar to another thread you posted, about iptables, and asking for documentation on the source-code, because it was hard to read. Someone sent you a document, but if you're looking for someone to explain the source code to you, I'd suggest contacting the developers, or hiring someone who knows programming.
 
Old 08-13-2008, 10:49 AM   #5
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Man pages often have information, in this case too. You can use the 'man' command on your Linux to access the pages or search them from the web if you like; I'm quoting this page below.

Compare this
Code:
getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s);
with this
Code:
int getsockopt(int socket, int level, int option_name,
void *restrict option_value, socklen_t *restrict option_len);
The explanations are on the same manpage. Here's the description, then:
Quote:
Description

The getsockopt() function manipulates options associated with a socket.

The getsockopt() function shall retrieve the value for the option specified by the option_name argument for the socket specified by the socket argument. If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. Otherwise, the object pointed to by the option_len argument shall be modified to indicate the actual length of the value.

The level argument specifies the protocol level at which the option resides. To retrieve options at the socket level, specify the level argument as SOL_SOCKET. To retrieve options at other levels, supply the appropriate level identifier for the protocol controlling the option. For example, to indicate that an option is interpreted by the TCP (Transmission Control Protocol), set level to IPPROTO_TCP as defined in the <netinet/in.h> header.
So getsockopt() in this case fetches information from 'sockfd' which is the socket file descriptor, so that's where the information comes from. I agree that this is one of those things that looks confusing at first, and does require one to spend some time reading the appropriate man pages (or other information) about the functions before getting the grips of it..I spent quite some time (in my own opinion) trying to learn how these socket things work in Linux while writing a C++ program, and after I got over the difficulties of socket library functions which seem to be in C everywhere (examples and all), it took some time to 'properly' implement them into a C++ program so that they 'fit in'..
 
Old 08-13-2008, 10:53 AM   #6
soltanihaji
LQ Newbie
 
Registered: Jul 2008
Posts: 24

Original Poster
Rep: Reputation: 15
Hi Dear TB0ne
I know programming and this is a project in bachelor of science in my university.
my question isn't relevant to iptabales and this is only a software linux question about getsockopt.
I think that this forum is for these jobs and anyone can ask questions and don't want hiring programmers.
we have a proverb that says: if you can't help anyone, you don't chill him.
however thanks for your reply.
if anyone knows about getsockopt and my question please help me.

Last edited by soltanihaji; 08-13-2008 at 10:57 AM.
 
Old 08-13-2008, 11:16 AM   #7
soltanihaji
LQ Newbie
 
Registered: Jul 2008
Posts: 24

Original Poster
Rep: Reputation: 15
Hi dear b0uncer
I read this man page and the similar pages to this and I worked socket programming in C#.net in windows and understand concepts of sockets.
getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s);

in this code exists
#define SO_GET_INFO IPT_SO_GET_INFO
#define IPT_SO_GET_INFO (IPT_BASE_CTL)
#define IPT_BASE_CTL 64
means SO_GET_INFO=64
means getsockopt(sockfd, TC_IPPROTO, 64, &info, &s);
now my question is options are specified things or anything that we want? for example option in this code is information for one structure that defined by user. but in man pages I read options are things that describe socket.
Thanks for your reply and if you can help me anymore,I beforehand thank you.
 
Old 08-13-2008, 11:16 AM   #8
immortaltechnique
Member
 
Registered: Oct 2006
Location: Kenya
Distribution: Ubuntu, RHEL, OpenBSD
Posts: 287

Rep: Reputation: 32
Code:
I know programming and this is a project in bachelor of science in my university.
my question isn't relevant to iptabales and this is only a software 
linux question about getsockopt.I think that this forum is for these jobs and
anyone can ask questions and don't want hiring programmers
I frankly dont agree on you on the fact that this is a forum for asking homework questions. If you are as good as you sound in as far as programming is concerned, dont you figure you would know how to contact the writers of code you dont know how to read? Bah!!


You seem to have missed that link on LQ Job Place......what an atitude. Will be amazed if you get any decent amount of help on this one.
 
Old 08-13-2008, 11:20 AM   #9
soltanihaji
LQ Newbie
 
Registered: Jul 2008
Posts: 24

Original Poster
Rep: Reputation: 15
hi dear immortaltechnique
this is your idea and this is respectable for me.
but I think different with you.

Thanks for your reply
 
Old 08-13-2008, 11:34 AM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by immortaltechnique View Post
I frankly dont agree on you on the fact that this is a forum for asking homework questions.
I see a big distinction between asking a homework question here and asking a question about some specific point of confusion that is hit while putting obvious effort into some "homework" problem.

If I had known the answer to this, I probably would have posted it, and if not, it would have been because of the subject line, not the content.

Soltanihaji, you should select better subject lines for your future questions. The fact that this issue is important to you isn't an important part of the subject line. The fact that you want to understand getsockopt more than you could from its man page is important.

Quote:
Will be amazed if you get any decent amount of help on this one.
I'm already amazed at how good the help from b0uncer was so far.
 
Old 08-13-2008, 11:44 AM   #11
soltanihaji
LQ Newbie
 
Registered: Jul 2008
Posts: 24

Original Poster
Rep: Reputation: 15
Hi dear johnsfine

your suggestion is good. I will try do it for next.
Thank you from your reply.
but unfortunately I haven't received my proper answer yet!!!!
 
Old 08-13-2008, 12:24 PM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by soltanihaji View Post
Hi dear johnsfine

your suggestion is good. I will try do it for next.
Thank you from your reply.
but unfortunately I haven't received my proper answer yet!!!!
Then read the code and figure it out yourself. This is YOUR school project, not ours. Get a book on socket programming, read one of the thousands of examples/tutorials/how-tos that you can get from a simple Google search.

Quote:
we have a proverb that says: if you can't help anyone, you don't chill him.
You misunderstand....I *CAN* help you; I've been a programmer for over 20 years. b0uncer gave you a good amount of help, and you got yet more help in the other thread you posted this same question to. If you can't understand the help, and don't want to do the work, perhaps programming isn't something you should be doing. You've been pointed to the developers, been given documents, good advice, etc., but you still keep asking the same question.

Do your own homework.
 
Old 08-13-2008, 12:43 PM   #13
soltanihaji
LQ Newbie
 
Registered: Jul 2008
Posts: 24

Original Poster
Rep: Reputation: 15
Dear TB0ne
we have a proverb that says: don't disscuse with a person that doesnt have any capacity for it
because he wastes your time.

Thank you for your reply.
 
Old 08-13-2008, 01:02 PM   #14
immortaltechnique
Member
 
Registered: Oct 2006
Location: Kenya
Distribution: Ubuntu, RHEL, OpenBSD
Posts: 287

Rep: Reputation: 32
Quote:
You misunderstand....I *CAN* help you; I've been a programmer
for over 20 years. b0uncer gave you a good amount of help, and you got yet more help in the other
thread you posted this same question to. If you can't understand the help, and don't want to do the work, perhaps programming isn't something you should be doing. You've been pointed to the developers, been given documents, good advice, etc., but you still
keep asking the same question.

Do your own homework.

I couldnt have put it in a more politer way. YOu dont just waltz in here and try to prove to the community you know yet you dont. The bare-fact that you are even asking help with your homework clearly shows you are lazy. Simple.

Last edited by reddazz; 08-15-2008 at 06:35 AM. Reason: change code tags to quote tags
 
Old 08-13-2008, 01:16 PM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by soltanihaji View Post
Dear TB0ne
we have a proverb that says: don't disscuse with a person that doesnt have any capacity for it
because he wastes your time.

Thank you for your reply.
If you spent as much time on your programming as you do on your 'proverbs', maybe you'd have the problem solved.

And your proverb seems to apply to you....you've been given help, documents, and answers, and yet you don't seem to have the capacity to understand what you've been given.

And thanks for the compliment, immortaltechnique.
 
  


Closed Thread



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
Very important ! johnlefevre Linux - Enterprise 4 10-08-2005 09:38 AM
Really Important engnet Linux - Software 3 07-21-2004 02:41 PM
re :::!!! very important narendra_i General 5 10-22-2003 11:47 AM
important . cybercop12us Linux - Security 4 05-23-2002 11:39 AM
important !! Hard-Target Linux - Security 2 12-20-2001 04:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:27 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration