send message through socket?

Now I want to write a server - client program to send message between them. I want to use SOCK_RAW and IPPROTO_RAW to send so I don't want to use TCP/IP protocol. At server-side, I use libpcap to capture packet from the client. And I have two questions:
1. In sendto() function, there's parameter "const struct sockaddr *to", if I just want to send to a defined Mac address (ignore the IP address), what info I must supply here?
2. In case we broadcast the message, there're 2 network interface cards, how can I choose exactly what NIC will send out the message, other one will not send out any.

Thanks for your answers.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

> .. how can I choose exactly what NIC will send out the message
i.g you cannot from within your program
To do that you need to write a driver which plugs in in the TCP/IP stack of your OS (if possible), or you need to replace the TCP/IP stack, or find a patch for your stack.

I guess that you first get used to networking, probably read "TCP/IP Illustrated", Stevens  ;-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Since You use IP protocol, You have to address the destination as IP.
You would have to use different protocol, IPX maybe?

Well, those two nics have their IP from different networks(i assume). Instead broadcasting to send it to the broadcast address of the nic.
1) If you want to send to a raw MAC address instead of a IP address, you have to bypass
the IP layer all together.  I think you need to do a define a PACKET socket and construct the IP frames yourself...TCP/IP is really two protocols -- TCP over IP --  you only get tcp/ip with SOCK_STREAM -- SOCK_DGRAM gets you udp/ip, IPPROTO_RAW sets you on top of the IP layer...if you want to specify MAC address, you can't sit on top of the ip layer.

2) This should work

setsockopt(ifd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)+1)

              Bind this socket to a particular device like "eth0",  as  speci-
              fied  in  the  passed  interface  name.  If the name is an empty
              string or the option length is zero, the socket  device  binding
              is  removed.  The passed option is a variable-length null termi-
              nated interface name string with the maximum size  of  IFNAMSIZ.
              If a socket is bound to an interface, only packets received from
              that particular interface are processed by the socket. Note that
              this  only  works  for  some  socket types, particularly AF_INET
              sockets. It is not supported  for  packet  sockets  (use  normal
              bind(8) there).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.