LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 04-17-2005, 08:53 AM   #1
linetnew
Member
 
Registered: Apr 2005
Location: India
Posts: 32

Rep: Reputation: 15
casting question in kernel source code


hello,
In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file
There is structure defined for queueing ip fragments as
struct ipq {
struct ipq *next; /* linked list pointers */
struct list_head lru_list; /* lru list member */
u32 user;
u32 saddr;
u32 daddr;
u16 id;
u8 protocol;
u8 last_in;
#define COMPLETE 4
#define FIRST_IN 2
#define LAST_IN 1

struct sk_buff *fragments; /* linked list of received fragments */
int len; /* total length of original datagram */
int meat;
spinlock_t lock;
atomic_t refcnt;
struct timer_list timer; /* when will this queue expire? */
struct ipq **pprev;
int iif;
struct timeval stamp;
};


static void ip_expire(unsigned long arg)
{
struct ipq *qp = (struct ipq *) arg;

spin_lock(&qp->lock);

if (qp->last_in & COMPLETE)
goto out;

ipq_kill(qp);
....
....
...
}

static struct ipq *ip_frag_create(unsigned hash, struct iphdr *iph, u32 user)
{
struct ipq *qp;

if ((qp = frag_alloc_queue()) == NULL)
goto out_nomem;

qp->protocol = iph->protocol;
qp->last_in = 0;
qp->id = iph->id;
qp->saddr = iph->saddr;
qp->daddr = iph->daddr;
qp->user = user;
qp->len = 0;
qp->meat = 0;
qp->fragments = NULL;
qp->iif = 0;

/* Initialize a timer for this entry. */
init_timer(&qp->timer);
qp->timer.data = (unsigned long) qp; /* pointer to queue */
qp->timer.function = ip_expire; /* expire function */
qp->lock = SPIN_LOCK_UNLOCKED;
atomic_set(&qp->refcnt, 1);

return ip_frag_intern(hash, qp);
}


My question is
1)how ip_expire is called in ip_frag_create? I mean where is its argument. check that function above.
2) how following casting occurs in ip_expire(unsigned long arg)
struct ipq *qp = (struct ipq *) arg;
how a unsigned long be converted to structure?
regards,
rahul.
 
Old 04-17-2005, 11:15 AM   #2
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Rep: Reputation: Disabled
can you please repost using the code tags?
 
Old 04-17-2005, 11:46 AM   #3
linetnew
Member
 
Registered: Apr 2005
Location: India
Posts: 32

Original Poster
Rep: Reputation: 15
question reposted:-
hello,
In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file
There is structure defined for queueing ip fragments as
Code:
struct ipq {
 struct ipq *next; /* linked list pointers */
struct list_head lru_list; /* lru list member */
u32 user;
u32 saddr;
u32 daddr;
u16 id;
u8 protocol;
u8 last_in;
#define COMPLETE 4
#define FIRST_IN 2
#define LAST_IN 1

struct sk_buff *fragments; /* linked list of received fragments */
int len; /* total length of original datagram */
int meat;
spinlock_t lock;
atomic_t refcnt;
struct timer_list timer; /* when will this queue expire? */
struct ipq **pprev;
int iif;
struct timeval stamp;
};
Code:
static void ip_expire(unsigned long arg)
{
struct ipq *qp = (struct ipq *) arg;

spin_lock(&qp->lock);

if (qp->last_in & COMPLETE)
goto out;

ipq_kill(qp);
....
....
...
}
Code:
static struct ipq *ip_frag_create(unsigned hash, struct iphdr *iph, u32 user)
{
struct ipq *qp;

if ((qp = frag_alloc_queue()) == NULL)
goto out_nomem;

qp->protocol = iph->protocol;
qp->last_in = 0;
qp->id = iph->id;
qp->saddr = iph->saddr;
qp->daddr = iph->daddr;
qp->user = user;
qp->len = 0;
qp->meat = 0;
qp->fragments = NULL;
qp->iif = 0;

/* Initialize a timer for this entry. */
init_timer(&qp->timer);
qp->timer.data = (unsigned long) qp; /* pointer to queue */
qp->timer.function = ip_expire; /* expire function */
qp->lock = SPIN_LOCK_UNLOCKED;
atomic_set(&qp->refcnt, 1);

return ip_frag_intern(hash, qp);
}
My question is
1)how ip_expire is called in ip_frag_create? I mean where is its argument. check that function above.
2) how following casting occurs in ip_expire(unsigned long arg)
struct ipq *qp = (struct ipq *) arg;
how a unsigned long be converted to structure?
regards,
rahul.

Last edited by linetnew; 04-17-2005 at 11:48 AM.
 
Old 04-18-2005, 04:55 AM   #4
mehuljv
Member
 
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
1)how ip_expire is called in ip_frag_create? I mean where is its argument. check that function above.

qp->timer.function = ip_expire; /* expire function */

i guess function is nt called , they are just setting a pointer to function.

2) how following casting occurs in ip_expire(unsigned long arg)
struct ipq *qp = (struct ipq *) arg;
how a unsigned long be converted to structure?

In C you can typecast long to structure, see foll. example
struct test
{
int a;
};
fun (unsigned ling b)
{
struct test *a = (struct test *)b;
print(a->a);
}
int main()
{
struct test a;
a.a = 10;
fub((unsigned long)&a);
}

this program will print 10 on the screen.

regards,
mehul.
 
  


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
A source code housekeeping question Dewar Linux - General 1 12-11-2003 07:02 PM
Question about Source Code Pwcca Linux - General 3 03-12-2003 10:58 PM
Another source code question. JMC Linux - General 3 05-28-2002 01:58 AM
Question about source code locations hanzerik Linux From Scratch 4 02-06-2002 09:33 PM
source code and platform question iggymac Linux - Software 3 01-24-2002 05:43 PM


All times are GMT -5. The time now is 11:43 AM.

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