Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

eap_archie_parser.hxx

00001 /* BEGIN_COPYRIGHT                                                        */
00002 /*                                                                        */
00003 /* Open Diameter: Open-source software for the Diameter and               */
00004 /*                Diameter related protocols                              */
00005 /*                                                                        */
00006 /* Copyright (C) 2002-2004 Open Diameter Project                          */
00007 /*                                                                        */
00008 /* This library is free software; you can redistribute it and/or modify   */
00009 /* it under the terms of the GNU Lesser General Public License as         */
00010 /* published by the Free Software Foundation; either version 2.1 of the   */
00011 /* License, or (at your option) any later version.                        */
00012 /*                                                                        */
00013 /* This library is distributed in the hope that it will be useful,        */
00014 /* but WITHOUT ANY WARRANTY; without even the implied warranty of         */
00015 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      */
00016 /* Lesser General Public License for more details.                        */
00017 /*                                                                        */
00018 /* You should have received a copy of the GNU Lesser General Public       */
00019 /* License along with this library; if not, write to the Free Software    */
00020 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307    */
00021 /* USA.                                                                   */
00022 /*                                                                        */
00023 /* In addition, when you copy and redistribute some or the entire part of */
00024 /* the source code of this software with or without modification, you     */
00025 /* MUST include this copyright notice in each copy.                       */
00026 /*                                                                        */
00027 /* If you make any changes that are appeared to be useful, please send    */
00028 /* sources that include the changed part to                               */
00029 /* diameter-developers@lists.sourceforge.net so that we can reflect your  */
00030 /* changes to one unified version of this software.                       */
00031 /*                                                                        */
00032 /* END_COPYRIGHT                                                          */
00033 
00034 // Archie parser definition.
00035 // Written by Yoshihiro Ohba (yohba@tari.toshiba.com)
00036 
00037 #ifndef  __EAP_ARCHIE_PARSER_H__
00038 #define  __EAP_ARCHIE_PARSER_H__
00039 
00040 #include "eap_archie.hxx"
00041 #include "eap_parser.hxx"
00042 #include "eap_log.hxx"
00043 
00045 typedef AAAParser<AAAMessageBlock*, EapRequestArchie*> 
00046 EapRequestArchieParser;
00047 
00048 inline void
00049 copyWithPadding(AAAMessageBlock *msg, const char *data, 
00050                 int dataLength, int blockSize)
00051 {
00052   msg->copy(data, dataLength ? dataLength : blockSize);
00053   if (dataLength)
00054     {
00055       ACE_OS::memset(msg->wr_ptr(), 0, blockSize - dataLength);
00056       msg->wr_ptr(blockSize - dataLength);
00057     }
00058 }
00059 
00064 inline void
00065 EapRequestArchieParser::parseRawToApp() 
00066 {
00067   AAAMessageBlock* msg = getRawData();
00068   EapRequestArchie* request = getAppData();
00069   // Read msgID.
00070   request->MsgID() = *(ACE_Byte*)msg->rd_ptr();
00071   msg->rd_ptr(1);
00072   return;
00073 }
00074 
00080 //void EapRequestArchieParser::parseAppToRaw();
00081 inline void
00082 EapRequestArchieParser::parseAppToRaw() 
00083 {
00084   AAAMessageBlock* msg = getRawData();
00085   EapRequestArchie* request = getAppData();
00086 
00087   // Write msgID.
00088   *(ACE_Byte*)msg->wr_ptr() = request->MsgID();
00089   msg->wr_ptr(1);
00090   return;
00091 }
00092 
00093 
00095 typedef AAAParser<AAAMessageBlock*, EapRequestArchieRequest*> 
00096 EapRequestArchieRequestParser;
00097 
00102 inline void 
00103 EapRequestArchieRequestParser::parseRawToApp()
00104 {
00105   AAAMessageBlock* msg = getRawData();
00106   EapRequestArchieRequest* request = getAppData();
00107 
00108   ACE_Byte msgId = *(ACE_Byte*)msg->rd_ptr();
00109   // Read msgID.
00110   if (msgId != 1)
00111     {
00112       EAP_LOG(LM_ERROR, "Invalid msgID %d (1 is expected).\n", msgId);
00113       throw -1;
00114     }
00115 
00116   msg->rd_ptr(2);  
00117   
00118   // Read NaiLength.
00119   ACE_Byte& naiLength = *(ACE_Byte*)msg->rd_ptr();
00120   msg->rd_ptr(1);
00121 
00122   // Read AuthID.
00123   request->AuthID() = std::string(msg->rd_ptr(), naiLength ? naiLength : 256);
00124   msg->rd_ptr(256);
00125 
00126   // Read Session ID.
00127   request->SessionID() = std::string(msg->rd_ptr(), 32);
00128   msg->rd_ptr(32);
00129 }
00130 
00136 //void EapRequestArchieRequestParser::parseAppToRaw();
00137 inline void 
00138 EapRequestArchieRequestParser::parseAppToRaw()
00139 {
00140   AAAMessageBlock* msg = getRawData();
00141   EapRequestArchieRequest* request = getAppData();
00142   
00143   // Write type field
00144   EapRequestParser requestParser;
00145   requestParser.setRawData(msg);
00146   requestParser.setAppData(request);
00147   requestParser.parseAppToRaw();     
00148 
00149   // Write msgID.
00150   *(ACE_Byte*)msg->wr_ptr() = request->MsgID();
00151   msg->wr_ptr(1);
00152 
00153   *msg->wr_ptr() = 0;  msg->wr_ptr(1);  // Skip Reserved field.
00154 
00155   // Check AuthID length.
00156   int length = request->AuthID().size();
00157   if (length == 0)
00158     {
00159       EAP_LOG(LM_ERROR, "AuthID is empty.");
00160       throw -1;
00161     }
00162   ACE_Byte naiLength = (ACE_Byte)length;
00163 
00164   *(ACE_Byte*)msg->wr_ptr() = naiLength;
00165   msg->wr_ptr(1);
00166 
00167   copyWithPadding(msg, request->AuthID().data(), naiLength, 256);
00168 
00169   msg->copy(request->SessionID().data(), 32);
00170 }
00171 
00173 typedef AAAParser<AAAMessageBlock*, EapResponseArchieResponse*> 
00174 EapResponseArchieResponseParser;
00175 
00180 inline void 
00181 EapResponseArchieResponseParser::parseRawToApp()
00182 {
00183   AAAMessageBlock* msg = getRawData();
00184   EapResponseArchieResponse* response = getAppData();
00185 
00186   // Read msgID.
00187   if (*(ACE_Byte*)msg->rd_ptr() != 2)
00188     {
00189       EAP_LOG(LM_ERROR, "Invalid msgID.");
00190       throw -1;
00191     }
00192 
00193   msg->rd_ptr(2);
00194 
00195   // Read NaiLength.
00196   ACE_Byte& naiLength = *(ACE_Byte*)msg->rd_ptr();
00197   msg->rd_ptr(1);
00198 
00199   // Read session id.
00200   response->SessionID() = std::string(msg->rd_ptr(), 32);
00201   msg->rd_ptr(32);
00202 
00203   // Read peer id.
00204   response->PeerID() = std::string(msg->rd_ptr(), naiLength ? naiLength : 256);
00205   msg->rd_ptr(256);
00206 
00207   // Read nonceP.
00208   response->NonceP() = std::string(msg->rd_ptr(), 40);
00209   msg->rd_ptr(40);
00210 
00211   // Read binding.
00212   response->Binding().bType = ntohs(*(ACE_UINT16*)msg->rd_ptr());
00213   msg->rd_ptr(2);
00214 
00215   ACE_Byte& sLength = *(ACE_Byte*)msg->rd_ptr();
00216   msg->rd_ptr(1);
00217 
00218   ACE_Byte& pLength = *(ACE_Byte*)msg->rd_ptr();
00219   msg->rd_ptr(1);
00220 
00221   response->Binding().addrS = 
00222     std::string(msg->rd_ptr(), sLength ? sLength : 256);
00223   msg->rd_ptr(256);
00224 
00225   response->Binding().addrP = 
00226     std::string(msg->rd_ptr(), pLength ? pLength : 256);
00227   msg->rd_ptr(256);
00228 
00229   // Read MAC1
00230   response->Mac1() = std::string(msg->rd_ptr(), 12);
00231   msg->rd_ptr(12);
00232 }
00233 
00239 //void EapResponseArchieResponseParser::parseAppToRaw();
00240 inline void 
00241 EapResponseArchieResponseParser::parseAppToRaw()
00242 {
00243   AAAMessageBlock* msg = getRawData();
00244   EapResponseArchieResponse* response = getAppData();
00245 
00246   // Write type field
00247   EapResponseParser responseParser;
00248   responseParser.setRawData(msg);
00249   responseParser.setAppData(response);
00250   responseParser.parseAppToRaw();     
00251 
00252   // Write msgID.
00253   *(ACE_Byte*)msg->wr_ptr() = response->MsgID();
00254   msg->wr_ptr(1);
00255 
00256   *msg->wr_ptr() = 0;  msg->wr_ptr(1);  // Skip Reserved field.
00257 
00258   // Check PeerID length.
00259   int length = response->PeerID().size();
00260   if (length == 0)
00261     {
00262       EAP_LOG(LM_ERROR, "PeerID is empty.");
00263       throw -1;
00264     }
00265   ACE_Byte naiLength = (ACE_Byte)length;
00266 
00267   *(ACE_Byte*)msg->wr_ptr() = naiLength;
00268   msg->wr_ptr(1);
00269 
00270   // Write session id.
00271   msg->copy(response->SessionID().data(), 32);
00272 
00273   // Write peer id.
00274   copyWithPadding(msg, response->PeerID().data(), naiLength, 256);
00275 
00276   // Write nonceP.
00277   msg->copy(response->NonceP().data(), 40);
00278 
00279   // Write binding.
00280   *(ACE_UINT16*)msg->wr_ptr() = ntohs(response->Binding().bType);
00281   msg->wr_ptr(2);
00282 
00283   ACE_Byte sLength = (ACE_Byte)response->Binding().addrS.size();
00284   *msg->wr_ptr() = sLength;
00285   msg->wr_ptr(1);
00286 
00287   ACE_Byte pLength = (ACE_Byte)response->Binding().addrP.size();
00288   *msg->wr_ptr() = pLength;
00289   msg->wr_ptr(1);
00290 
00291   copyWithPadding(msg, response->Binding().addrS.data(), sLength, 256);
00292   copyWithPadding(msg, response->Binding().addrP.data(), pLength, 256);
00293 
00294   // Write MAC1.
00295   msg->copy(response->Mac1().data(), 12);
00296 }
00297 
00299 typedef AAAParser<AAAMessageBlock*, EapRequestArchieConfirm*> 
00300 EapRequestArchieConfirmParser;
00301 
00306 inline void 
00307 EapRequestArchieConfirmParser::parseRawToApp()
00308 {
00309   AAAMessageBlock* msg = getRawData();
00310   EapRequestArchieConfirm* confirm = getAppData();
00311 
00312   // Read msgID.
00313   if (*(ACE_Byte*)msg->rd_ptr() != 3)
00314     {
00315       EAP_LOG(LM_ERROR, "Invalid msgID.");
00316       throw -1;
00317     }
00318 
00319   msg->rd_ptr(3);  
00320 
00321   // Read session id.
00322   confirm->SessionID() = std::string(msg->rd_ptr(), 32);
00323   msg->rd_ptr(32);
00324 
00325   // Read nonceA.
00326   confirm->NonceA() = std::string(msg->rd_ptr(), 40);
00327   msg->rd_ptr(40);
00328 
00329   // Read binding.
00330   confirm->Binding().bType = ntohs(*(ACE_UINT16*)msg->rd_ptr());
00331   msg->rd_ptr(2);
00332 
00333   ACE_Byte& sLength = *(ACE_Byte*)msg->rd_ptr();
00334   msg->rd_ptr(1);
00335 
00336   ACE_Byte& pLength = *(ACE_Byte*)msg->rd_ptr();
00337   msg->rd_ptr(1);
00338 
00339   confirm->Binding().addrS = 
00340     std::string(msg->rd_ptr(), sLength ? sLength : 256);
00341   msg->rd_ptr(256);
00342 
00343   confirm->Binding().addrP = 
00344     std::string(msg->rd_ptr(), pLength ? pLength : 256);
00345   msg->rd_ptr(256);
00346 
00347   // Read MAC2
00348   confirm->Mac2() = std::string(msg->rd_ptr(), 12);
00349   msg->rd_ptr(12);
00350 }
00351 
00357 inline void 
00358 EapRequestArchieConfirmParser::parseAppToRaw()
00359 {
00360   AAAMessageBlock* msg = getRawData();
00361   EapRequestArchieConfirm* confirm = getAppData();
00362 
00363   // Write type field
00364   EapRequestParser requestParser;
00365   requestParser.setRawData(msg);
00366   requestParser.setAppData(confirm);
00367   requestParser.parseAppToRaw();     
00368 
00369   // Write msgID.
00370   *(ACE_Byte*)msg->wr_ptr() = confirm->MsgID();
00371   msg->wr_ptr(1);
00372 
00373   *msg->wr_ptr() = 0;  msg->wr_ptr(1);  // Skip Reserved field.
00374   *msg->wr_ptr() = 0;  msg->wr_ptr(1);  // Skip Reserved field.
00375 
00376   // Write session id.
00377   msg->copy(confirm->SessionID().data(), 32);
00378 
00379   // Write nonceA.
00380   msg->copy(confirm->NonceA().data(), 40);
00381 
00382   // Write binding.
00383   *(ACE_UINT16*)msg->wr_ptr() = ntohs(confirm->Binding().bType);
00384   msg->wr_ptr(2);
00385 
00386   ACE_Byte sLength = (ACE_Byte)confirm->Binding().addrS.size();
00387   *msg->wr_ptr() = sLength;
00388   msg->wr_ptr(1);
00389 
00390   ACE_Byte pLength = (ACE_Byte)confirm->Binding().addrP.size();
00391   *msg->wr_ptr() = pLength;
00392   msg->wr_ptr(1);
00393 
00394   copyWithPadding(msg, confirm->Binding().addrS.data(), sLength, 256);
00395   copyWithPadding(msg, confirm->Binding().addrP.data(), pLength, 256);
00396 
00397   // Write MAC2
00398   msg->copy(confirm->Mac2().data(), 12);
00399 }
00400 
00402 typedef AAAParser<AAAMessageBlock*, EapResponseArchieFinish*> 
00403 EapResponseArchieFinishParser;
00404 
00409 inline void 
00410 EapResponseArchieFinishParser::parseRawToApp()
00411 {
00412   AAAMessageBlock* msg = getRawData();
00413   EapResponseArchieFinish* finish = getAppData();
00414 
00415   // Read msgID.
00416   if (*(ACE_Byte*)msg->rd_ptr() != 4)
00417     {
00418       EAP_LOG(LM_ERROR, "Invalid msgID.");
00419       throw -1;
00420     }
00421 
00422   msg->rd_ptr(3);  
00423 
00424   // Read session id.
00425   finish->SessionID() = std::string(msg->rd_ptr(), 32);
00426   msg->rd_ptr(32);
00427 
00428   // Read MAC3
00429   finish->Mac3() = std::string(msg->rd_ptr(), 12);
00430   msg->rd_ptr(12);
00431 }
00432 
00438 inline void 
00439 EapResponseArchieFinishParser::parseAppToRaw()
00440 {
00441   AAAMessageBlock* msg = getRawData();
00442   EapResponseArchieFinish* finish = getAppData();
00443 
00444   // Write type field
00445   EapResponseParser responseParser;
00446   responseParser.setRawData(msg);
00447   responseParser.setAppData(finish);
00448   responseParser.parseAppToRaw();     
00449 
00450   // Write msgID.
00451   *(ACE_Byte*)msg->wr_ptr() = finish->MsgID();
00452   msg->wr_ptr(1);
00453 
00454   *msg->wr_ptr() = 0;  msg->wr_ptr(1);  // Skip Reserved field.
00455   *msg->wr_ptr() = 0;  msg->wr_ptr(1);  // Skip Reserved field.
00456 
00457   // Write session id.
00458   msg->copy(finish->SessionID().data(), 32);
00459 
00460   // Write MAC3
00461   msg->copy(finish->Mac3().data(), 12);
00462 }
00463 
00464 #endif // __EAP_ARCHIE_PARSER_H__

Generated on Fri Jun 25 19:16:16 2004 for EAP State Machine by doxygen 1.3.5