• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

I_NetVConnection.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   This file implements an I/O Processor for network I/O
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022 
00023  */
00024 
00025 #ifndef __NETVCONNECTION_H__
00026 #define __NETVCONNECTION_H__
00027 
00028 #include "I_Action.h"
00029 #include "I_VConnection.h"
00030 #include "I_Event.h"
00031 #include "List.h"
00032 #include "I_IOBuffer.h"
00033 #include "I_Socks.h"
00034 #include <ts/apidefs.h>
00035 
00036 #define CONNECT_SUCCESS   1
00037 #define CONNECT_FAILURE   0
00038 
00039 #define SSL_EVENT_SERVER 0
00040 #define SSL_EVENT_CLIENT 1
00041 
00042 /** Holds client options for NetVConnection.
00043 
00044     This class holds various options a user can specify for
00045     NetVConnection. Various clients need many slightly different
00046     features. This is an attempt to prevent out of control growth of
00047     the connection method signatures. Only options of interest need to
00048     be explicitly set -- the rest get sensible default values.
00049 
00050     @note Binding addresses is a bit complex. It is not currently
00051     possible to bind indiscriminately across protocols, which means
00052     any connection must commit to IPv4 or IPv6. For this reason the
00053     connection logic will look at the address family of @a local_addr
00054     even if @a addr_binding is @c ANY_ADDR and bind to any address in
00055     that protocol. If it's not an IP protocol, IPv4 will be used.
00056 */
00057 struct NetVCOptions {
00058   typedef NetVCOptions self; ///< Self reference type.
00059 
00060   /// Values for valid IP protocols.
00061   enum ip_protocol_t {
00062     USE_TCP, ///< TCP protocol.
00063     USE_UDP ///< UDP protocol.
00064   };
00065 
00066   /// IP (TCP or UDP) protocol to use on socket.
00067   ip_protocol_t ip_proto;
00068 
00069   /** IP address family.
00070 
00071       This is used for inbound connections only if @c local_ip is not
00072       set, which is sometimes more convenient for the client. This
00073       defaults to @c AF_INET so if the client sets neither this nor @c
00074       local_ip then IPv4 is used.
00075 
00076       For outbound connections this is ignored and the family of the
00077       remote address used.
00078 
00079       @note This is (inconsistently) called "domain" and "protocol" in
00080       other places. "family" is used here because that's what the
00081       standard IP data structures use.
00082 
00083   */
00084   uint16_t ip_family;
00085 
00086   /** The set of ways in which the local address should be bound.
00087 
00088       The protocol is set by the contents of @a local_addr regardless
00089       of this value. @c ANY_ADDR will override only the address.
00090 
00091       @note The difference between @c INTF_ADDR and @c FOREIGN_ADDR is
00092       whether transparency is enabled on the socket. It is the
00093       client's responsibility to set this correctly based on whether
00094       the address in @a local_addr is associated with an interface on
00095       the local system ( @c INTF_ADDR ) or is owned by a foreign
00096       system ( @c FOREIGN_ADDR ).  A binding style of @c ANY_ADDR
00097       causes the value in @a local_addr to be ignored.
00098 
00099       The IP address and port are separate because most clients treat
00100       these independently. For the same reason @c IpAddr is used
00101       to be clear that it contains no port data.
00102 
00103       @see local_addr
00104       @see addr_binding
00105    */
00106   enum addr_bind_style {
00107     ANY_ADDR, ///< Bind to any available local address (don't care, default).
00108     INTF_ADDR, ///< Bind to interface address in @a local_addr.
00109     FOREIGN_ADDR ///< Bind to foreign address in @a local_addr.
00110   };
00111 
00112   /** Local address for the connection.
00113 
00114       For outbound connections this must have the same family as the
00115       remote address (which is not stored in this structure). For
00116       inbound connections the family of this value overrides @a
00117       ip_family if set.
00118 
00119       @note Ignored if @a addr_binding is @c ANY_ADDR.
00120       @see addr_binding
00121       @see ip_family
00122   */
00123   IpAddr local_ip;
00124   /** Local port for connection.
00125       Set to 0 for "don't care" (default).
00126    */
00127   uint16_t local_port;
00128   /// How to bind the local address.
00129   /// @note Default is @c ANY_ADDR.
00130   addr_bind_style addr_binding;
00131 
00132   /// Make the socket blocking on I/O (default: @c false)
00133   bool f_blocking;
00134   /// Make socket block on connect (default: @c false)
00135   bool f_blocking_connect;
00136 
00137   /// Control use of SOCKS.
00138   /// Set to @c NO_SOCKS to disable use of SOCKS. Otherwise SOCKS is
00139   /// used if available.
00140   unsigned char socks_support;
00141   /// Version of SOCKS to use.
00142   unsigned char socks_version;
00143 
00144   int socket_recv_bufsize;
00145   int socket_send_bufsize;
00146 
00147   /// Configuration options for sockets.
00148   /// @note These are not identical to internal socket options but
00149   /// specifically defined for configuration. These are mask values
00150   /// and so must be powers of 2.
00151   uint32_t sockopt_flags;
00152   /// Value for TCP no delay for @c sockopt_flags.
00153   static uint32_t const SOCK_OPT_NO_DELAY = 1;
00154   /// Value for keep alive for @c sockopt_flags.
00155   static uint32_t const SOCK_OPT_KEEP_ALIVE = 2;
00156 
00157   uint32_t packet_mark;
00158   uint32_t packet_tos;
00159 
00160   EventType etype;
00161 
00162   char * sni_servername; // SSL SNI to origin
00163 
00164   /// Reset all values to defaults.
00165   void reset();
00166 
00167   void set_sock_param(int _recv_bufsize, int _send_bufsize, unsigned long _opt_flags,
00168                       unsigned long _packet_mark = 0, unsigned long _packet_tos = 0);
00169 
00170   NetVCOptions() : sni_servername(NULL) {
00171     reset();
00172   }
00173 
00174   ~NetVCOptions() {
00175     ats_free(sni_servername);
00176   }
00177 
00178   void set_sni_servername(const char * name, size_t len) {
00179     IpEndpoint ip;
00180 
00181     ats_free(sni_servername);
00182     sni_servername = NULL;
00183     // Literal IPv4 and IPv6 addresses are not permitted in "HostName".(rfc6066#section-3)
00184     if (ats_ip_pton(ts::ConstBuffer(name, len), &ip) != 0) {
00185       sni_servername = ats_strndup(name, len);
00186     }
00187   }
00188 
00189   NetVCOptions & operator=(const NetVCOptions & opt) {
00190     if (&opt != this) {
00191       ats_free(this->sni_servername);
00192       memcpy(this, &opt, sizeof(opt));
00193       if (opt.sni_servername) {
00194         this->sni_servername = ats_strdup(opt.sni_servername);
00195       }
00196     }
00197     return *this;
00198   }
00199 
00200   /// @name Debugging
00201   //@{
00202   /// Convert @a s to its string equivalent.
00203   static char const* toString(addr_bind_style s);
00204   //@}
00205 
00206 private:
00207   NetVCOptions(const NetVCOptions&);
00208 };
00209 
00210 /**
00211   A VConnection for a network socket. Abstraction for a net connection.
00212   Similar to a socket descriptor VConnections are IO handles to
00213   streams. In one sense, they serve a purpose similar to file
00214   descriptors. Unlike file descriptors, VConnections allow for a
00215   stream IO to be done based on a single read or write call.
00216 
00217 */
00218 class NetVConnection:public VConnection
00219 {
00220 public:
00221 
00222   /**
00223      Initiates read. Thread safe, may be called when not handling
00224      an event from the NetVConnection, or the NetVConnection creation
00225      callback.
00226 
00227     Callbacks: non-reentrant, c's lock taken during callbacks.
00228 
00229     <table>
00230       <tr><td>c->handleEvent(VC_EVENT_READ_READY, vio)</td><td>data added to buffer</td></tr>
00231       <tr><td>c->handleEvent(VC_EVENT_READ_COMPLETE, vio)</td><td>finished reading nbytes of data</td></tr>
00232       <tr><td>c->handleEvent(VC_EVENT_EOS, vio)</td><td>the stream has been shutdown</td></tr>
00233       <tr><td>c->handleEvent(VC_EVENT_ERROR, vio)</td><td>error</td></tr>
00234     </table>
00235 
00236     The vio returned during callbacks is the same as the one returned
00237     by do_io_read(). The vio can be changed only during call backs
00238     from the vconnection.
00239 
00240     @param c continuation to be called back after (partial) read
00241     @param nbytes no of bytes to read, if unknown set to INT64_MAX
00242     @param buf buffer to put the data into
00243     @return vio
00244 
00245   */
00246   virtual VIO * do_io_read(Continuation * c, int64_t nbytes, MIOBuffer * buf) = 0;
00247 
00248   /**
00249     Initiates write. Thread-safe, may be called when not handling
00250     an event from the NetVConnection, or the NetVConnection creation
00251     callback.
00252 
00253     Callbacks: non-reentrant, c's lock taken during callbacks.
00254 
00255     <table>
00256       <tr>
00257         <td>c->handleEvent(VC_EVENT_WRITE_READY, vio)</td>
00258         <td>signifies data has written from the reader or there are no bytes available for the reader to write.</td>
00259       </tr>
00260       <tr>
00261         <td>c->handleEvent(VC_EVENT_WRITE_COMPLETE, vio)</td>
00262         <td>signifies the amount of data indicated by nbytes has been read from the buffer</td>
00263       </tr>
00264       <tr>
00265         <td>c->handleEvent(VC_EVENT_ERROR, vio)</td>
00266         <td>signified that error occured during write.</td>
00267       </tr>
00268     </table>
00269 
00270     The vio returned during callbacks is the same as the one returned
00271     by do_io_write(). The vio can be changed only during call backs
00272     from the vconnection. The vconnection deallocates the reader
00273     when it is destroyed.
00274 
00275     @param c continuation to be called back after (partial) write
00276     @param nbytes no of bytes to write, if unknown msut be set to INT64_MAX
00277     @param buf source of data
00278     @param owner
00279     @return vio pointer
00280 
00281   */
00282   virtual VIO *do_io_write(Continuation * c, int64_t nbytes, IOBufferReader * buf, bool owner = false) = 0;
00283 
00284   /**
00285     Closes the vconnection. A state machine MUST call do_io_close()
00286     when it has finished with a VConenction. do_io_close() indicates
00287     that the VConnection can be deallocated. After a close has been
00288     called, the VConnection and underlying processor must NOT send
00289     any more events related to this VConnection to the state machine.
00290     Likeswise, state machine must not access the VConnectuion or
00291     any returned VIOs after calling close. lerrno indicates whether
00292     a close is a normal close or an abort. The difference between
00293     a normal close and an abort depends on the underlying type of
00294     the VConnection. Passing VIO::CLOSE for lerrno indicates a
00295     normal close while passing VIO::ABORT indicates an abort.
00296 
00297     @param lerrno VIO:CLOSE for regular close or VIO::ABORT for aborts
00298 
00299   */
00300   virtual void do_io_close(int lerrno = -1) = 0;
00301 
00302   /**
00303     Shuts down read side, write side, or both. do_io_shutdown() can
00304     be used to terminate one or both sides of the VConnection. The
00305     howto is one of IO_SHUTDOWN_READ, IO_SHUTDOWN_WRITE,
00306     IO_SHUTDOWN_READWRITE. Once a side of a VConnection is shutdown,
00307     no further I/O can be done on that side of the connections and
00308     the underlying processor MUST NOT send any further events
00309     (INCLUDING TIMOUT EVENTS) to the state machine. The state machine
00310     MUST NOT use any VIOs from a shutdown side of a connection.
00311     Even if both sides of a connection are shutdown, the state
00312     machine MUST still call do_io_close() when it wishes the
00313     VConnection to be deallocated.
00314 
00315     @param howto IO_SHUTDOWN_READ, IO_SHUTDOWN_WRITE, IO_SHUTDOWN_READWRITE
00316 
00317   */
00318   virtual void do_io_shutdown(ShutdownHowTo_t howto) = 0;
00319 
00320 
00321   /**
00322     Sends out of band messages over the connection. This function
00323     is used to send out of band messages (is this still useful?).
00324     cont is called back with VC_EVENT_OOB_COMPLETE - on successful
00325     send or VC_EVENT_EOS - if the other side has shutdown the
00326     connection. These callbacks could be re-entrant. Only one
00327     send_OOB can be in progress at any time for a VC.
00328 
00329     @param cont to be called back with events.
00330     @param buf message buffer.
00331     @param len length of the message.
00332 
00333   */
00334   virtual Action *send_OOB(Continuation * cont, char *buf, int len);
00335 
00336   /**
00337     Cancels a scheduled send_OOB. Part of the message could have
00338     been sent already. Not callbacks to the cont are made after
00339     this call. The Action returned by send_OOB should not be accessed
00340     after cancel_OOB.
00341 
00342   */
00343   virtual void cancel_OOB();
00344 
00345   ////////////////////////////////////////////////////////////
00346   // Set the timeouts associated with this connection.      //
00347   // active_timeout is for the total elasped time of        //
00348   // the connection.                                        //
00349   // inactivity_timeout is the elapsed time from the time   //
00350   // a read or a write was scheduled during which the       //
00351   // connection  was unable to sink/provide data.           //
00352   // calling these functions repeatedly resets the timeout. //
00353   // These functions are NOT THREAD-SAFE, and may only be   //
00354   // called when handing an  event from this NetVConnection,//
00355   // or the NetVConnection creation callback.               //
00356   ////////////////////////////////////////////////////////////
00357 
00358   /**
00359     Sets time after which SM should be notified.
00360 
00361     Sets the amount of time (in nanoseconds) after which the state
00362     machine using the NetVConnection should receive a
00363     VC_EVENT_ACTIVE_TIMEOUT event. The timeout is value is ignored
00364     if neither the read side nor the write side of the connection
00365     is currently active. The timer is reset if the function is
00366     called repeatedly This call can be used by SMs to make sure
00367     that it does not keep any connections open for a really long
00368     time.
00369 
00370     Timeout symantics:
00371 
00372     Should a timeout occur, the state machine for the read side of
00373     the NetVConnection is signaled first assuming that a read has
00374     been initiated on the NetVConnection and that the read side of
00375     the NetVConnection has not been shutdown. Should either of the
00376     two conditions not be met, the NetProcessor will attempt to
00377     signal the write side. If a timeout is sent to the read side
00378     state machine and its handler, return EVENT_DONE, a timeout
00379     will not be sent to the write side. Should the return from the
00380     handler not be EVENT_DONE and the write side state machine is
00381     different (in terms of pointer comparison) from the read side
00382     state machine, the NetProcessor will try to signal the write
00383     side state machine as well. To signal write side, a write must
00384     have been initiated on it and the write must not have been
00385     shutdown.
00386 
00387     Receiving a timeout is only a notification that the timer has
00388     expired. The NetVConnection is still usable. Further timeouts
00389     of the type signaled will not be generated unless the timeout
00390     is reset via the set_active_timeout() or set_inactivity_timeout()
00391     interfaces.
00392 
00393   */
00394   virtual void set_active_timeout(ink_hrtime timeout_in) = 0;
00395 
00396   /**
00397     Sets time after which SM should be notified if the requested
00398     IO could not be performed. Sets the amount of time (in nanoseconds),
00399     if the NetVConnection is idle on both the read or write side,
00400     after which the state machine using the NetVConnection should
00401     receive a VC_EVENT_INACTIVITY_TIMEOUT event. Either read or
00402     write traffic will cause timer to be reset. Calling this function
00403     again also resets the timer. The timeout is value is ignored
00404     if neither the read side nor the write side of the connection
00405     is currently active. See section on timeout semantics above.
00406 
00407    */
00408   virtual void set_inactivity_timeout(ink_hrtime timeout_in) = 0;
00409 
00410   /**
00411     Clears the active timeout. No active timeouts will be sent until
00412     set_active_timeout() is used to reset the active timeout.
00413 
00414   */
00415   virtual void cancel_active_timeout() = 0;
00416 
00417   /**
00418     Clears the inactivity timeout. No inactivity timeouts will be
00419     sent until set_inactivity_timeout() is used to reset the
00420     inactivity timeout.
00421 
00422   */
00423   virtual void cancel_inactivity_timeout() = 0;
00424 
00425   /** @return the current active_timeout value in nanosecs */
00426   virtual ink_hrtime get_active_timeout() = 0;
00427 
00428   /** @return current inactivity_timeout value in nanosecs */
00429   virtual ink_hrtime get_inactivity_timeout() = 0;
00430 
00431   /** Returns local sockaddr storage. */
00432   sockaddr const* get_local_addr();
00433 
00434   /** Returns local ip.
00435       @deprecated get_local_addr() should be used instead for AF_INET6 compatibility.
00436   */
00437   
00438   in_addr_t get_local_ip();
00439 
00440   /** Returns local port. */
00441   uint16_t get_local_port();
00442 
00443   /** Returns remote sockaddr storage. */
00444   sockaddr const* get_remote_addr();
00445 
00446   /** Returns remote ip. 
00447       @deprecated get_remote_addr() should be used instead for AF_INET6 compatibility.
00448   */
00449   in_addr_t get_remote_ip();
00450 
00451   /** Returns remote port. */
00452   uint16_t get_remote_port();
00453 
00454   /** Structure holding user options. */
00455   NetVCOptions options;
00456 
00457   /** Attempt to push any changed options down */
00458   virtual void apply_options() = 0;
00459 
00460   //
00461   // Private
00462   //
00463 
00464   //The following variable used to obtain host addr when transparency
00465   //is enabled by SocksProxy
00466   SocksAddrType socks_addr;
00467 
00468   unsigned int attributes;
00469   EThread *thread;
00470 
00471   /// PRIVATE: The public interface is VIO::reenable()
00472   virtual void reenable(VIO * vio) = 0;
00473 
00474   /// PRIVATE: The public interface is VIO::reenable()
00475   virtual void reenable_re(VIO * vio) = 0;
00476 
00477   /// PRIVATE
00478   virtual ~NetVConnection() {}
00479 
00480   /**
00481     PRIVATE: instances of NetVConnection cannot be created directly
00482     by the state machines. The objects are created by NetProcessor
00483     calls like accept connect_re() etc. The constructor is public
00484     just to avoid compile errors.
00485 
00486   */
00487   NetVConnection();
00488 
00489   virtual SOCKET get_socket() = 0;
00490 
00491   /** Set the TCP initial congestion window */
00492   virtual int set_tcp_init_cwnd(int init_cwnd) = 0;
00493 
00494   /** Set local sock addr struct. */
00495   virtual void set_local_addr() = 0;
00496 
00497   /** Set remote sock addr struct. */
00498   virtual void set_remote_addr() = 0;
00499 
00500   // for InkAPI
00501   bool get_is_internal_request() const {
00502     return is_internal_request;
00503   }
00504 
00505   void set_is_internal_request(bool val = false) {
00506     is_internal_request = val;
00507   }
00508 
00509   /// Get the transparency state.
00510   bool get_is_transparent() const {
00511     return is_transparent;
00512   }
00513   /// Set the transparency state.
00514   void set_is_transparent(bool state = true) {
00515     is_transparent = state;
00516   }
00517 
00518 private:
00519   NetVConnection(const NetVConnection &);
00520   NetVConnection & operator =(const NetVConnection &);
00521 
00522 protected:
00523   IpEndpoint local_addr;
00524   IpEndpoint remote_addr;
00525 
00526   bool got_local_addr;
00527   bool got_remote_addr;
00528 
00529   bool is_internal_request;
00530   /// Set if this connection is transparent.
00531   bool is_transparent;
00532 };
00533 
00534 inline
00535 NetVConnection::NetVConnection():
00536   VConnection(NULL),
00537   attributes(0),
00538   thread(NULL),
00539   got_local_addr(0),
00540   got_remote_addr(0),
00541   is_internal_request(false),
00542   is_transparent(false)
00543 {
00544   ink_zero(local_addr);
00545   ink_zero(remote_addr);
00546 }
00547 
00548 #endif

Generated by  doxygen 1.7.1