ipcpout.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH
  3. * Copyright (c) 1989 by Carnegie Mellon University
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * \file net/ipcout.c
  37. * \brief PPP IPC output functions.
  38. *
  39. * \verbatim
  40. * $Id: ipcpout.c 3683 2011-12-04 13:42:04Z haraldkipp $
  41. * \endverbatim
  42. */
  43. #include <string.h>
  44. #include <dev/ppp.h>
  45. #include <sys/heap.h>
  46. #include <sys/thread.h>
  47. #include <sys/types.h>
  48. #include <net/if_var.h>
  49. #include <netinet/if_ppp.h>
  50. #include <netinet/ppp_fsm.h>
  51. #include <netinet/in.h>
  52. #include <net/ppp.h>
  53. /*!
  54. * \addtogroup xgIPCP
  55. */
  56. /*@{*/
  57. /*!
  58. * \brief Send a IPCP packet.
  59. *
  60. * \note Applications typically do not call this function.
  61. *
  62. * \param dev Identifies the device to use.
  63. * \param code Type subcode.
  64. * \param id Exchange identifier.
  65. * \param nb Network buffer structure containing the packet to send
  66. * or null if the packet contains no information.
  67. * The structure must have been allocated by a previous
  68. * call NutNetBufAlloc() and will be freed when this function
  69. * returns.
  70. *
  71. * \return 0 on success, -1 in case of any errors.
  72. */
  73. int NutIpcpOutput(NUTDEVICE * dev, uint8_t code, uint8_t id, NETBUF * nb)
  74. {
  75. XCPHDR *xcp;
  76. if ((nb = NutNetBufAlloc(nb, NBAF_NETWORK, sizeof(XCPHDR))) == 0)
  77. return -1;
  78. xcp = nb->nb_nw.vp;
  79. xcp->xch_code = code;
  80. xcp->xch_id = id;
  81. xcp->xch_len = htons(nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz);
  82. if (NutPppOutput(dev, PPP_IPCP, 0, nb) == 0) {
  83. NutNetBufFree(nb);
  84. return 0;
  85. }
  86. return -1;
  87. }
  88. /*
  89. * Send a Configure-Request.
  90. * TODO: May use preconfigured addresses.
  91. */
  92. void IpcpTxConfReq(NUTDEVICE *dev, uint8_t id)
  93. {
  94. PPPDCB *dcb = dev->dev_dcb;
  95. XCPOPT *xcpo;
  96. NETBUF *nb;
  97. int len;
  98. /*
  99. * Not currently negotiating.
  100. */
  101. if(dcb->dcb_ipcp_state != PPPS_REQSENT &&
  102. dcb->dcb_ipcp_state != PPPS_ACKRCVD &&
  103. dcb->dcb_ipcp_state != PPPS_ACKSENT) {
  104. dcb->dcb_ipcp_naks = 0;
  105. }
  106. dcb->dcb_acked = 0;
  107. /*
  108. * Create the request.
  109. */
  110. len = 6;
  111. if((dcb->dcb_rejects & REJ_IPCP_DNS1) == 0)
  112. len += 6;
  113. if((dcb->dcb_rejects & REJ_IPCP_DNS2) == 0)
  114. len += 6;
  115. if ((nb = NutNetBufAlloc(0, NBAF_APPLICATION, len)) != 0) {
  116. xcpo = nb->nb_ap.vp;
  117. xcpo->xcpo_type = IPCP_ADDR;
  118. xcpo->xcpo_len = 6;
  119. xcpo->xcpo_.ul = dcb->dcb_local_ip;
  120. if((dcb->dcb_rejects & REJ_IPCP_DNS1) == 0) {
  121. xcpo = (XCPOPT *)((char *)xcpo + xcpo->xcpo_len);
  122. xcpo->xcpo_type = IPCP_MS_DNS1;
  123. xcpo->xcpo_len = 6;
  124. xcpo->xcpo_.ul = dcb->dcb_ip_dns1;
  125. }
  126. if((dcb->dcb_rejects & REJ_IPCP_DNS2) == 0) {
  127. xcpo = (XCPOPT *)((char *)xcpo + xcpo->xcpo_len);
  128. xcpo->xcpo_type = IPCP_MS_DNS2;
  129. xcpo->xcpo_len = 6;
  130. xcpo->xcpo_.ul = dcb->dcb_ip_dns2;
  131. }
  132. NutIpcpOutput(dev, XCP_CONFREQ, id, nb);
  133. }
  134. }
  135. /*@}*/