1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #pragma once
25 #include "P_EventSystem.h"
26 #include "I_Socks.h"
27 
28 #ifdef SOCKS_WITH_TS
29 #include "ParentSelection.h"
30 #include "tscore/IpMap.h"
31 #endif
32 
33 enum {
34   // types of events for Socks auth handlers
35   SOCKS_AUTH_OPEN,
36   SOCKS_AUTH_WRITE_COMPLETE,
37   SOCKS_AUTH_READ_COMPLETE,
38   SOCKS_AUTH_FILL_WRITE_BUF
39 };
40 
41 struct socks_conf_struct {
42   int socks_needed              = 0;
43   int server_connect_timeout    = 0;
44   int socks_timeout             = 100;
45   unsigned char default_version = 5;
46   char *user_name_n_passwd      = nullptr;
47   int user_name_n_passwd_len    = 0;
48 
49   int per_server_connection_attempts = 1;
50   int connection_attempts            = 0;
51 
52   // the following ports are used by SocksProxy
53   int accept_enabled       = 0;
54   int accept_port          = 0;
55   unsigned short http_port = 1080;
56 
57 #ifdef SOCKS_WITH_TS
58   IpMap ip_map;
59 #endif
60 
61 #ifndef SOCKS_WITH_TS
62   IpEndpoint server_addr;
63 #endif
64 
socks_conf_structsocks_conf_struct65   socks_conf_struct()
66 
67   {
68 #if !defined(SOCKS_WITH_TS)
69     memset(&server_addr, 0, sizeof(server_addr));
70 #endif
71   }
72 };
73 
74 extern struct socks_conf_struct *g_socks_conf_stuff;
75 
76 void start_SocksProxy(int port);
77 
78 int loadSocksAuthInfo(int fd, socks_conf_struct *socks_stuff);
79 
80 // umm.. the following typedef should take _its own_ type as one of the args
81 // not possible with C
82 // Right now just use a generic fn ptr and hide casting in an inline fn.
83 typedef int (*SocksAuthHandler)(int event, unsigned char *buf, void (**h_ptr)(void));
84 
85 TS_INLINE int
invokeSocksAuthHandler(SocksAuthHandler & h,int arg1,unsigned char * arg2)86 invokeSocksAuthHandler(SocksAuthHandler &h, int arg1, unsigned char *arg2)
87 {
88   return (h)(arg1, arg2, (void (**)(void))(&h));
89 }
90 
91 void loadSocksConfiguration(socks_conf_struct *socks_conf_stuff);
92 int socks5BasicAuthHandler(int event, unsigned char *p, void (**)(void));
93 int socks5PasswdAuthHandler(int event, unsigned char *p, void (**)(void));
94 int socks5ServerAuthHandler(int event, unsigned char *p, void (**)(void));
95 
96 class UnixNetVConnection;
97 typedef UnixNetVConnection SocksNetVC;
98 
99 struct SocksEntry : public Continuation {
100   MIOBuffer *buf         = nullptr;
101   IOBufferReader *reader = nullptr;
102 
103   SocksNetVC *netVConnection = nullptr;
104 
105   // Changed from @a ip and @a port.
106   IpEndpoint target_addr; ///< Original target address.
107   // Changed from @a server_ip, @a server_port.
108   IpEndpoint server_addr; ///< Origin server address.
109 
110   int nattempts = 0;
111 
112   Action action_;
113   int lerrno            = 0;
114   Event *timeout        = nullptr;
115   unsigned char version = 5;
116 
117   bool write_done = false;
118 
119   SocksAuthHandler auth_handler = nullptr;
120   unsigned char socks_cmd       = NORMAL_SOCKS;
121 
122 #ifdef SOCKS_WITH_TS
123   // socks server selection:
124   ParentConfigParams *server_params = nullptr;
125   HttpRequestData req_data; // We dont use any http specific fields.
126   ParentResult server_result;
127 #endif
128 
129   int startEvent(int event, void *data);
130   int mainEvent(int event, void *data);
131   void findServer();
132   void init(Ptr<ProxyMutex> &m, SocksNetVC *netvc, unsigned char socks_support, unsigned char ver);
133   void free();
134 
SocksEntrySocksEntry135   SocksEntry()
136   {
137     memset(&target_addr, 0, sizeof(target_addr));
138     memset(&server_addr, 0, sizeof(server_addr));
139   }
140 };
141 
142 typedef int (SocksEntry::*SocksEntryHandler)(int, void *);
143 
144 extern ClassAllocator<SocksEntry> socksAllocator;
145 
146 TS_INLINE void
reset()147 SocksAddrType::reset()
148 {
149   if (type != SOCKS_ATYPE_IPV4 && addr.buf) {
150     ats_free(addr.buf);
151   }
152 
153   addr.buf = nullptr;
154   type     = SOCKS_ATYPE_NONE;
155 }
156