Webserver+3d print
cyclone_tcp/ppp/ppp_misc.c@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file ppp_misc.c |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief PPP miscellaneous functions |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneTCP Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 26 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 27 | **/ |
Sergunb | 0:8918a71cdbe9 | 28 | |
Sergunb | 0:8918a71cdbe9 | 29 | //Switch to the appropriate trace level |
Sergunb | 0:8918a71cdbe9 | 30 | #define TRACE_LEVEL PPP_TRACE_LEVEL |
Sergunb | 0:8918a71cdbe9 | 31 | |
Sergunb | 0:8918a71cdbe9 | 32 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 33 | #include "core/net.h" |
Sergunb | 0:8918a71cdbe9 | 34 | #include "ppp/ppp_misc.h" |
Sergunb | 0:8918a71cdbe9 | 35 | #include "ppp/ppp_debug.h" |
Sergunb | 0:8918a71cdbe9 | 36 | #include "ppp/lcp.h" |
Sergunb | 0:8918a71cdbe9 | 37 | #include "ppp/ipcp.h" |
Sergunb | 0:8918a71cdbe9 | 38 | #include "ppp/ipv6cp.h" |
Sergunb | 0:8918a71cdbe9 | 39 | #include "debug.h" |
Sergunb | 0:8918a71cdbe9 | 40 | |
Sergunb | 0:8918a71cdbe9 | 41 | //Check TCP/IP stack configuration |
Sergunb | 0:8918a71cdbe9 | 42 | #if (PPP_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 43 | |
Sergunb | 0:8918a71cdbe9 | 44 | |
Sergunb | 0:8918a71cdbe9 | 45 | /** |
Sergunb | 0:8918a71cdbe9 | 46 | * @brief Send Configure-Ack, Nak or Reject packet |
Sergunb | 0:8918a71cdbe9 | 47 | * @param[in] context PPP context |
Sergunb | 0:8918a71cdbe9 | 48 | * @param[in] configureReqPacket Pointer to the incoming Configure-Request |
Sergunb | 0:8918a71cdbe9 | 49 | * @param[in] protocol Protocol field |
Sergunb | 0:8918a71cdbe9 | 50 | * @param[in] code Code field |
Sergunb | 0:8918a71cdbe9 | 51 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 52 | **/ |
Sergunb | 0:8918a71cdbe9 | 53 | |
Sergunb | 0:8918a71cdbe9 | 54 | error_t pppSendConfigureAckNak(PppContext *context, |
Sergunb | 0:8918a71cdbe9 | 55 | const PppConfigurePacket *configureReqPacket, PppProtocol protocol, PppCode code) |
Sergunb | 0:8918a71cdbe9 | 56 | { |
Sergunb | 0:8918a71cdbe9 | 57 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 58 | size_t length; |
Sergunb | 0:8918a71cdbe9 | 59 | size_t offset; |
Sergunb | 0:8918a71cdbe9 | 60 | NetBuffer *buffer; |
Sergunb | 0:8918a71cdbe9 | 61 | PppConfigurePacket *configureAckNakPacket; |
Sergunb | 0:8918a71cdbe9 | 62 | PppOption *option; |
Sergunb | 0:8918a71cdbe9 | 63 | |
Sergunb | 0:8918a71cdbe9 | 64 | //Initialize status code |
Sergunb | 0:8918a71cdbe9 | 65 | error = NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 66 | //Retrieve the length of the Configure-Request packet |
Sergunb | 0:8918a71cdbe9 | 67 | length = ntohs(configureReqPacket->length); |
Sergunb | 0:8918a71cdbe9 | 68 | |
Sergunb | 0:8918a71cdbe9 | 69 | //Allocate a buffer memory to hold the Configure-Ack, Nak or Reject packet |
Sergunb | 0:8918a71cdbe9 | 70 | buffer = pppAllocBuffer(length, &offset); |
Sergunb | 0:8918a71cdbe9 | 71 | //Failed to allocate memory? |
Sergunb | 0:8918a71cdbe9 | 72 | if(buffer == NULL) |
Sergunb | 0:8918a71cdbe9 | 73 | return ERROR_OUT_OF_MEMORY; |
Sergunb | 0:8918a71cdbe9 | 74 | |
Sergunb | 0:8918a71cdbe9 | 75 | //Point to the beginning of the packet |
Sergunb | 0:8918a71cdbe9 | 76 | configureAckNakPacket = netBufferAt(buffer, offset); |
Sergunb | 0:8918a71cdbe9 | 77 | |
Sergunb | 0:8918a71cdbe9 | 78 | //Format packet header |
Sergunb | 0:8918a71cdbe9 | 79 | configureAckNakPacket->code = code; |
Sergunb | 0:8918a71cdbe9 | 80 | configureAckNakPacket->identifier = configureReqPacket->identifier; |
Sergunb | 0:8918a71cdbe9 | 81 | configureAckNakPacket->length = sizeof(PppConfigurePacket); |
Sergunb | 0:8918a71cdbe9 | 82 | |
Sergunb | 0:8918a71cdbe9 | 83 | //Retrieve the length of the option list |
Sergunb | 0:8918a71cdbe9 | 84 | length -= sizeof(PppConfigurePacket); |
Sergunb | 0:8918a71cdbe9 | 85 | //Point to the first option |
Sergunb | 0:8918a71cdbe9 | 86 | option = (PppOption *) configureReqPacket->options; |
Sergunb | 0:8918a71cdbe9 | 87 | |
Sergunb | 0:8918a71cdbe9 | 88 | //Parse configuration options |
Sergunb | 0:8918a71cdbe9 | 89 | while(length > 0) |
Sergunb | 0:8918a71cdbe9 | 90 | { |
Sergunb | 0:8918a71cdbe9 | 91 | //LCP protocol? |
Sergunb | 0:8918a71cdbe9 | 92 | if(protocol == PPP_PROTOCOL_LCP) |
Sergunb | 0:8918a71cdbe9 | 93 | { |
Sergunb | 0:8918a71cdbe9 | 94 | //Parse LCP option |
Sergunb | 0:8918a71cdbe9 | 95 | lcpParseOption(context, option, length, configureAckNakPacket); |
Sergunb | 0:8918a71cdbe9 | 96 | } |
Sergunb | 0:8918a71cdbe9 | 97 | #if (IPV4_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 98 | //IPCP protocol? |
Sergunb | 0:8918a71cdbe9 | 99 | else if(protocol == PPP_PROTOCOL_IPCP) |
Sergunb | 0:8918a71cdbe9 | 100 | { |
Sergunb | 0:8918a71cdbe9 | 101 | //Parse IPCP option |
Sergunb | 0:8918a71cdbe9 | 102 | ipcpParseOption(context, option, length, configureAckNakPacket); |
Sergunb | 0:8918a71cdbe9 | 103 | } |
Sergunb | 0:8918a71cdbe9 | 104 | #endif |
Sergunb | 0:8918a71cdbe9 | 105 | #if (IPV6_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 106 | //IPV6CP protocol? |
Sergunb | 0:8918a71cdbe9 | 107 | else if(protocol == PPP_PROTOCOL_IPV6CP) |
Sergunb | 0:8918a71cdbe9 | 108 | { |
Sergunb | 0:8918a71cdbe9 | 109 | //Parse IPV6CP option |
Sergunb | 0:8918a71cdbe9 | 110 | ipv6cpParseOption(context, option, length, configureAckNakPacket); |
Sergunb | 0:8918a71cdbe9 | 111 | } |
Sergunb | 0:8918a71cdbe9 | 112 | #endif |
Sergunb | 0:8918a71cdbe9 | 113 | |
Sergunb | 0:8918a71cdbe9 | 114 | //Remaining bytes to process |
Sergunb | 0:8918a71cdbe9 | 115 | length -= option->length; |
Sergunb | 0:8918a71cdbe9 | 116 | //Jump to the next option |
Sergunb | 0:8918a71cdbe9 | 117 | option = (PppOption *) ((uint8_t *) option + option->length); |
Sergunb | 0:8918a71cdbe9 | 118 | } |
Sergunb | 0:8918a71cdbe9 | 119 | |
Sergunb | 0:8918a71cdbe9 | 120 | //Adjust the length of the multi-part buffer |
Sergunb | 0:8918a71cdbe9 | 121 | netBufferSetLength(buffer, offset + configureAckNakPacket->length); |
Sergunb | 0:8918a71cdbe9 | 122 | //Convert length field to network byte order |
Sergunb | 0:8918a71cdbe9 | 123 | configureAckNakPacket->length = htons(configureAckNakPacket->length); |
Sergunb | 0:8918a71cdbe9 | 124 | |
Sergunb | 0:8918a71cdbe9 | 125 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 126 | if(code == PPP_CODE_CONFIGURE_ACK) |
Sergunb | 0:8918a71cdbe9 | 127 | { |
Sergunb | 0:8918a71cdbe9 | 128 | TRACE_INFO("Sending Configure-Ack packet (%" PRIuSIZE " bytes)...\r\n", |
Sergunb | 0:8918a71cdbe9 | 129 | ntohs(configureAckNakPacket->length)); |
Sergunb | 0:8918a71cdbe9 | 130 | } |
Sergunb | 0:8918a71cdbe9 | 131 | else if(code == PPP_CODE_CONFIGURE_NAK) |
Sergunb | 0:8918a71cdbe9 | 132 | { |
Sergunb | 0:8918a71cdbe9 | 133 | TRACE_INFO("Sending Configure-Nak packet (%" PRIuSIZE " bytes)...\r\n", |
Sergunb | 0:8918a71cdbe9 | 134 | ntohs(configureAckNakPacket->length)); |
Sergunb | 0:8918a71cdbe9 | 135 | } |
Sergunb | 0:8918a71cdbe9 | 136 | else if(code == PPP_CODE_CONFIGURE_REJ) |
Sergunb | 0:8918a71cdbe9 | 137 | { |
Sergunb | 0:8918a71cdbe9 | 138 | TRACE_INFO("Sending Configure-Reject packet (%" PRIuSIZE " bytes)...\r\n", |
Sergunb | 0:8918a71cdbe9 | 139 | ntohs(configureAckNakPacket->length)); |
Sergunb | 0:8918a71cdbe9 | 140 | } |
Sergunb | 0:8918a71cdbe9 | 141 | |
Sergunb | 0:8918a71cdbe9 | 142 | //Dump packet contents for debugging purpose |
Sergunb | 0:8918a71cdbe9 | 143 | pppDumpPacket((PppPacket *) configureAckNakPacket, |
Sergunb | 0:8918a71cdbe9 | 144 | ntohs(configureAckNakPacket->length), protocol); |
Sergunb | 0:8918a71cdbe9 | 145 | |
Sergunb | 0:8918a71cdbe9 | 146 | //Send PPP frame |
Sergunb | 0:8918a71cdbe9 | 147 | error = pppSendFrame(context->interface, buffer, offset, protocol); |
Sergunb | 0:8918a71cdbe9 | 148 | |
Sergunb | 0:8918a71cdbe9 | 149 | //Free previously allocated memory block |
Sergunb | 0:8918a71cdbe9 | 150 | netBufferFree(buffer); |
Sergunb | 0:8918a71cdbe9 | 151 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 152 | return error; |
Sergunb | 0:8918a71cdbe9 | 153 | } |
Sergunb | 0:8918a71cdbe9 | 154 | |
Sergunb | 0:8918a71cdbe9 | 155 | |
Sergunb | 0:8918a71cdbe9 | 156 | /** |
Sergunb | 0:8918a71cdbe9 | 157 | * @brief Send Terminate-Request packet |
Sergunb | 0:8918a71cdbe9 | 158 | * @param[in] context PPP context |
Sergunb | 0:8918a71cdbe9 | 159 | * @param[in] identifier Identifier field |
Sergunb | 0:8918a71cdbe9 | 160 | * @param[in] protocol Protocol field |
Sergunb | 0:8918a71cdbe9 | 161 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 162 | **/ |
Sergunb | 0:8918a71cdbe9 | 163 | |
Sergunb | 0:8918a71cdbe9 | 164 | error_t pppSendTerminateReq(PppContext *context, |
Sergunb | 0:8918a71cdbe9 | 165 | uint8_t identifier, PppProtocol protocol) |
Sergunb | 0:8918a71cdbe9 | 166 | { |
Sergunb | 0:8918a71cdbe9 | 167 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 168 | size_t length; |
Sergunb | 0:8918a71cdbe9 | 169 | size_t offset; |
Sergunb | 0:8918a71cdbe9 | 170 | NetBuffer *buffer; |
Sergunb | 0:8918a71cdbe9 | 171 | PppTerminatePacket *terminateReqPacket; |
Sergunb | 0:8918a71cdbe9 | 172 | |
Sergunb | 0:8918a71cdbe9 | 173 | //Length of the Terminate-Request packet |
Sergunb | 0:8918a71cdbe9 | 174 | length = sizeof(PppTerminatePacket); |
Sergunb | 0:8918a71cdbe9 | 175 | |
Sergunb | 0:8918a71cdbe9 | 176 | //Allocate a buffer memory to hold the Terminate-Request packet |
Sergunb | 0:8918a71cdbe9 | 177 | buffer = pppAllocBuffer(length, &offset); |
Sergunb | 0:8918a71cdbe9 | 178 | //Failed to allocate memory? |
Sergunb | 0:8918a71cdbe9 | 179 | if(buffer == NULL) |
Sergunb | 0:8918a71cdbe9 | 180 | return ERROR_OUT_OF_MEMORY; |
Sergunb | 0:8918a71cdbe9 | 181 | |
Sergunb | 0:8918a71cdbe9 | 182 | //Point to the Terminate-Request packet |
Sergunb | 0:8918a71cdbe9 | 183 | terminateReqPacket = netBufferAt(buffer, offset); |
Sergunb | 0:8918a71cdbe9 | 184 | |
Sergunb | 0:8918a71cdbe9 | 185 | //Format packet header |
Sergunb | 0:8918a71cdbe9 | 186 | terminateReqPacket->code = PPP_CODE_TERMINATE_REQ; |
Sergunb | 0:8918a71cdbe9 | 187 | terminateReqPacket->identifier = identifier; |
Sergunb | 0:8918a71cdbe9 | 188 | terminateReqPacket->length = htons(length); |
Sergunb | 0:8918a71cdbe9 | 189 | |
Sergunb | 0:8918a71cdbe9 | 190 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 191 | TRACE_INFO("Sending Terminate-Request packet (%" PRIuSIZE " bytes)...\r\n", length); |
Sergunb | 0:8918a71cdbe9 | 192 | //Dump packet contents for debugging purpose |
Sergunb | 0:8918a71cdbe9 | 193 | pppDumpPacket((PppPacket *) terminateReqPacket, length, protocol); |
Sergunb | 0:8918a71cdbe9 | 194 | |
Sergunb | 0:8918a71cdbe9 | 195 | //Send PPP frame |
Sergunb | 0:8918a71cdbe9 | 196 | error = pppSendFrame(context->interface, buffer, offset, protocol); |
Sergunb | 0:8918a71cdbe9 | 197 | |
Sergunb | 0:8918a71cdbe9 | 198 | //Free previously allocated memory block |
Sergunb | 0:8918a71cdbe9 | 199 | netBufferFree(buffer); |
Sergunb | 0:8918a71cdbe9 | 200 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 201 | return error; |
Sergunb | 0:8918a71cdbe9 | 202 | } |
Sergunb | 0:8918a71cdbe9 | 203 | |
Sergunb | 0:8918a71cdbe9 | 204 | |
Sergunb | 0:8918a71cdbe9 | 205 | /** |
Sergunb | 0:8918a71cdbe9 | 206 | * @brief Send Terminate-Ack packet |
Sergunb | 0:8918a71cdbe9 | 207 | * @param[in] context PPP context |
Sergunb | 0:8918a71cdbe9 | 208 | * @param[in] identifier Identifier field |
Sergunb | 0:8918a71cdbe9 | 209 | * @param[in] protocol Protocol field |
Sergunb | 0:8918a71cdbe9 | 210 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 211 | **/ |
Sergunb | 0:8918a71cdbe9 | 212 | |
Sergunb | 0:8918a71cdbe9 | 213 | error_t pppSendTerminateAck(PppContext *context, |
Sergunb | 0:8918a71cdbe9 | 214 | uint8_t identifier, PppProtocol protocol) |
Sergunb | 0:8918a71cdbe9 | 215 | { |
Sergunb | 0:8918a71cdbe9 | 216 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 217 | size_t length; |
Sergunb | 0:8918a71cdbe9 | 218 | size_t offset; |
Sergunb | 0:8918a71cdbe9 | 219 | NetBuffer *buffer; |
Sergunb | 0:8918a71cdbe9 | 220 | PppTerminatePacket *terminateAckPacket; |
Sergunb | 0:8918a71cdbe9 | 221 | |
Sergunb | 0:8918a71cdbe9 | 222 | //Length of the Terminate-Ack packet |
Sergunb | 0:8918a71cdbe9 | 223 | length = sizeof(PppTerminatePacket); |
Sergunb | 0:8918a71cdbe9 | 224 | |
Sergunb | 0:8918a71cdbe9 | 225 | //Allocate a buffer memory to hold the Terminate-Ack packet |
Sergunb | 0:8918a71cdbe9 | 226 | buffer = pppAllocBuffer(length, &offset); |
Sergunb | 0:8918a71cdbe9 | 227 | //Failed to allocate memory? |
Sergunb | 0:8918a71cdbe9 | 228 | if(buffer == NULL) |
Sergunb | 0:8918a71cdbe9 | 229 | return ERROR_OUT_OF_MEMORY; |
Sergunb | 0:8918a71cdbe9 | 230 | |
Sergunb | 0:8918a71cdbe9 | 231 | //Point to the Terminate-Ack packet |
Sergunb | 0:8918a71cdbe9 | 232 | terminateAckPacket = netBufferAt(buffer, offset); |
Sergunb | 0:8918a71cdbe9 | 233 | |
Sergunb | 0:8918a71cdbe9 | 234 | //Format packet header |
Sergunb | 0:8918a71cdbe9 | 235 | terminateAckPacket->code = PPP_CODE_TERMINATE_ACK; |
Sergunb | 0:8918a71cdbe9 | 236 | terminateAckPacket->identifier = identifier; |
Sergunb | 0:8918a71cdbe9 | 237 | terminateAckPacket->length = htons(length); |
Sergunb | 0:8918a71cdbe9 | 238 | |
Sergunb | 0:8918a71cdbe9 | 239 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 240 | TRACE_INFO("Sending Terminate-Ack packet (%" PRIuSIZE " bytes)...\r\n", length); |
Sergunb | 0:8918a71cdbe9 | 241 | //Dump packet contents for debugging purpose |
Sergunb | 0:8918a71cdbe9 | 242 | pppDumpPacket((PppPacket *) terminateAckPacket, length, protocol); |
Sergunb | 0:8918a71cdbe9 | 243 | |
Sergunb | 0:8918a71cdbe9 | 244 | //Send PPP frame |
Sergunb | 0:8918a71cdbe9 | 245 | error = pppSendFrame(context->interface, buffer, offset, protocol); |
Sergunb | 0:8918a71cdbe9 | 246 | |
Sergunb | 0:8918a71cdbe9 | 247 | //Free previously allocated memory block |
Sergunb | 0:8918a71cdbe9 | 248 | netBufferFree(buffer); |
Sergunb | 0:8918a71cdbe9 | 249 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 250 | return error; |
Sergunb | 0:8918a71cdbe9 | 251 | } |
Sergunb | 0:8918a71cdbe9 | 252 | |
Sergunb | 0:8918a71cdbe9 | 253 | |
Sergunb | 0:8918a71cdbe9 | 254 | /** |
Sergunb | 0:8918a71cdbe9 | 255 | * @brief Send Code-Reject packet |
Sergunb | 0:8918a71cdbe9 | 256 | * @param[in] context PPP context |
Sergunb | 0:8918a71cdbe9 | 257 | * @param[in] packet Un-interpretable packet received from the peer |
Sergunb | 0:8918a71cdbe9 | 258 | * @param[in] identifier Identifier field |
Sergunb | 0:8918a71cdbe9 | 259 | * @param[in] protocol Protocol field |
Sergunb | 0:8918a71cdbe9 | 260 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 261 | **/ |
Sergunb | 0:8918a71cdbe9 | 262 | |
Sergunb | 0:8918a71cdbe9 | 263 | error_t pppSendCodeRej(PppContext *context, const PppPacket *packet, |
Sergunb | 0:8918a71cdbe9 | 264 | uint8_t identifier, PppProtocol protocol) |
Sergunb | 0:8918a71cdbe9 | 265 | { |
Sergunb | 0:8918a71cdbe9 | 266 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 267 | size_t length; |
Sergunb | 0:8918a71cdbe9 | 268 | size_t offset; |
Sergunb | 0:8918a71cdbe9 | 269 | NetBuffer *buffer; |
Sergunb | 0:8918a71cdbe9 | 270 | PppCodeRejPacket *codeRejPacket; |
Sergunb | 0:8918a71cdbe9 | 271 | |
Sergunb | 0:8918a71cdbe9 | 272 | //Calculate the length of the Code-Reject packet |
Sergunb | 0:8918a71cdbe9 | 273 | length = ntohs(packet->length) + sizeof(PppCodeRejPacket); |
Sergunb | 0:8918a71cdbe9 | 274 | |
Sergunb | 0:8918a71cdbe9 | 275 | //The rejected packet must be truncated to comply with |
Sergunb | 0:8918a71cdbe9 | 276 | //the peer's established MRU |
Sergunb | 0:8918a71cdbe9 | 277 | length = MIN(length, context->peerConfig.mru); |
Sergunb | 0:8918a71cdbe9 | 278 | |
Sergunb | 0:8918a71cdbe9 | 279 | //Allocate a buffer memory to hold the Code-Reject packet |
Sergunb | 0:8918a71cdbe9 | 280 | buffer = pppAllocBuffer(sizeof(PppCodeRejPacket), &offset); |
Sergunb | 0:8918a71cdbe9 | 281 | //Failed to allocate memory? |
Sergunb | 0:8918a71cdbe9 | 282 | if(buffer == NULL) |
Sergunb | 0:8918a71cdbe9 | 283 | return ERROR_OUT_OF_MEMORY; |
Sergunb | 0:8918a71cdbe9 | 284 | |
Sergunb | 0:8918a71cdbe9 | 285 | //Point to the Code-Reject packet |
Sergunb | 0:8918a71cdbe9 | 286 | codeRejPacket = netBufferAt(buffer, offset); |
Sergunb | 0:8918a71cdbe9 | 287 | |
Sergunb | 0:8918a71cdbe9 | 288 | //Format packet header |
Sergunb | 0:8918a71cdbe9 | 289 | codeRejPacket->code = PPP_CODE_CODE_REJ; |
Sergunb | 0:8918a71cdbe9 | 290 | codeRejPacket->identifier = identifier; |
Sergunb | 0:8918a71cdbe9 | 291 | codeRejPacket->length = htons(length); |
Sergunb | 0:8918a71cdbe9 | 292 | |
Sergunb | 0:8918a71cdbe9 | 293 | //The Rejected-Packet field contains a copy of the packet which is being rejected |
Sergunb | 0:8918a71cdbe9 | 294 | error = netBufferAppend(buffer, packet, length - sizeof(PppCodeRejPacket)); |
Sergunb | 0:8918a71cdbe9 | 295 | |
Sergunb | 0:8918a71cdbe9 | 296 | //Check status code |
Sergunb | 0:8918a71cdbe9 | 297 | if(!error) |
Sergunb | 0:8918a71cdbe9 | 298 | { |
Sergunb | 0:8918a71cdbe9 | 299 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 300 | TRACE_INFO("Sending Code-Reject packet (%" PRIuSIZE " bytes)...\r\n", length); |
Sergunb | 0:8918a71cdbe9 | 301 | |
Sergunb | 0:8918a71cdbe9 | 302 | //Send PPP frame |
Sergunb | 0:8918a71cdbe9 | 303 | error = pppSendFrame(context->interface, buffer, offset, protocol); |
Sergunb | 0:8918a71cdbe9 | 304 | } |
Sergunb | 0:8918a71cdbe9 | 305 | |
Sergunb | 0:8918a71cdbe9 | 306 | //Free previously allocated memory block |
Sergunb | 0:8918a71cdbe9 | 307 | netBufferFree(buffer); |
Sergunb | 0:8918a71cdbe9 | 308 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 309 | return error; |
Sergunb | 0:8918a71cdbe9 | 310 | } |
Sergunb | 0:8918a71cdbe9 | 311 | |
Sergunb | 0:8918a71cdbe9 | 312 | |
Sergunb | 0:8918a71cdbe9 | 313 | /** |
Sergunb | 0:8918a71cdbe9 | 314 | * @brief Send Protocol-Reject packet |
Sergunb | 0:8918a71cdbe9 | 315 | * @param[in] context PPP context |
Sergunb | 0:8918a71cdbe9 | 316 | * @param[in] identifier Identifier field |
Sergunb | 0:8918a71cdbe9 | 317 | * @param[in] protocol Rejected protocol |
Sergunb | 0:8918a71cdbe9 | 318 | * @param[in] information Rejected information |
Sergunb | 0:8918a71cdbe9 | 319 | * @param[in] length Length of the rejected information |
Sergunb | 0:8918a71cdbe9 | 320 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 321 | **/ |
Sergunb | 0:8918a71cdbe9 | 322 | |
Sergunb | 0:8918a71cdbe9 | 323 | error_t pppSendProtocolRej(PppContext *context, uint8_t identifier, |
Sergunb | 0:8918a71cdbe9 | 324 | uint16_t protocol, const uint8_t *information, size_t length) |
Sergunb | 0:8918a71cdbe9 | 325 | { |
Sergunb | 0:8918a71cdbe9 | 326 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 327 | size_t offset; |
Sergunb | 0:8918a71cdbe9 | 328 | NetBuffer *buffer; |
Sergunb | 0:8918a71cdbe9 | 329 | PppProtocolRejPacket *protocolRejPacket; |
Sergunb | 0:8918a71cdbe9 | 330 | |
Sergunb | 0:8918a71cdbe9 | 331 | //Calculate the length of the Protocol-Reject packet |
Sergunb | 0:8918a71cdbe9 | 332 | length += sizeof(PppProtocolRejPacket); |
Sergunb | 0:8918a71cdbe9 | 333 | |
Sergunb | 0:8918a71cdbe9 | 334 | //The Rejected-Information must be truncated to comply with |
Sergunb | 0:8918a71cdbe9 | 335 | //the peer's established MRU |
Sergunb | 0:8918a71cdbe9 | 336 | length = MIN(length, context->peerConfig.mru); |
Sergunb | 0:8918a71cdbe9 | 337 | |
Sergunb | 0:8918a71cdbe9 | 338 | //Allocate a buffer memory to hold the Protocol-Reject packet |
Sergunb | 0:8918a71cdbe9 | 339 | buffer = pppAllocBuffer(sizeof(PppProtocolRejPacket), &offset); |
Sergunb | 0:8918a71cdbe9 | 340 | //Failed to allocate memory? |
Sergunb | 0:8918a71cdbe9 | 341 | if(buffer == NULL) |
Sergunb | 0:8918a71cdbe9 | 342 | return ERROR_OUT_OF_MEMORY; |
Sergunb | 0:8918a71cdbe9 | 343 | |
Sergunb | 0:8918a71cdbe9 | 344 | //Point to the Protocol-Reject packet |
Sergunb | 0:8918a71cdbe9 | 345 | protocolRejPacket = netBufferAt(buffer, offset); |
Sergunb | 0:8918a71cdbe9 | 346 | |
Sergunb | 0:8918a71cdbe9 | 347 | //Format packet header |
Sergunb | 0:8918a71cdbe9 | 348 | protocolRejPacket->code = PPP_CODE_PROTOCOL_REJ; |
Sergunb | 0:8918a71cdbe9 | 349 | protocolRejPacket->identifier = identifier; |
Sergunb | 0:8918a71cdbe9 | 350 | protocolRejPacket->length = htons(length); |
Sergunb | 0:8918a71cdbe9 | 351 | protocolRejPacket->rejectedProtocol = htons(protocol); |
Sergunb | 0:8918a71cdbe9 | 352 | |
Sergunb | 0:8918a71cdbe9 | 353 | //The Rejected-Information field contains a copy of the |
Sergunb | 0:8918a71cdbe9 | 354 | //packet which is being rejected |
Sergunb | 0:8918a71cdbe9 | 355 | error = netBufferAppend(buffer, information, |
Sergunb | 0:8918a71cdbe9 | 356 | length - sizeof(PppProtocolRejPacket)); |
Sergunb | 0:8918a71cdbe9 | 357 | |
Sergunb | 0:8918a71cdbe9 | 358 | //Check status code |
Sergunb | 0:8918a71cdbe9 | 359 | if(!error) |
Sergunb | 0:8918a71cdbe9 | 360 | { |
Sergunb | 0:8918a71cdbe9 | 361 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 362 | TRACE_INFO("Sending Protocol-Reject packet (%" PRIuSIZE " bytes)...\r\n", length); |
Sergunb | 0:8918a71cdbe9 | 363 | |
Sergunb | 0:8918a71cdbe9 | 364 | //Send PPP frame |
Sergunb | 0:8918a71cdbe9 | 365 | error = pppSendFrame(context->interface, buffer, offset, PPP_PROTOCOL_LCP); |
Sergunb | 0:8918a71cdbe9 | 366 | } |
Sergunb | 0:8918a71cdbe9 | 367 | |
Sergunb | 0:8918a71cdbe9 | 368 | //Free previously allocated memory block |
Sergunb | 0:8918a71cdbe9 | 369 | netBufferFree(buffer); |
Sergunb | 0:8918a71cdbe9 | 370 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 371 | return error; |
Sergunb | 0:8918a71cdbe9 | 372 | } |
Sergunb | 0:8918a71cdbe9 | 373 | |
Sergunb | 0:8918a71cdbe9 | 374 | |
Sergunb | 0:8918a71cdbe9 | 375 | /** |
Sergunb | 0:8918a71cdbe9 | 376 | * @brief Send Echo-Reply packet |
Sergunb | 0:8918a71cdbe9 | 377 | * @param[in] context PPP context |
Sergunb | 0:8918a71cdbe9 | 378 | * @param[in] echoReqPacket Echo-Request packet received from the peer |
Sergunb | 0:8918a71cdbe9 | 379 | * @param[in] protocol Protocol field |
Sergunb | 0:8918a71cdbe9 | 380 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 381 | **/ |
Sergunb | 0:8918a71cdbe9 | 382 | |
Sergunb | 0:8918a71cdbe9 | 383 | error_t pppSendEchoRep(PppContext *context, |
Sergunb | 0:8918a71cdbe9 | 384 | const PppEchoPacket *echoReqPacket, PppProtocol protocol) |
Sergunb | 0:8918a71cdbe9 | 385 | { |
Sergunb | 0:8918a71cdbe9 | 386 | error_t error; |
Sergunb | 0:8918a71cdbe9 | 387 | size_t length; |
Sergunb | 0:8918a71cdbe9 | 388 | size_t offset; |
Sergunb | 0:8918a71cdbe9 | 389 | NetBuffer *buffer; |
Sergunb | 0:8918a71cdbe9 | 390 | PppEchoPacket *echoRepPacket; |
Sergunb | 0:8918a71cdbe9 | 391 | |
Sergunb | 0:8918a71cdbe9 | 392 | //Retrieve the length of the Echo-Request packet |
Sergunb | 0:8918a71cdbe9 | 393 | length = ntohs(echoReqPacket->length); |
Sergunb | 0:8918a71cdbe9 | 394 | |
Sergunb | 0:8918a71cdbe9 | 395 | //Make sure the length is valid |
Sergunb | 0:8918a71cdbe9 | 396 | if(length < sizeof(PppEchoPacket)) |
Sergunb | 0:8918a71cdbe9 | 397 | return ERROR_INVALID_LENGTH; |
Sergunb | 0:8918a71cdbe9 | 398 | if(length > context->peerConfig.mru) |
Sergunb | 0:8918a71cdbe9 | 399 | return ERROR_INVALID_LENGTH; |
Sergunb | 0:8918a71cdbe9 | 400 | |
Sergunb | 0:8918a71cdbe9 | 401 | //Allocate a buffer memory to hold the Echo-Reply packet |
Sergunb | 0:8918a71cdbe9 | 402 | buffer = pppAllocBuffer(sizeof(PppEchoPacket), &offset); |
Sergunb | 0:8918a71cdbe9 | 403 | //Failed to allocate memory? |
Sergunb | 0:8918a71cdbe9 | 404 | if(buffer == NULL) |
Sergunb | 0:8918a71cdbe9 | 405 | return ERROR_OUT_OF_MEMORY; |
Sergunb | 0:8918a71cdbe9 | 406 | |
Sergunb | 0:8918a71cdbe9 | 407 | //Point to the Echo-Reply packet |
Sergunb | 0:8918a71cdbe9 | 408 | echoRepPacket = netBufferAt(buffer, offset); |
Sergunb | 0:8918a71cdbe9 | 409 | |
Sergunb | 0:8918a71cdbe9 | 410 | //Format packet header |
Sergunb | 0:8918a71cdbe9 | 411 | echoRepPacket->code = PPP_CODE_ECHO_REP; |
Sergunb | 0:8918a71cdbe9 | 412 | echoRepPacket->identifier = echoReqPacket->identifier; |
Sergunb | 0:8918a71cdbe9 | 413 | echoRepPacket->length = htons(length); |
Sergunb | 0:8918a71cdbe9 | 414 | echoRepPacket->magicNumber = context->localConfig.magicNumber; |
Sergunb | 0:8918a71cdbe9 | 415 | |
Sergunb | 0:8918a71cdbe9 | 416 | //The data field of the Echo-Request packet is copied into the data |
Sergunb | 0:8918a71cdbe9 | 417 | //field of the Echo-Reply packet |
Sergunb | 0:8918a71cdbe9 | 418 | error = netBufferAppend(buffer, echoReqPacket->data, length - sizeof(PppEchoPacket)); |
Sergunb | 0:8918a71cdbe9 | 419 | |
Sergunb | 0:8918a71cdbe9 | 420 | //Check status code |
Sergunb | 0:8918a71cdbe9 | 421 | if(!error) |
Sergunb | 0:8918a71cdbe9 | 422 | { |
Sergunb | 0:8918a71cdbe9 | 423 | //Debug message |
Sergunb | 0:8918a71cdbe9 | 424 | TRACE_INFO("Sending Echo-Reply packet (%" PRIuSIZE " bytes)...\r\n", length); |
Sergunb | 0:8918a71cdbe9 | 425 | |
Sergunb | 0:8918a71cdbe9 | 426 | //Send PPP frame |
Sergunb | 0:8918a71cdbe9 | 427 | error = pppSendFrame(context->interface, buffer, offset, protocol); |
Sergunb | 0:8918a71cdbe9 | 428 | } |
Sergunb | 0:8918a71cdbe9 | 429 | |
Sergunb | 0:8918a71cdbe9 | 430 | //Free previously allocated memory block |
Sergunb | 0:8918a71cdbe9 | 431 | netBufferFree(buffer); |
Sergunb | 0:8918a71cdbe9 | 432 | //Return status code |
Sergunb | 0:8918a71cdbe9 | 433 | return error; |
Sergunb | 0:8918a71cdbe9 | 434 | } |
Sergunb | 0:8918a71cdbe9 | 435 | |
Sergunb | 0:8918a71cdbe9 | 436 | |
Sergunb | 0:8918a71cdbe9 | 437 | /** |
Sergunb | 0:8918a71cdbe9 | 438 | * @brief Add an option to a Configure packet |
Sergunb | 0:8918a71cdbe9 | 439 | * @param[in,out] packet Pointer to the Configure packet |
Sergunb | 0:8918a71cdbe9 | 440 | * @param[in] optionType Option type |
Sergunb | 0:8918a71cdbe9 | 441 | * @param[in] optionValue Option value |
Sergunb | 0:8918a71cdbe9 | 442 | * @param[in] optionLen Length of the option value |
Sergunb | 0:8918a71cdbe9 | 443 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 444 | **/ |
Sergunb | 0:8918a71cdbe9 | 445 | |
Sergunb | 0:8918a71cdbe9 | 446 | error_t pppAddOption(PppConfigurePacket *packet, uint8_t optionType, |
Sergunb | 0:8918a71cdbe9 | 447 | const void *optionValue, uint8_t optionLen) |
Sergunb | 0:8918a71cdbe9 | 448 | { |
Sergunb | 0:8918a71cdbe9 | 449 | PppOption *option; |
Sergunb | 0:8918a71cdbe9 | 450 | |
Sergunb | 0:8918a71cdbe9 | 451 | //Make sure the length is valid |
Sergunb | 0:8918a71cdbe9 | 452 | if(optionLen > (UINT8_MAX - sizeof(PppOption))) |
Sergunb | 0:8918a71cdbe9 | 453 | return ERROR_INVALID_LENGTH; |
Sergunb | 0:8918a71cdbe9 | 454 | |
Sergunb | 0:8918a71cdbe9 | 455 | //Point to the end of the Configure packet |
Sergunb | 0:8918a71cdbe9 | 456 | option = (PppOption *) ((uint8_t *) packet + packet->length); |
Sergunb | 0:8918a71cdbe9 | 457 | |
Sergunb | 0:8918a71cdbe9 | 458 | //Write specified option at current location |
Sergunb | 0:8918a71cdbe9 | 459 | option->type = optionType; |
Sergunb | 0:8918a71cdbe9 | 460 | option->length = optionLen + sizeof(PppOption); |
Sergunb | 0:8918a71cdbe9 | 461 | //Copy option data |
Sergunb | 0:8918a71cdbe9 | 462 | memcpy(option->data, optionValue, optionLen); |
Sergunb | 0:8918a71cdbe9 | 463 | |
Sergunb | 0:8918a71cdbe9 | 464 | //Update the length of the Configure packet |
Sergunb | 0:8918a71cdbe9 | 465 | packet->length += optionLen + sizeof(PppOption); |
Sergunb | 0:8918a71cdbe9 | 466 | |
Sergunb | 0:8918a71cdbe9 | 467 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 468 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 469 | } |
Sergunb | 0:8918a71cdbe9 | 470 | |
Sergunb | 0:8918a71cdbe9 | 471 | #endif |
Sergunb | 0:8918a71cdbe9 | 472 |