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

I_NetProcessor.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 __I_NETPROCESSOR_H__
00026 #define __I_NETPROCESSOR_H__
00027 
00028 #include "I_EventSystem.h"
00029 #include "I_Socks.h"
00030 struct socks_conf_struct;
00031 #define NET_CONNECT_TIMEOUT 30
00032 
00033 struct NetVCOptions;
00034 
00035 /**
00036   This is the heart of the Net system. Provides common network APIs,
00037   like accept, connect etc. It performs network I/O on behalf of a
00038   state machine.
00039 
00040 */
00041 class NetProcessor:public Processor
00042 {
00043 public:
00044   /** Options for @c accept.
00045    */
00046   struct AcceptOptions {
00047     typedef AcceptOptions self; ///< Self reference type.
00048 
00049     /// Port on which to listen.
00050     /// 0 => don't care, which is useful if the socket is already bound.
00051     int local_port;
00052     /// Local address to bind for accept.
00053     /// If not set -> any address.
00054     IpAddr local_ip;
00055     /// IP address family.
00056     /// @note Ignored if an explicit incoming address is set in the
00057     /// the configuration (@c local_ip). If neither is set IPv4 is used.
00058     int ip_family;
00059     /// Should we use accept threads? If so, how many?
00060     int accept_threads;
00061     /// Event type to generate on accept.
00062     EventType etype;
00063     /** If @c true, the continuation is called back with
00064         @c NET_EVENT_ACCEPT_SUCCEED
00065         or @c NET_EVENT_ACCEPT_FAILED on success and failure resp.
00066     */
00067     bool f_callback_on_open;
00068     /** Accept only on the loopback address.
00069         Default: @c false.
00070      */
00071     bool localhost_only;
00072     /// Are frequent accepts expected?
00073     /// Default: @c false.
00074     bool frequent_accept;
00075     bool backdoor;
00076 
00077     /// Socket receive buffer size.
00078     /// 0 => OS default.
00079     int recv_bufsize;
00080     /// Socket transmit buffer size.
00081     /// 0 => OS default.
00082     int send_bufsize;
00083     /// Socket options for @c sockopt.
00084     /// 0 => do not set options.
00085     uint32_t sockopt_flags;
00086     uint32_t packet_mark;
00087     uint32_t packet_tos;
00088 
00089     /** Transparency on client (user agent) connection.
00090         @internal This is irrelevant at a socket level (since inbound
00091         transparency must be set up when the listen socket is created)
00092         but it's critical that the connection handling logic knows
00093         whether the inbound (client / user agent) connection is
00094         transparent.
00095     */
00096     bool f_inbound_transparent;
00097 
00098     /// Default constructor.
00099     /// Instance is constructed with default values.
00100     AcceptOptions() { this->reset(); }
00101     /// Reset all values to defaults.
00102     self& reset();
00103   };
00104 
00105   /**
00106     Accept connections on a port.
00107 
00108     Callbacks:
00109       - cont->handleEvent( NET_EVENT_ACCEPT, NetVConnection *) is
00110         called for each new connection
00111       - cont->handleEvent(EVENT_ERROR,-errno) on a bad error
00112 
00113     Re-entrant callbacks (based on callback_on_open flag):
00114       - cont->handleEvent(NET_EVENT_ACCEPT_SUCCEED, 0) on successful
00115         accept init
00116       - cont->handleEvent(NET_EVENT_ACCEPT_FAILED, 0) on accept
00117         init failure
00118 
00119     @param cont Continuation to be called back with events this
00120       continuation is not locked on callbacks and so the handler must
00121       be re-entrant.
00122     @param opt Accept options.
00123     @return Action, that can be cancelled to cancel the accept. The
00124       port becomes free immediately.
00125    */
00126   inkcoreapi virtual Action * accept(
00127     Continuation * cont,
00128     AcceptOptions const& opt = DEFAULT_ACCEPT_OPTIONS
00129   );
00130 
00131   /**
00132     Accepts incoming connections on port. Accept connections on port.
00133     Accept is done on all net threads and throttle limit is imposed
00134     if frequent_accept flag is true. This is similar to the accept
00135     method described above. The only difference is that the list
00136     of parameter that is takes is limited.
00137 
00138     Callbacks:
00139       - cont->handleEvent( NET_EVENT_ACCEPT, NetVConnection *) is called for each new connection
00140       - cont->handleEvent(EVENT_ERROR,-errno) on a bad error
00141 
00142     Re-entrant callbacks (based on callback_on_open flag):
00143       - cont->handleEvent(NET_EVENT_ACCEPT_SUCCEED, 0) on successful accept init
00144       - cont->handleEvent(NET_EVENT_ACCEPT_FAILED, 0) on accept init failure
00145 
00146     @param cont Continuation to be called back with events this
00147       continuation is not locked on callbacks and so the handler must
00148       be re-entrant.
00149     @param listen_socket_in if passed, used for listening.
00150     @param opt Accept options.
00151     @return Action, that can be cancelled to cancel the accept. The
00152       port becomes free immediately.
00153 
00154   */
00155   virtual Action *main_accept(
00156     Continuation * cont,
00157     SOCKET listen_socket_in,
00158     AcceptOptions const& opt = DEFAULT_ACCEPT_OPTIONS
00159   );
00160 
00161   /**
00162     Open a NetVConnection for connection oriented I/O. Connects
00163     through sockserver if netprocessor is configured to use socks
00164     or is socks parameters to the call are set.
00165 
00166     Re-entrant callbacks:
00167       - On success calls: c->handleEvent(NET_EVENT_OPEN, NetVConnection *)
00168       - On failure calls: c->handleEvent(NET_EVENT_OPEN_FAILED, -errno)
00169 
00170     @note Connection may not have been established when cont is
00171       call back with success. If this behaviour is desired use
00172       synchronous connect connet_s method.
00173 
00174     @see connect_s()
00175 
00176     @param cont Continuation to be called back with events.
00177     @param addr target address and port to connect to.
00178     @param options @see NetVCOptions.
00179 
00180   */
00181 
00182   inkcoreapi Action *connect_re(
00183     Continuation * cont,
00184     sockaddr const* addr,
00185     NetVCOptions * options = NULL
00186   );
00187 
00188   /**
00189     Open a NetVConnection for connection oriented I/O. This call
00190     is simliar to connect method except that the cont is called
00191     back only after the connections has been established. In the
00192     case of connect the cont could be called back with NET_EVENT_OPEN
00193     event and OS could still be in the process of establishing the
00194     connection. Re-entrant Callbacks: same as connect. If unix
00195     asynchronous type connect is desired use connect_re().
00196 
00197     @param cont Continuation to be called back with events.
00198     @param addr Address to which to connect (includes port).
00199     @param timeout for connect, the cont will get NET_EVENT_OPEN_FAILED
00200       if connection could not be established for timeout msecs. The
00201       default is 30 secs.
00202     @param options @see NetVCOptions.
00203 
00204     @see connect_re()
00205 
00206   */
00207   Action *connect_s(
00208     Continuation * cont,
00209     sockaddr const* addr,
00210     int timeout = NET_CONNECT_TIMEOUT,
00211     NetVCOptions * opts = NULL
00212   );
00213 
00214   /**
00215     Starts the Netprocessor. This has to be called before doing any
00216     other net call.
00217 
00218     @param number_of_net_threads is not used. The net processor
00219       uses the Event Processor threads for its activity.
00220 
00221   */
00222   virtual int start(int number_of_net_threads, size_t stacksize) = 0;
00223 
00224   inkcoreapi virtual NetVConnection *allocate_vc(EThread *) = 0;
00225 
00226   /** Private constructor. */
00227   NetProcessor()
00228   {
00229   };
00230 
00231   /** Private destructor. */
00232   virtual ~ NetProcessor() {
00233   };
00234 
00235   /** This is MSS for connections we accept (client connections). */
00236   static int accept_mss;
00237 
00238   //
00239   // The following are required by the SOCKS protocol:
00240   //
00241   // Either the configuration variables will give your a regular
00242   // expression for all the names that are to go through the SOCKS
00243   // server, or will give you a list of domain names which should *not* go
00244   // through SOCKS. If the SOCKS option is set to false then, these
00245   // variables (regular expression or list) should be set
00246   // appropriately. If it is set to TRUE then, in addition to supplying
00247   // the regular expression or the list, the user should also give the
00248   // the ip address and port number for the SOCKS server (use
00249   // appropriate defaults)
00250 
00251   /* shared by regular netprocessor and ssl netprocessor */
00252   static socks_conf_struct *socks_conf_stuff;
00253 
00254   /// Default options instance.
00255   static AcceptOptions const DEFAULT_ACCEPT_OPTIONS;
00256 
00257 private:
00258 
00259   /** @note Not implemented. */
00260   virtual int stop()
00261   {
00262     ink_release_assert(!"NetProcessor::stop not implemented");
00263     return 1;
00264   }
00265 
00266   NetProcessor(const NetProcessor &);
00267   NetProcessor & operator =(const NetProcessor &);
00268 
00269 };
00270 
00271 
00272 /**
00273   Global NetProcessor singleton object for making net calls. All
00274   net processor calls like connect, accept, etc are made using this
00275   object.
00276 
00277   @code
00278     netProcesors.accept(my_cont, ...);
00279     netProcessor.connect_re(my_cont, ...);
00280   @endcode
00281 
00282 */
00283 extern inkcoreapi NetProcessor& netProcessor;
00284 
00285 /**
00286   Global netProcessor singleton object for making ssl enabled net
00287   calls. As far as the SM is concerned this behaves exactly like
00288   netProcessor. The only difference is that the connections are
00289   over ssl.
00290 
00291 */
00292 extern inkcoreapi NetProcessor& sslNetProcessor;
00293 
00294 #endif

Generated by  doxygen 1.7.1