3
0

clientsocket.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * clientsocket.c -- DHCP client socket creation
  3. *
  4. * udhcp client
  5. *
  6. * Russ Dill <Russ.Dill@asu.edu> July 2001
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <unistd.h>
  25. #include <netinet/in.h>
  26. #include <features.h>
  27. #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
  28. #include <netpacket/packet.h>
  29. #include <net/ethernet.h>
  30. #else
  31. #include <asm/types.h>
  32. #include <linux/if_packet.h>
  33. #include <linux/if_ether.h>
  34. #endif
  35. #include "clientsocket.h"
  36. #include "common.h"
  37. int raw_socket(int ifindex)
  38. {
  39. int fd;
  40. struct sockaddr_ll sock;
  41. DEBUG(LOG_INFO, "Opening raw socket on ifindex %d", ifindex);
  42. if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
  43. DEBUG(LOG_ERR, "socket call failed: %m");
  44. return -1;
  45. }
  46. sock.sll_family = AF_PACKET;
  47. sock.sll_protocol = htons(ETH_P_IP);
  48. sock.sll_ifindex = ifindex;
  49. if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
  50. DEBUG(LOG_ERR, "bind call failed: %m");
  51. close(fd);
  52. return -1;
  53. }
  54. return fd;
  55. }