struct sock->inet_sock convert question

3 messages, 2 authors, 2012-02-03 · open the first message on its own page

struct sock->inet_sock convert question

From: hu jun <hidden>
Date: 2012-02-02 12:44:00

in kernel linux-2.6.16.60-0.54.5  code:
static int inet_create(struct socket *sock, int protocol){struct sock *sk;struct inet_sock *inet;....
sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1);	if (sk == NULL)		goto out;
inet = inet_sk(sk);inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) == INET_PROTOSW_ICSK;
	if (SOCK_RAW == sock->type) {		inet->num = protocol;		if (IPPROTO_RAW == protocol)			inet->hdrincl = 1;	}
	if (ipv4_config.no_pmtu_disc)		inet->pmtudisc = IP_PMTUDISC_DONT;	else		inet->pmtudisc = IP_PMTUDISC_WANT;
	inet->id = 0;....}
my question is in the red line ,  why can convert  like that?
thanks! 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120202/acddbb43/attachment.html 

struct sock->inet_sock convert question

From: Jeff Haran <hidden>
Date: 2012-02-02 18:36:03

 

 

From: kernelnewbies-bounces@kernelnewbies.org
[mailto:kernelnewbies-bounces at kernelnewbies.org] On Behalf Of hu jun
Sent: Thursday, February 02, 2012 4:44 AM
To: kernelnewbies at kernelnewbies.org
Subject: struct sock->inet_sock convert question

 

in kernel linux-2.6.16.60-0.54.5  code:

 

static int inet_create(struct socket *sock, int protocol)

{

struct sock *sk;

struct inet_sock *inet;

....

 

sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1);

          if (sk == NULL)

                      goto out;

 

inet = inet_sk(sk);

inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) == INET_PROTOSW_ICSK;

 

          if (SOCK_RAW == sock->type) {

                      inet->num = protocol;

                      if (IPPROTO_RAW == protocol)

                                  inet->hdrincl = 1;

          }

 

          if (ipv4_config.no_pmtu_disc)

                      inet->pmtudisc = IP_PMTUDISC_DONT;

          else

                      inet->pmtudisc = IP_PMTUDISC_WANT;

 

          inet->id = 0;

....

}

 

my question is in the red line ,  why can convert  like that?

 

thanks!

 

I am not sure I understand your question, but inet_sk() is just a type
cast:

 

static inline struct inet_sock *inet_sk(const struct sock *sk)

{

        return (struct inet_sock *)sk;

}

 

Which is valid since the first member of struct inet_sock is a struct
sock:

 

struct inet_sock {

        /* sk and pinet6 has to be the first two members of inet_sock */

        struct sock             sk;

       ...

 

I haven't dived into the bowels of sk_alloc(), but I assume it allocates
a big enough memory block so that after the type cast, the resultant
pointer points to memory big enough to store an struct inet_sock or any
other *_sock structures that get cast to struct sock.

 

Jeff Haran

 

 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120202/e9415039/attachment.html 

struct sock->inet_sock convert question

From: hu jun <hidden>
Date: 2012-02-03 00:55:42

now , I see .
thank you , Jeff



Subject: RE: struct sock->inet_sock convert question
Date: Thu, 2 Feb 2012 10:36:03 -0800
From: jharan@bytemobile.com
To: duanshuidao at hotmail.com; kernelnewbies at kernelnewbies.org



  From: kernelnewbies-bounces at kernelnewbies.org [mailto:kernelnewbies-bounces at kernelnewbies.org] On Behalf Of hu jun
Sent: Thursday, February 02, 2012 4:44 AM
To: kernelnewbies at kernelnewbies.org
Subject: struct sock->inet_sock convert question in kernel linux-2.6.16.60-0.54.5  code: static int inet_create(struct socket *sock, int protocol){struct sock *sk;struct inet_sock *inet;.... sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1);          if (sk == NULL)                      goto out; inet = inet_sk(sk);inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) == INET_PROTOSW_ICSK;           if (SOCK_RAW == sock->type) {                      inet->num = protocol;                      if (IPPROTO_RAW == protocol)                                  inet->hdrincl = 1;          }           if (ipv4_config.no_pmtu_disc)                      inet->pmtudisc = IP_PMTUDISC_DONT;          else                      inet->pmtudisc = IP_PMTUDISC_WANT;           inet->id = 0;....} my question is in the red line ,  why can convert  like that? thanks! I am not sure I understand your question, but inet_sk() is just a type cast: static inline struct inet_sock *inet_sk(const struct sock *sk){        return (struct inet_sock *)sk;} Which is valid since the first member of struct inet_sock is a struct sock: struct inet_sock {        /* sk and pinet6 has to be the first two members of inet_sock */        struct sock             sk;       ... I haven't dived into the bowels of sk_alloc(), but I assume it allocates a big enough memory block so that after the type cast, the resultant pointer points to memory big enough to store an struct inet_sock or any other *_sock structures that get cast to struct sock. Jeff Haran  
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120203/7dd588e3/attachment-0001.html 
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help