<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 ,&nbsp;Jeff<div><br></div><div><br><div><br><br><div><div id="SkyDrivePlaceholder"></div><hr id="stopSpelling">Subject: RE: struct sock-&gt;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:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">&nbsp;</span></p><p class="ecxMsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">&nbsp;</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:&quot;Tahoma&quot;,&quot;sans-serif&quot;">From:</span></b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"> 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-&gt;inet_sock convert question</span></p></div></div><p class="ecxMsoNormal">&nbsp;</p><div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">in kernel linux-2.6.16.60-0.54.5&nbsp;&nbsp;code:</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">static int inet_create(struct socket *sock, int protocol)</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">{</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">struct sock *sk;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">struct inet_sock *inet;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">....</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;</span></p></div><div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">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:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">if (sk == NULL)</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">goto out;</span></p></div></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;</span></p></div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;;color:red">inet = inet_sk(sk);</span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"></span></p><div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">inet-&gt;is_icsk = (INET_PROTOSW_ICSK &amp; answer_flags) == INET_PROTOSW_ICSK;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">if (SOCK_RAW == sock-&gt;type) {</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">inet-&gt;num = protocol;</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">if (IPPROTO_RAW == protocol)</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">inet-&gt;hdrincl = 1;</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">}</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">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:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">inet-&gt;pmtudisc = IP_PMTUDISC_DONT;</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">else</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">inet-&gt;pmtudisc = IP_PMTUDISC_WANT;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;</span></p></div><div><p class="ecxMsoNormal"><span class="ecxapple-tab-span"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">inet-&gt;id = 0;</span></p></div></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">....</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">}</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">my question is in the red line , &nbsp;why can convert &nbsp;like that?</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">&nbsp;</span></p></div><div><p class="ecxMsoNormal"><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">thanks!</span></p><p class="ecxMsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">&nbsp;</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">&nbsp;</p><p class="ecxMsoPlainText">static inline struct inet_sock *inet_sk(const struct sock *sk)</p><p class="ecxMsoPlainText">{</p><p class="ecxMsoPlainText">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (struct inet_sock *)sk;</p><p class="ecxMsoPlainText">}</p><p class="ecxMsoPlainText">&nbsp;</p><p class="ecxMsoPlainText">Which is valid since the first member of struct inet_sock is a struct sock:</p><p class="ecxMsoPlainText">&nbsp;</p><p class="ecxMsoPlainText">struct inet_sock {</p><p class="ecxMsoPlainText">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* sk and pinet6 has to be the first two members of inet_sock */</p><p class="ecxMsoPlainText">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct sock&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sk;</p><p class="ecxMsoPlainText">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...</p><p class="ecxMsoPlainText">&nbsp;</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">&nbsp;</p><p class="ecxMsoPlainText">Jeff Haran</p><p class="ecxMsoPlainText">&nbsp;</p><p class="ecxMsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">&nbsp;</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>