papout.c 4.0 KB

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