You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.2 KiB

2 years ago
  1. /*
  2. * Copyright 2008, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18. #include <string.h>
  19. #include <netinet/in.h>
  20. #include "dhcpmsg.h"
  21. static void *init_dhcp_msg(dhcp_msg *msg, int type, void *hwaddr, uint32_t xid)
  22. {
  23. uint8_t *x;
  24. memset(msg, 0, sizeof(dhcp_msg));
  25. msg->op = OP_BOOTREQUEST;
  26. msg->htype = HTYPE_ETHER;
  27. msg->hlen = 6;
  28. msg->hops = 0;
  29. msg->flags = htons(FLAGS_BROADCAST);
  30. msg->xid = xid;
  31. memcpy(msg->chaddr, hwaddr, 6);
  32. x = msg->options;
  33. *x++ = OPT_COOKIE1;
  34. *x++ = OPT_COOKIE2;
  35. *x++ = OPT_COOKIE3;
  36. *x++ = OPT_COOKIE4;
  37. *x++ = OPT_MESSAGE_TYPE;
  38. *x++ = 1;
  39. *x++ = type;
  40. return x;
  41. }
  42. int init_dhcp_discover_msg(dhcp_msg *msg, void *hwaddr, uint32_t xid)
  43. {
  44. uint8_t *x;
  45. x = init_dhcp_msg(msg, DHCPDISCOVER, hwaddr, xid);
  46. *x++ = OPT_PARAMETER_LIST;
  47. *x++ = 4;
  48. *x++ = OPT_SUBNET_MASK;
  49. *x++ = OPT_GATEWAY;
  50. *x++ = OPT_DNS;
  51. *x++ = OPT_BROADCAST_ADDR;
  52. *x++ = OPT_END;
  53. return DHCP_MSG_FIXED_SIZE + (x - msg->options);
  54. }
  55. int init_dhcp_request_msg(dhcp_msg *msg, void *hwaddr, uint32_t xid,
  56. uint32_t ipaddr, uint32_t serveraddr)
  57. {
  58. uint8_t *x;
  59. x = init_dhcp_msg(msg, DHCPREQUEST, hwaddr, xid);
  60. *x++ = OPT_PARAMETER_LIST;
  61. *x++ = 4;
  62. *x++ = OPT_SUBNET_MASK;
  63. *x++ = OPT_GATEWAY;
  64. *x++ = OPT_DNS;
  65. *x++ = OPT_BROADCAST_ADDR;
  66. *x++ = OPT_REQUESTED_IP;
  67. *x++ = 4;
  68. memcpy(x, &ipaddr, 4);
  69. x += 4;
  70. *x++ = OPT_SERVER_ID;
  71. *x++ = 4;
  72. memcpy(x, &serveraddr, 4);
  73. x += 4;
  74. *x++ = OPT_END;
  75. return DHCP_MSG_FIXED_SIZE + (x - msg->options);
  76. }