Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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.
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.
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'..
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.
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.
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.
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.