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

CSocket.cpp

Go to the documentation of this file.
00001 /*
00002                     GNU GENERAL PUBLIC LICENSE
00003                        Version 2, June 1991
00004 
00005  Copyright (C) 1989, 1991 Free Software Foundation, Inc.
00006                        59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00007  Everyone is permitted to copy and distribute verbatim copies
00008  of this license document, but changing it is not allowed.
00009 
00010                             Preamble
00011 
00012   The licenses for most software are designed to take away your
00013 freedom to share and change it.  By contrast, the GNU General Public
00014 License is intended to guarantee your freedom to share and change free
00015 software--to make sure the software is free for all its users.  This
00016 General Public License applies to most of the Free Software
00017 Foundation's software and to any other program whose authors commit to
00018 using it.  (Some other Free Software Foundation software is covered by
00019 the GNU Library General Public License instead.)  You can apply it to
00020 your programs, too.
00021 
00022   When we speak of free software, we are referring to freedom, not
00023 price.  Our General Public Licenses are designed to make sure that you
00024 have the freedom to distribute copies of free software (and charge for
00025 this service if you wish), that you receive source code or can get it
00026 if you want it, that you can change the software or use pieces of it
00027 in new free programs; and that you know you can do these things.
00028 
00029   To protect your rights, we need to make restrictions that forbid
00030 anyone to deny you these rights or to ask you to surrender the rights.
00031 These restrictions translate to certain responsibilities for you if you
00032 distribute copies of the software, or if you modify it.
00033 
00034   For example, if you distribute copies of such a program, whether
00035 gratis or for a fee, you must give the recipients all the rights that
00036 you have.  You must make sure that they, too, receive or can get the
00037 source code.  And you must show them these terms so they know their
00038 rights.
00039 
00040   We protect your rights with two steps: (1) copyright the software, and
00041 (2) offer you this license which gives you legal permission to copy,
00042 distribute and/or modify the software.
00043 
00044   Also, for each author's protection and ours, we want to make certain
00045 that everyone understands that there is no warranty for this free
00046 software.  If the software is modified by someone else and passed on, we
00047 want its recipients to know that what they have is not the original, so
00048 that any problems introduced by others will not reflect on the original
00049 authors' reputations.
00050 
00051   Finally, any free program is threatened constantly by software
00052 patents.  We wish to avoid the danger that redistributors of a free
00053 program will individually obtain patent licenses, in effect making the
00054 program proprietary.  To prevent this, we have made it clear that any
00055 patent must be licensed for everyone's free use or not licensed at all.
00056 
00057   The precise terms and conditions for copying, distribution and
00058 modification follow.
00059 
00060                     GNU GENERAL PUBLIC LICENSE
00061    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
00062 
00063   0. This License applies to any program or other work which contains
00064 a notice placed by the copyright holder saying it may be distributed
00065 under the terms of this General Public License.  The "Program", below,
00066 refers to any such program or work, and a "work based on the Program"
00067 means either the Program or any derivative work under copyright law:
00068 that is to say, a work containing the Program or a portion of it,
00069 either verbatim or with modifications and/or translated into another
00070 language.  (Hereinafter, translation is included without limitation in
00071 the term "modification".)  Each licensee is addressed as "you".
00072 
00073 Activities other than copying, distribution and modification are not
00074 covered by this License; they are outside its scope.  The act of
00075 running the Program is not restricted, and the output from the Program
00076 is covered only if its contents constitute a work based on the
00077 Program (independent of having been made by running the Program).
00078 Whether that is true depends on what the Program does.
00079 
00080   1. You may copy and distribute verbatim copies of the Program's
00081 source code as you receive it, in any medium, provided that you
00082 conspicuously and appropriately publish on each copy an appropriate
00083 copyright notice and disclaimer of warranty; keep intact all the
00084 notices that refer to this License and to the absence of any warranty;
00085 and give any other recipients of the Program a copy of this License
00086 along with the Program.
00087 
00088 You may charge a fee for the physical act of transferring a copy, and
00089 you may at your option offer warranty protection in exchange for a fee.
00090 
00091   2. You may modify your copy or copies of the Program or any portion
00092 of it, thus forming a work based on the Program, and copy and
00093 distribute such modifications or work under the terms of Section 1
00094 above, provided that you also meet all of these conditions:
00095 
00096     a) You must cause the modified files to carry prominent notices
00097     stating that you changed the files and the date of any change.
00098 
00099     b) You must cause any work that you distribute or publish, that in
00100     whole or in part contains or is derived from the Program or any
00101     part thereof, to be licensed as a whole at no charge to all third
00102     parties under the terms of this License.
00103 
00104     c) If the modified program normally reads commands interactively
00105     when run, you must cause it, when started running for such
00106     interactive use in the most ordinary way, to print or display an
00107     announcement including an appropriate copyright notice and a
00108     notice that there is no warranty (or else, saying that you provide
00109     a warranty) and that users may redistribute the program under
00110     these conditions, and telling the user how to view a copy of this
00111     License.  (Exception: if the Program itself is interactive but
00112     does not normally print such an announcement, your work based on
00113     the Program is not required to print an announcement.)
00114 
00115 These requirements apply to the modified work as a whole.  If
00116 identifiable sections of that work are not derived from the Program,
00117 and can be reasonably considered independent and separate works in
00118 themselves, then this License, and its terms, do not apply to those
00119 sections when you distribute them as separate works.  But when you
00120 distribute the same sections as part of a whole which is a work based
00121 on the Program, the distribution of the whole must be on the terms of
00122 this License, whose permissions for other licensees extend to the
00123 entire whole, and thus to each and every part regardless of who wrote it.
00124 
00125 Thus, it is not the intent of this section to claim rights or contest
00126 your rights to work written entirely by you; rather, the intent is to
00127 exercise the right to control the distribution of derivative or
00128 collective works based on the Program.
00129 
00130 In addition, mere aggregation of another work not based on the Program
00131 with the Program (or with a work based on the Program) on a volume of
00132 a storage or distribution medium does not bring the other work under
00133 the scope of this License.
00134 
00135   3. You may copy and distribute the Program (or a work based on it,
00136 under Section 2) in object code or executable form under the terms of
00137 Sections 1 and 2 above provided that you also do one of the following:
00138 
00139     a) Accompany it with the complete corresponding machine-readable
00140     source code, which must be distributed under the terms of Sections
00141     1 and 2 above on a medium customarily used for software interchange; or,
00142 
00143     b) Accompany it with a written offer, valid for at least three
00144     years, to give any third party, for a charge no more than your
00145     cost of physically performing source distribution, a complete
00146     machine-readable copy of the corresponding source code, to be
00147     distributed under the terms of Sections 1 and 2 above on a medium
00148     customarily used for software interchange; or,
00149 
00150     c) Accompany it with the information you received as to the offer
00151     to distribute corresponding source code.  (This alternative is
00152     allowed only for noncommercial distribution and only if you
00153     received the program in object code or executable form with such
00154     an offer, in accord with Subsection b above.)
00155 
00156 The source code for a work means the preferred form of the work for
00157 making modifications to it.  For an executable work, complete source
00158 code means all the source code for all modules it contains, plus any
00159 associated interface definition files, plus the scripts used to
00160 control compilation and installation of the executable.  However, as a
00161 special exception, the source code distributed need not include
00162 anything that is normally distributed (in either source or binary
00163 form) with the major components (compiler, kernel, and so on) of the
00164 operating system on which the executable runs, unless that component
00165 itself accompanies the executable.
00166 
00167 If distribution of executable or object code is made by offering
00168 access to copy from a designated place, then offering equivalent
00169 access to copy the source code from the same place counts as
00170 distribution of the source code, even though third parties are not
00171 compelled to copy the source along with the object code.
00172 
00173   4. You may not copy, modify, sublicense, or distribute the Program
00174 except as expressly provided under this License.  Any attempt
00175 otherwise to copy, modify, sublicense or distribute the Program is
00176 void, and will automatically terminate your rights under this License.
00177 However, parties who have received copies, or rights, from you under
00178 this License will not have their licenses terminated so long as such
00179 parties remain in full compliance.
00180 
00181   5. You are not required to accept this License, since you have not
00182 signed it.  However, nothing else grants you permission to modify or
00183 distribute the Program or its derivative works.  These actions are
00184 prohibited by law if you do not accept this License.  Therefore, by
00185 modifying or distributing the Program (or any work based on the
00186 Program), you indicate your acceptance of this License to do so, and
00187 all its terms and conditions for copying, distributing or modifying
00188 the Program or works based on it.
00189 
00190   6. Each time you redistribute the Program (or any work based on the
00191 Program), the recipient automatically receives a license from the
00192 original licensor to copy, distribute or modify the Program subject to
00193 these terms and conditions.  You may not impose any further
00194 restrictions on the recipients' exercise of the rights granted herein.
00195 You are not responsible for enforcing compliance by third parties to
00196 this License.
00197 
00198   7. If, as a consequence of a court judgment or allegation of patent
00199 infringement or for any other reason (not limited to patent issues),
00200 conditions are imposed on you (whether by court order, agreement or
00201 otherwise) that contradict the conditions of this License, they do not
00202 excuse you from the conditions of this License.  If you cannot
00203 distribute so as to satisfy simultaneously your obligations under this
00204 License and any other pertinent obligations, then as a consequence you
00205 may not distribute the Program at all.  For example, if a patent
00206 license would not permit royalty-free redistribution of the Program by
00207 all those who receive copies directly or indirectly through you, then
00208 the only way you could satisfy both it and this License would be to
00209 refrain entirely from distribution of the Program.
00210 
00211 If any portion of this section is held invalid or unenforceable under
00212 any particular circumstance, the balance of the section is intended to
00213 apply and the section as a whole is intended to apply in other
00214 circumstances.
00215 
00216 It is not the purpose of this section to induce you to infringe any
00217 patents or other property right claims or to contest validity of any
00218 such claims; this section has the sole purpose of protecting the
00219 integrity of the free software distribution system, which is
00220 implemented by public license practices.  Many people have made
00221 generous contributions to the wide range of software distributed
00222 through that system in reliance on consistent application of that
00223 system; it is up to the author/donor to decide if he or she is willing
00224 to distribute software through any other system and a licensee cannot
00225 impose that choice.
00226 
00227 This section is intended to make thoroughly clear what is believed to
00228 be a consequence of the rest of this License.
00229 
00230   8. If the distribution and/or use of the Program is restricted in
00231 certain countries either by patents or by copyrighted interfaces, the
00232 original copyright holder who places the Program under this License
00233 may add an explicit geographical distribution limitation excluding
00234 those countries, so that distribution is permitted only in or among
00235 countries not thus excluded.  In such case, this License incorporates
00236 the limitation as if written in the body of this License.
00237 
00238   9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time.  Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
00239 
00240 Each version is given a distinguishing version number.  If the Program
00241 specifies a version number of this License which applies to it and "any
00242 later version", you have the option of following the terms and conditions 
00243 either of that version or of any later version published by the Free Software 
00244 Foundation.  If the Program does not specify a version number of this License,
00245  you may choose any version ever published by the Free Software Foundation.
00246 
00247   10. If you wish to incorporate parts of the Program into other free
00248 programs whose distribution conditions are different, write to the author to 
00249 ask for permission.  For software which is copyrighted by the Free Software 
00250 Foundation, write to the Free Software Foundation; we sometimes make 
00251 exceptions for this.  Our decision will be guided by the two goals of 
00252 preserving the free status of all derivatives of our free software and of 
00253 promoting the sharing and reuse of software generally.
00254 
00255                             NO WARRANTY
00256 
00257   11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
00258 THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN 
00259 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE 
00260 THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
00261 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
00262 FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND 
00263 PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, 
00264 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
00265 
00266   12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 
00267 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 
00268 REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 
00269 INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 
00270 OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO 
00271 LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR 
00272 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 
00273 EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
00274 DAMAGES.
00275 
00276                      END OF TERMS AND CONDITIONS '
00277 */
00278 static const char* Copyright= "(C) Copyright Michigan State University 2002, All rights reserved";
00302 // Synchronization assumptions:
00303 //    the (2) socket calls are recursive (threadsafe).
00304 //    the (3) network database calls are not recursive (notthreadsafe).
00305 //  as such, any calls to (3) network database calls are bracketed with
00306 //  calls to globally synchronize the thread.
00311 
00312 #include <config.h>
00313 #include "CSocket.h"    
00314 #include <CApplicationSerializer.h>
00315 
00316 // Exception classes required:
00317 
00318 #include <CTCPNoSuchHost.h>
00319 #include <CTCPNoSuchService.h>
00320 #include <CTCPBadSocketState.h>
00321 #include <CTCPConnectionFailed.h>
00322 #include <ErrnoException.h>
00323 #include <CTCPConnectionLost.h>
00324 
00325 // Standard and netork library headers.
00326 
00327 #include <stdio.h>
00328 #include <sys/types.h>
00329 #include <sys/socket.h>
00330 #include <sys/time.h>
00331 #include <netinet/in.h>
00332 #include <arpa/inet.h>
00333 
00334 // STL includes.
00335 
00336 #include <vector>       
00337 
00338 #ifdef HAVE_STD_NAMESPACE
00339 using namespace std;
00340 #endif
00341                         
00342 
00343 // Manifest constannts:
00344 
00345 #ifndef TRUE
00346 #define TRUE 1
00347 #endif
00348 #ifndef FALSE
00349 #define FALSE 0
00350 #endif
00351 
00352 // Static members:
00353 
00354 map<CSocket::State, string> CSocket::m_StateNames;
00355 
00371 CSocket::CSocket ()
00372    : m_Fd(-1),
00373      m_State(Disconnected)
00374  
00375 {
00376   OpenSocket();
00377 } 
00378 
00384 CSocket::~CSocket ( )  //Destructor - Delete dynamic objects
00385 {
00386   if(m_State == Connected) {
00387     Shutdown();
00388   }
00389   if(m_Fd > 0) {
00390     close(m_Fd);
00391   }
00392 }
00393 
00404 CSocket::CSocket (    int am_Fd,  CSocket::State am_State  ) :
00405   m_Fd(am_Fd),
00406   m_State(am_State)
00407 
00408 {  
00409   StockStateMap();              // Ensure the statename map is stocked.
00410 } 
00411 
00412 // Functions for class CSocket
00413 
00448 void 
00449 CSocket::Connect(const string& host, const string& service)  
00450 { 
00451   // We must be disconnected to do any of this...
00452   
00453   vector<CSocket::State> allowedStates;
00454   allowedStates.push_back(Disconnected);
00455 
00456   if(m_State != Disconnected) throw CTCPBadSocketState(m_State, allowedStates,
00457                                                     "CSocket::Connect");
00458 
00459   // If the host is a dotted ip number just translate it.. otherwise
00460   // try to resolve the name:
00461 
00462   unsigned long int ipaddr;
00463   struct hostent* pEntry;
00464 
00465     CApplicationSerializer::getInstance()->Lock(); // <-- Critical region
00466     {                                              // (3) calls not threadsafe.
00467       ipaddr = inet_addr(host.c_str());
00468       if(ipaddr == INADDR_NONE) {       // Need to translate the name...
00469         pEntry = gethostbyname(host.c_str());
00470         if(pEntry) memcpy(&ipaddr, pEntry->h_addr, 4);
00471       }                                              // <-- End Critical region
00472       CApplicationSerializer::getInstance()->UnLock();
00473       if(!pEntry) throw CTCPNoSuchHost(host, 
00474                                        "Null return from gethostbyname(3)");
00475     }
00476   // Next resolve the service name... this can be either a number or a 
00477   // service name.  Note that the port number is in the service database
00478   // in network byte order.
00479 
00480   unsigned short port = Service(service);
00481 
00482 
00483   // Now we have everything we need to do call 'the other' Connect.
00484 
00485   Connect(ntohl(ipaddr), ntohs(port));
00486 }  
00487 
00500 void 
00501 CSocket::Connect(unsigned long int IpAddress, unsigned short service)
00502 
00503 {
00504   // Must be disconnected:
00505 
00506   vector<CSocket::State> allowedStates;
00507   allowedStates.push_back(Disconnected);
00508 
00509   if(m_State != Disconnected) throw CTCPBadSocketState(m_State, allowedStates,
00510                                                     "CSocket::Connect");
00511 
00512   // Both the ip address and service must be in network byte order to be 
00513   // acceptable to connect(2).
00514 
00515   IpAddress = htonl(IpAddress);
00516   service   = htons(service);
00517 
00518   // Build the socket peer address structure.
00519 
00520   sockaddr_in Peer;
00521   Peer.sin_port = service;
00522   memcpy(&(Peer.sin_addr), &IpAddress, sizeof(unsigned long int));
00523   Peer.sin_family = AF_INET;
00524 
00525   // Try the connect.
00526 
00527   if(connect(m_Fd, (const sockaddr*)&Peer, sizeof(sockaddr_in)) < 0) {
00528     int sErrno = errno;
00529     string Ip, Svc;
00530     CApplicationSerializer::getInstance()->Lock(); //<-- Critical section start
00531     {
00532       char port[50];
00533       Ip = inet_ntoa(Peer.sin_addr);
00534       sprintf(port,"%d", ntohs(service));
00535       Svc  = port;
00536 
00537     }
00538     CApplicationSerializer::getInstance()->UnLock(); //<-- End Crit. Section.
00539     errno = sErrno;             // sprintf e.g. destroys errno.
00540     throw CTCPConnectionFailed(Ip, Svc,
00541                                "CSocket::Connect client connect(2) failed");
00542   }
00543 
00544   m_State = Connected;
00545   
00546 
00547 }
00548 
00567 void 
00568 CSocket::Bind(const string& service)  
00569 { 
00570   vector<CSocket::State> allowedStates;
00571   allowedStates.push_back(Disconnected);
00572   if(m_State != Disconnected) throw CTCPBadSocketState(m_State, allowedStates,
00573                                                        "CSocket::Bind");
00574   // Allow bind to re-use an existing address.. otherwise there are
00575   // terrible problems with servers that exit and then restart.
00576 
00577   int reuse=TRUE;
00578   if(setsockopt(m_Fd, SOL_SOCKET,
00579                 SO_REUSEADDR,  &reuse, sizeof(int)) < 0) {
00580     throw CErrnoException("CSocket::Bind - setsockopt92) for re-use failed");
00581   }
00582 
00583   // Determine the service in network byte order.
00584 
00585   unsigned short port = Service(service);
00586 
00587 
00588   // Now bind the socket.
00589 
00590   sockaddr_in Peer;
00591   Peer.sin_port   = port;
00592   Peer.sin_family = AF_INET;
00593   Peer.sin_addr.s_addr = htonl(INADDR_ANY);
00594 
00595   if(bind(m_Fd, (sockaddr*)&Peer, sizeof(sockaddr_in)) < 0) {
00596     throw CErrnoException("Error from bind(2) in CSocket::Bind()");
00597   }
00598   m_State = Bound;
00599 
00600 }  
00601 
00623 void 
00624 CSocket::Listen(unsigned int nBacklog)  
00625 { 
00626   // Throw CTCPBadSocketState if not bound:
00627 
00628   if(m_State != Bound) {
00629     vector<CSocket::State> allowedStates;
00630     allowedStates.push_back(Bound);
00631     throw CTCPBadSocketState(m_State, allowedStates, 
00632                              "CSocket::Listen");
00633   }
00634   if(listen(m_Fd, nBacklog) < 0) 
00635     throw CErrnoException("CSocket::Listen listen(2) failed");
00636 
00637   m_State = Listening;
00638 }  
00639 
00672 CSocket*  
00673 CSocket::Accept(string& client)  
00674 { 
00675   // Throw CTCPBadSocket if not listening.
00676 
00677   if(m_State != Listening) {
00678     vector<CSocket::State> allowedStates;
00679     allowedStates.push_back(Listening);
00680     throw CTCPBadSocketState(m_State, allowedStates,
00681                              "CSocket::Accept");
00682   }
00683   
00684   // Attempt the accept.  If successfule, the return
00685   // value will be a socket fd.
00686 
00687   struct sockaddr_in peerInfo;
00688   socklen_t          addrlen(sizeof(sockaddr_in));
00689   int peerFd = accept(m_Fd, (sockaddr*)&peerInfo, &addrlen);
00690   if(peerFd < 0) {
00691     throw CErrnoException("CSocket::Accept accept(2) failed");
00692   }
00693 
00694   // At this time we have received a good connection. There are two things
00695   // we need to do:
00696   //   1. Construct a new CSocket in the connected state.
00697   //   2. Figure out and fill in the client:
00698 
00699   CSocket* pConnection = new CSocket(peerFd, Connected);
00700   client = AddressToHostString(peerInfo.sin_addr);
00701 
00702   return pConnection;
00703 }  
00704 
00721 void 
00722 CSocket::Shutdown()  
00723 { 
00724   // Require that the socket be connected.
00725 
00726   if(m_State != Connected) {
00727     vector<CSocket::State> allowedStates;
00728     allowedStates.push_back(Connected);
00729     throw CTCPBadSocketState(m_State, allowedStates,
00730                              "CSocket::Shutdown");
00731   }
00732 
00733  
00734   if(shutdown(m_Fd, SHUT_RDWR) < 0) {
00735     throw CErrnoException("CSocket::Shutdown failed in call to shutdown(2)");
00736   }
00737 
00738   m_State = Disconnected;
00739   close(m_Fd);
00740   m_Fd = -1;                    // Mark the socket closed.
00741 OpenSocket();
00742 }  
00743 
00762 int 
00763 CSocket::Read(void* pBuffer, size_t nBytes)  
00764 { 
00765   // Require the socket be connected:
00766 
00767   if(m_State != Connected) {
00768     vector<CSocket::State> allowedStates;
00769     allowedStates.push_back(Connected);
00770     throw CTCPBadSocketState(m_State, allowedStates,
00771                              "CSocket::Read()");
00772   }
00773   // Attempt the read:
00774 
00775   int nB = read(m_Fd, pBuffer, nBytes);
00776   
00777   // Check for EOF:
00778 
00779   if(nB == 0) {
00780     m_State = Disconnected;
00781     throw CTCPConnectionLost(this, "CSocket::Read: from read(2)");
00782   }
00783   // Check for error:
00784 
00785   if(nB < 0) {
00786     m_State = Disconnected;
00787     throw CErrnoException("CSocket::Read failed read(2)");
00788   }
00789   // There's data to transfer.
00790 
00791   return nB;
00792 }  
00793 
00794 
00823 int 
00824 CSocket::Write(void* pBuffer, size_t nBytes)  
00825 {
00826   // Require that the socket is connected:
00827 
00828   
00829 
00830   // write(2) as many times as are needed to get the data out.
00831 
00832 
00833   char* p((char* )pBuffer);     // Pointer to current write.
00834   int   resid(nBytes);          // Number of remaining bytes to write.
00835   int   nWritten(0);            // Number  of bytes already written.
00836 
00837   while(resid) {
00838     int n = write(m_Fd, p, resid);
00839     if(n > 0) {                 // Do the book-keeping for a successful write.
00840       resid    -= n;
00841       nWritten += n;
00842       p        += n;
00843     } 
00844     else {                      // Write failed:
00845       if(nWritten > 0) {        // Assume if not first write, additional
00846         break;                  // Writes will give the same error.
00847       }
00848       if(errno == EPIPE) {      // Disconnected.
00849         m_State = Disconnected;
00850         throw CTCPConnectionLost(this,"CSocket::Write - first write of loop");
00851       }
00852       else {                    // Some other error:
00853         m_State = Disconnected;
00854         throw CErrnoException("CSocket::Write failed write(2)");
00855       }
00856     }
00857   }
00858   return nWritten;              // Indicate how many bytes were actually
00859                                 // written. If < nBytes something unusual
00860                                 // occured in the loop above.
00861 }  
00862 
00883 void 
00884 CSocket::getPeer(unsigned short&  port, string& peer)  
00885 {
00886   // Enforce connection requirement:
00887 
00888   if(m_State != Connected) {
00889     vector<CSocket::State> allowedStates;
00890     allowedStates.push_back(Connected);
00891     throw CTCPBadSocketState(m_State, allowedStates,
00892                              "CSocket::GetPeer");
00893   }
00894   // Try the call to getpeername(2):
00895 
00896   sockaddr_in PeerInfo;
00897   socklen_t   addrsize(sizeof(sockaddr_in));
00898   if(getpeername(m_Fd, (sockaddr*)&PeerInfo, &addrsize) < 0) {
00899     throw CErrnoException("CSocket::getPeer failed call to getpeername(2)");
00900   }
00901   // Pull out the port in local host byte order, and translate the
00902   // ip address:
00903 
00904   port = ntohs(PeerInfo.sin_port);
00905   peer = AddressToHostString(PeerInfo.sin_addr);
00906 
00907 }  
00908 
00929 void 
00930 CSocket::OOBInline(bool State)  
00931 { 
00932   int state((int)State);
00933   if(setsockopt(m_Fd, SOL_SOCKET, 
00934                 SO_OOBINLINE, (void*)&state, sizeof(int)) < 0) {
00935     throw CErrnoException("CSocket::OOBInline setsockopt(2) failed");
00936   }
00937 }  
00938 
00954 bool 
00955 CSocket::isOOBInline()  
00956 { 
00957   unsigned int state;
00958   socklen_t    size(sizeof(int));
00959   if(getsockopt(m_Fd, SOL_SOCKET, 
00960                 SO_OOBINLINE, (void*)&state, &size) < 0) {
00961     throw CErrnoException("CSocket::OOBInline getsockopt(2) failed");
00962   }
00963   return state ? TRUE : FALSE;
00964 }  
00965 
00986 void 
00987 CSocket::setRcvLowWaterMark(size_t nBytes)  
00988 { 
00989   if(setsockopt(m_Fd, SOL_SOCKET, SO_RCVLOWAT, (void*)&nBytes, sizeof(size_t)) < 0) {
00990     if (errno == ENOPROTOOPT) {
00991       return; // Just not suppported is ok..
00992     }
00993     throw 
00994       CErrnoException("CSocket::setRcvLowWaterMark() setsockopt(2) failed");
00995   }
00996 }
00997 
00998 
01010 size_t 
01011 CSocket::getRcvLowWaterMark()  
01012 { 
01013   int nBytes;
01014   socklen_t addrlen(sizeof(int));
01015   if(getsockopt(m_Fd, SOL_SOCKET, SO_RCVLOWAT, &nBytes, &addrlen) < 0) {
01016     throw 
01017       CErrnoException("CSocket::getRcvLowWaterMark() getsockopt(2) failed");
01018   }
01019   return (size_t)nBytes;
01020 }  
01021 
01039 void 
01040 CSocket::setSndLowWaterMark(size_t nBytes)  
01041 { 
01042   if(setsockopt(m_Fd, SOL_SOCKET, SO_SNDLOWAT, &nBytes, sizeof(size_t)) < 0) {
01043     if (errno != ENOPROTOOPT) {
01044       CErrnoException("CSocket::setSndLowWaterMark() setsockopt(2) failed");
01045     }
01046   }  
01047 }
01057 size_t 
01058 CSocket::getSndLowWaterMark()  
01059 {
01060   int nBytes;
01061   socklen_t adrlen(sizeof(int));
01062   if(getsockopt(m_Fd, SOL_SOCKET, SO_SNDLOWAT, &nBytes, &adrlen) < 0) {
01063     throw 
01064       CErrnoException("CSocket::getRcvLowWaterMark() getsockopt(2) failed");
01065   }
01066   return (size_t)nBytes; 
01067 }  
01068 
01080 void 
01081 CSocket::setRcvTimeout(unsigned int nMs)  
01082 { 
01083   //
01084   // First convert the nMs parameter into a timeval.
01085   //
01086   timeval timeout;
01087   timeout.tv_sec  = nMs / 1000;          // Number of seconds...
01088   timeout.tv_usec = (nMs % 1000) * 1000; // Left over milliseconds in uSec.
01089 
01090   // Now make the request:
01091 
01092   if(setsockopt(m_Fd, SOL_SOCKET, SO_RCVTIMEO, 
01093                 &timeout, sizeof(timeval)) < 0) {
01094     if(errno != ENOPROTOOPT) {
01095       throw CErrnoException("CSocket::setRcvTimeout setsockopt(2) failed");
01096     }
01097   }
01098 }  
01099 
01111 unsigned int 
01112 CSocket::getRcvTimeout()  
01113 { 
01114   timeval timeout;
01115   socklen_t adrlen(sizeof(timeval));
01116   
01117   // Get the timeout if possible: 
01118 
01119   if(getsockopt(m_Fd, SOL_SOCKET, SO_RCVTIMEO, 
01120                 &timeout, &adrlen) < 0) {
01121     throw CErrnoException("CSocket::getRcvTimeout getsockopt(2) failed");
01122   }
01123   // Convert to ms and return.
01124 
01125   int nMs = timeout.tv_sec*1000; + timeout.tv_usec/1000000;
01126   return nMs;
01127 }  
01128 
01144 void 
01145 CSocket::setSndTimeout(unsigned int nMs)  
01146 { 
01147   // Convert timeout to a timeval structure:
01148 
01149   timeval timeout;
01150   timeout.tv_sec  = nMs / 1000;          // Number of seconds...
01151   timeout.tv_usec = (nMs % 1000) * 1000; // Left over milliseconds in uSec.
01152 
01153   // Now make the request:
01154 
01155   if(setsockopt(m_Fd, SOL_SOCKET, SO_SNDTIMEO, 
01156                 &timeout, sizeof(timeval)) < 0) {
01157     if(errno != ENOPROTOOPT) {
01158       throw CErrnoException("CSocket::setRcvTimeout setsockopt(2) failed");
01159     }
01160   }
01161 }  
01162 
01172 unsigned int 
01173 CSocket::getSndTimeout()  
01174 { 
01175   timeval timeout;
01176   socklen_t adrlen(sizeof(timeval));
01177   
01178   // Get the timeout if possible: 
01179 
01180   if(getsockopt(m_Fd, SOL_SOCKET, SO_SNDTIMEO, 
01181                 &timeout, &adrlen) < 0) {
01182     throw CErrnoException("CSocket::getRcvTimeout getsockopt(2) failed");
01183   }
01184   // Convert to ms and return.
01185 
01186   int nMs = timeout.tv_sec*1000; + timeout.tv_usec/1000000;
01187   return nMs;
01188 }  
01189 
01206 void 
01207 CSocket::Debug(bool fState)  
01208 {
01209   int state((int)fState);
01210   if(setsockopt(m_Fd, SOL_SOCKET, SO_DEBUG, &state, sizeof(int)) < 0) {
01211     throw CErrnoException("CSocket::Debug setsockopt(2) failed");
01212   }
01213 
01214 }  
01215 
01227 bool 
01228 CSocket::isDebug()  
01229 { 
01230   int fstate;
01231   socklen_t adrlen(sizeof(int));
01232   if(getsockopt(m_Fd, SOL_SOCKET, SO_DEBUG, &fstate, &adrlen) < 0) {
01233     throw CErrnoException("CSocket:Debug getsockopt(2) failed");
01234   }
01235   return fstate ? TRUE : FALSE;
01236 }  
01237 
01260 void 
01261 CSocket::SetNotRoutable(bool fRoutable)  
01262 { 
01263   int state((int)fRoutable);
01264   if(setsockopt(m_Fd, SOL_SOCKET, SO_DONTROUTE, &state, sizeof(int)) < 0) {
01265     throw CErrnoException("CSocket::SetNotRoutable setsockopt(2) failed");
01266   }
01267 
01268 }  
01269 
01285 bool 
01286 CSocket::isNotRoutable()  
01287 { 
01288   int fState;
01289   socklen_t adrlen(sizeof(int));
01290   if(getsockopt(m_Fd, SOL_SOCKET, SO_DONTROUTE, &fState, &adrlen) < 0) {
01291     throw CErrnoException("CSocket::isNotRoutable getsockopt(2) failed");
01292   }
01293   return fState ? TRUE : FALSE;
01294 }  
01295 
01318 void 
01319 CSocket::setSndBufSize(size_t nBufferSize)  
01320 { 
01321   unsigned int size((unsigned int)nBufferSize);
01322   if(setsockopt(m_Fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0) {
01323     throw CErrnoException("CSocket::setSndBufSize setsockopt(2) failed");
01324   }
01325 }  
01326 
01339 size_t 
01340 CSocket::getSndBufSize()  
01341 { 
01342   unsigned int size;
01343   socklen_t    adrlen(sizeof(int));
01344   if(getsockopt(m_Fd, SOL_SOCKET, SO_SNDBUF, &size, &adrlen) < 0) {
01345     throw CErrnoException("CSocket::getSndBufSize getsockopt(2) failed");
01346   }
01347   return (size_t)size;
01348 }  
01349 
01364 void 
01365 CSocket::setRcvBufSize(size_t nBytes)  
01366 {
01367   unsigned int size = (unsigned int)nBytes;
01368  
01369   if(setsockopt(m_Fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(int)) < 0) {
01370     throw CErrnoException("CSocket::setRcvBufSize setsockopt(2) failed");
01371   }
01372 }  
01373 
01386 size_t 
01387 CSocket::getRcvBufSize()  
01388 { 
01389   unsigned int size;
01390   socklen_t    adrlen(sizeof(int));
01391   if(getsockopt(m_Fd, SOL_SOCKET, SO_RCVBUF, &size, &adrlen) < 0) {
01392     throw CErrnoException("CSocket::getSndBufSize getsockopt(2) failed");
01393   }
01394   return (size_t)size;
01395 }  
01396 
01418 void 
01419 CSocket::setLinger(bool lOn, int nLingerSeconds)  
01420 { 
01421   // The linger structure is filled in the way it is so that the user could
01422   // conceivably omit a timeout interval if lOn == FALSE since in that case,
01423   // nLingerSeconds won't be referenced.
01424 
01425   linger lInfo;
01426   lInfo.l_onoff = lOn;
01427   if(lOn) lInfo.l_linger = nLingerSeconds;
01428 
01429   // Set the linger parameters:
01430 
01431   if(setsockopt(m_Fd, SOL_SOCKET, SO_LINGER, &lInfo, sizeof(linger)) < 0) {
01432     throw CErrnoException("CSocket::setLinger setsockopt(2) failed");
01433   }
01434 }  
01435 
01452 void 
01453 CSocket::getLinger(bool& isLingering, int& nLingerSeconds)  
01454 { 
01455   linger linfo;
01456   socklen_t addrlen(sizeof(linger));
01457     if(getsockopt(m_Fd, SOL_SOCKET, SO_LINGER, &linfo, &addrlen) < 0) {
01458       throw CErrnoException("CSocket::getLinger getsockopt(2) failed");
01459     }
01460 
01461   // Fill in the parameters from the linger structure:
01462 
01463   isLingering    = linfo.l_onoff;
01464   nLingerSeconds = linfo.l_linger; 
01465 }
01477 unsigned short
01478 CSocket::Service(const string& rService)
01479 {
01480   unsigned short port;
01481   int           nport;
01482 
01483   if(sscanf(rService.c_str(), "%d", &nport) != 1) { // Non numeric service number.
01484     struct servent* pEntry;
01485     CApplicationSerializer::getInstance()->Lock(); // <-- Critical region
01486     {                                              //  getservbyname not recursive
01487       pEntry = getservbyname(rService.c_str(), "tcp");
01488       if(pEntry) port = pEntry->s_port;
01489     }
01490     CApplicationSerializer::getInstance()->UnLock(); // <-- End critical rgn.
01491     if(!pEntry) throw CTCPNoSuchService(rService, 
01492                                      "Null return from getservbyname(3)");
01493   }
01494   else {
01495     port = nport;
01496     port = htons(port);
01497   }
01498   return port;
01499 }
01500 
01514 string
01515 CSocket:: AddressToHostString(in_addr peer)
01516 {
01517   string result;
01518   CApplicationSerializer::getInstance()->Lock(); // <-- Critical Region.
01519   {
01520     struct hostent* pEntry = gethostbyaddr((const char*)&peer, 
01521                                            sizeof(in_addr), AF_INET);
01522     if(pEntry) {                // Reverse DNS worked...
01523       result = pEntry->h_name;
01524       
01525     }
01526     else {                      // Need to return dotted address.
01527       result = inet_ntoa(peer);
01528     }
01529   }
01530   CApplicationSerializer::getInstance()->UnLock(); // <-- End Critical Region.
01531 
01532   return result;
01533 }
01543 string
01544 CSocket::StateName(CSocket::State state)
01545 {
01546   StockStateMap();
01547   return m_StateNames[state];
01548 }
01555 void 
01556 CSocket::StockStateMap()
01557 {
01558   if(!m_StateNames.empty()) {
01559     m_StateNames[Disconnected] = "Disconnected";
01560     m_StateNames[Bound]        = "Bound";
01561     m_StateNames[Listening]    = "Listening";
01562     m_StateNames[Connected]    = "Connected";
01563   }
01564 }
01568 void
01569 CSocket::OpenSocket()
01570 {
01571   int             protocol;
01572   struct protoent* pEntry;
01573   StockStateMap();              // Ensure the state name map is stocked.
01574 
01575   CApplicationSerializer::getInstance()->Lock(); //--> Critical region since
01576   {                                            // getprotoent not recursive.
01577     struct protoent* pEntry = getprotobyname("TCP");
01578     if(pEntry) protocol = pEntry->p_proto;
01579   }                                            // <-- End Critical Region.
01580   CApplicationSerializer::getInstance()->UnLock();
01581 
01582   if(!pEntry) throw 
01583               CErrnoException("Getting TCP protocol num from getprotoent(3)");
01584 
01585   m_Fd = socket(PF_INET, SOCK_STREAM, protocol);
01586   if(m_Fd == -1) throw
01587                    CErrnoException("Creating socket with socket(2)");
01588 
01589 }

Generated on Thu Jan 6 16:58:42 2005 for Spectrodaq External Event Framework by  doxygen 1.3.9.1