<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
<div>now , I see .</div><div><br></div>thank you , Jeff<div><br></div><div><br><div><br><br><div><div id="SkyDrivePlaceholder"></div><hr id="stopSpelling">Subject: RE: struct sock->inet_sock convert question<br>Date: Thu, 2 Feb 2012 10:36:03 -0800<br>From: jharan@bytemobile.com<br>To: duanshuidao@hotmail.com; kernelnewbies@kernelnewbies.org<br><br>
<meta http-equiv="Content-Type" content="text/html; charset=unicode">
<meta name="Generator" content="Microsoft SafeHTML"><style>
.ExternalClass p.ecxMsoNormal, .ExternalClass li.ecxMsoNormal, .ExternalClass div.ecxMsoNormal
{margin-bottom:.0001pt;font-size:12.0pt;font-family:"Times New Roman","serif";}
.ExternalClass a:link, .ExternalClass span.ecxMsoHyperlink
{color:blue;text-decoration:underline;}
.ExternalClass a:visited, .ExternalClass span.ecxMsoHyperlinkFollowed
{color:purple;text-decoration:underline;}
.ExternalClass p.ecxMsoPlainText, .ExternalClass li.ecxMsoPlainText, .ExternalClass div.ecxMsoPlainText
{margin-bottom:.0001pt;font-size:11.0pt;font-family:"Calibri","sans-serif";}
.ExternalClass p
{margin-right:0in;margin-left:0in;font-size:12.0pt;font-family:"Times New Roman","serif";}
.ExternalClass span.ecxapple-tab-span
{;}
.ExternalClass span.ecxEmailStyle19
{font-family:"Calibri","sans-serif";color:#1F497D;}
.ExternalClass span.ecxPlainTextChar
{font-family:"Calibri","sans-serif";}
.ExternalClass .ecxMsoChpDefault
{font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;}
.ExternalClass div.ecxWordSection1
{page:WordSection1;}
</style><div class="ecxWordSection1"><p class="ecxMsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span></p><p class="ecxMsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span></p><div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt"><div><div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in"><p class="ecxMsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> kernelnewbies-bounces@kernelnewbies.org [mailto:kernelnewbies-bounces@kernelnewbies.org] <b>On Behalf Of </b>hu jun<br><b>Sent:</b> Thursday, February 02, 2012 4:44 AM<br><b>To:</b> kernelnewbies@kernelnewbies.org<br><b>Subject:</b> struct sock->inet_sock convert question</span></p></div></div><p class="ecxMsoNormal"> </p><div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">in kernel linux-2.6.16.60-0.54.5 code:</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">static int inet_create(struct socket *sock, int protocol)</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">{</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">struct sock *sk;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">struct inet_sock *inet;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">....</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></p></div><div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1);</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">if (sk == NULL)</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">goto out;</span></p></div></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></p></div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:red">inet = inet_sk(sk);</span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""></span></p><div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) == INET_PROTOSW_ICSK;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">if (SOCK_RAW == sock->type) {</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">inet->num = protocol;</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">if (IPPROTO_RAW == protocol)</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">inet->hdrincl = 1;</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">}</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">if (ipv4_config.no_pmtu_disc)</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">inet->pmtudisc = IP_PMTUDISC_DONT;</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">else</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">inet->pmtudisc = IP_PMTUDISC_WANT;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">inet->id = 0;</span></p></div></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">....</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">}</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">my question is in the red line , why can convert like that?</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> </span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">thanks!</span></p><p class="ecxMsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span></p><p class="ecxMsoPlainText">I am not sure I understand your question, but inet_sk() is just a type cast:</p><p class="ecxMsoPlainText"> </p><p class="ecxMsoPlainText">static inline struct inet_sock *inet_sk(const struct sock *sk)</p><p class="ecxMsoPlainText">{</p><p class="ecxMsoPlainText"> return (struct inet_sock *)sk;</p><p class="ecxMsoPlainText">}</p><p class="ecxMsoPlainText"> </p><p class="ecxMsoPlainText">Which is valid since the first member of struct inet_sock is a struct sock:</p><p class="ecxMsoPlainText"> </p><p class="ecxMsoPlainText">struct inet_sock {</p><p class="ecxMsoPlainText"> /* sk and pinet6 has to be the first two members of inet_sock */</p><p class="ecxMsoPlainText"> struct sock sk;</p><p class="ecxMsoPlainText"> ...</p><p class="ecxMsoPlainText"> </p><p class="ecxMsoPlainText">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.</p><p class="ecxMsoPlainText"> </p><p class="ecxMsoPlainText">Jeff Haran</p><p class="ecxMsoPlainText"> </p><p class="ecxMsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"> </span></p></div></div></div></div><font face="monospace"></font><br>_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</div></div></div>                                            </div></body>
</html>