<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="296948" data-post-id="296948">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="gus" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gus/120/37315_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  gus
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Nerves Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I can confirm that running the following does work (ie, using the same interface index provided by <code>ioctl</code> to get the name as the interface index):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">{:ok, sock} = :socket.open(29, :raw, 1)

# confirm ifindex and ifname
{:ok, ifindex} = :socket.ioctl(sock, :gifindex, 'can0') # returns {:ok, 2}
:socket.ioctl(sock, :gifname, ifindex) = {:ok, ~c"can0"}

# using index from ioctl doesn't work
addr = &lt;&lt;29::size(16)-little, 0::size(16)-little, ifindex::size(32)-little, 0::size(32), 0::size(32), 0::size(64)&gt;&gt;
:socket.bind(sock, %{:family =&gt; 29, :addr =&gt; addr}) = {:error, :enodev}

# hard code to index 0 works
ifindex = 0
addr = &lt;&lt;29::size(16)-little, 0::size(16)-little, ifindex::size(32)-little, 0::size(32), 0::size(32), 0::size(64)&gt;&gt;
:socket.bind(sock, %{:family =&gt; 29, :addr =&gt; addr}) = :ok
</code></pre>
<aside class="quote group-Erlang-Core-Team" data-username="jhogberg" data-post="11" data-topic="57294">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jhogberg/48/24176_2.png" class="avatar"> jhogberg:</div>
<blockquote>
<p>I’d be surprised if the ABI is different, so try checking how <code>struct sockaddr_can</code> looks in memory if you set all the fields to recognizable values, maybe we’ve missed something when constructing the binary address.</p>
</blockquote>
</aside>
<p>I can give this a go! But I think this part seems to be working at the moment, I think I was more referring to the interface indices are probably not the same since the network interfaces might be configured in a different order for Nerves vs Raspbian. But let me try your suggestion <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="296948" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/erlang-socket-module-for-socketcan-on-nerves-device/57294/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-296948" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="296948"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="296984" data-post-id="296984">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="gus" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gus/120/37315_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  gus
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Nerves Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>An interesting discovery in the SocketCAN docs - a interface index of 0 corresponds to binding on “any” interface.</p>
<blockquote>
<p>When the CAN interface is bound to ‘any’ existing CAN interface (addr.can_ifindex = 0) it is recommended to use recvfrom(2) if the information about the originating CAN interface is needed</p>
</blockquote>
<p>Also, the resulting bytes of the <code>sockaddr_can</code> struct test is printed below (in hex):</p>
<p><code>34, 12, 0, 0, bc, 9a, 78, 56, 88, 77, 66, 55, 44, 33, 22, 11, 0, 0, 0, 0, 0, 0, 0, 0</code></p>
<p>The equivalent bytes when printing this from the packet formed with the Elixir code is:</p>
<p><code>34 12 00 00 BC 9A 78 56 88 77 66 55 44 33 22 11 00 00 00 00 00 00 00 00</code></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="296984" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/erlang-socket-module-for-socketcan-on-nerves-device/57294/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-296984" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="296984"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="305141" data-post-id="305141">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="pgorczak" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/pgorczak/120/32263_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  pgorczak
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>So I ran into this today and figured out the issue with the address data.</p>
<p>The <code>:socket.bind/2</code> function takes a map with <code>:family</code> and <code>:addr</code> keys. Their values get copied into the <code>sa_family</code> and <code>sa_data</code> fields of a <code>sockaddr</code> struct in the <a href="https://github.com/erlang/otp/blob/dd57c853a324a9572a9e5ce227d8675ff004c6fe/erts/emulator/nifs/common/socket_util.c#L2071-L2072" rel="noopener nofollow ugc">erlang source</a>.</p>
<pre data-code-wrap="c"><code class="lang-c">struct sockaddr addr;
((struct sockaddr_can*) &amp;addr)-&gt;can_ifindex = ifindex;
return addr.sa_data; // &lt;- this is what we want to pass to :socket.bind
</code></pre>
<p>This means, your approach works, when cutting off the first field of the binary:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">addr = &lt;&lt;0::size(16)-little, ifindex::size(32)-little, 0::size(32), 0::size(32), 0::size(64)&gt;&gt;
</code></pre>
<p>The initial 2 Bytes of padding remain–I think because <code>sockaddr</code> is aligned to 2 Bytes while <code>sockaddr_can</code> is aligned to 4 Bytes (can_ifindex being an int).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="305141" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/erlang-socket-module-for-socketcan-on-nerves-device/57294/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-305141" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="305141"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>