title: boost::asio over SocketCAN
body: <p>I was thinking of making use of <a href="http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio.html" rel="noreferrer">Boost Asio</a> to read data from a <a href="http://en.wikipedia.org/wiki/SocketCAN" rel="noreferrer">Socket CAN</a>.
  There's nothing fancy going on in <a href="http://os1a.cs.columbia.edu/lxr/source/include/linux/can.h" rel="noreferrer"> linux/can.h </a>, and the device should
  behave like the loopback interface, and be used with a raw socket.</p>
  
  <p>Looking at the <code>basic_raw_socket</code> interface it seems that I can make use of
  <a href="http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/reference/basic_socket.html" rel="noreferrer">basic_raw_socket::assign</a> to assign the native socket created with</p>
  
  <pre><code>socket( PF_CAN, SOCK_RAW, CAN_RAW );
  </code></pre>
  
  <p>This is what I have so far</p>
  
  <pre><code>namespace can {
         class CanSocket {
         public:
                 typedef boost::asio::ip::basic_endpoint&lt;CanSocket&gt; endpoint;
                 typedef boost::asio::ip::basic_resolver_query&lt;CanSocket&gt; resolver_query;
                 typedef boost::asio::ip::basic_resolver_iterator&lt;CanSocket&gt; resolver_iterator;
                 typedef boost::asio::basic_raw_socket&lt;CanSocket&gt; socket;
                 typedef boost::asio::ip::basic_resolver&lt;CanSocket&gt; resolver;
  
                 CanSocket()
                         : _protocol( CAN_RAW )
                         , _family( PF_CAN )
                 {
                 }
  
                 static CanSocket v4()
                 {
                         return CanSocket();
                 }
                 static CanSocket v6();
                 int type() const;
                 int protocol() const;
                 int family() const;
  
                 friend bool operator==(const CanSocket&amp; p1, const CanSocket&amp; p2)
                 {
                         return p1._protocol != p2._protocol || p1._family != p2._family;
                 }
                 friend bool operator!=(const CanSocket&amp; p1, const CanSocket&amp; p2)
                 {
                         return p1._protocol == p2._protocol || p1._family == p2._family;
                 }
  
         private:
                 int _protocol;
                 int _family;
  };
  }
  </code></pre>
  
  <p>And this is how I use it in my application</p>
  
  <pre><code>   boost::asio::io_service ioserv;
  
     CanSocket::socket s( ioserv );
  
     int sock = socket( PF_CAN, SOCK_RAW, CAN_RAW );
  
     s.assign(CanSocket::v4(), sock);
  
     struct ifreq ifr;
     strcpy(ifr.ifr_name, "vcan0");
     ioctl(sock, SIOCGIFINDEX, &amp;ifr); /* ifr.ifr_ifindex gets filled
                                   * with that device's index */
  
     /* Select that CAN interface, and bind the socket to it. */
  
     /* this should be the endpoint */
     struct sockaddr_can addr;
     addr.can_family = AF_CAN;
     addr.can_ifindex = ifr.ifr_ifindex;
  
     /* s.bind (....) */
     bind( sock, (struct sockaddr*)&amp;addr, sizeof(addr) );
  </code></pre>
  
  <p>What I don't quite get is how do I <a href="http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/reference/basic_raw_socket/bind.html" rel="noreferrer">bind</a> <code>s</code> to the local endpoint? There are no IPs or ports involved.</p>
  
  <p>Is there anything else that should be implemented besides the endpoint to get it going?</p>