|  | 
|  | 1 | +#include "ipcalc.hpp" | 
|  | 2 | + | 
|  | 3 | +#include "../utils/ip_utils.hpp" | 
|  | 4 | + | 
|  | 5 | +namespace duckdb | 
|  | 6 | +{ | 
|  | 7 | +    namespace netquack | 
|  | 8 | +    { | 
|  | 9 | +        struct IPCalcData : public TableFunctionData | 
|  | 10 | +        { | 
|  | 11 | +            string ip; | 
|  | 12 | +        }; | 
|  | 13 | + | 
|  | 14 | +        struct IPCalcLocalState : public LocalTableFunctionState | 
|  | 15 | +        { | 
|  | 16 | +            std::atomic_bool done{ false }; | 
|  | 17 | +        }; | 
|  | 18 | + | 
|  | 19 | +        unique_ptr<FunctionData> IPCalcFunc::Bind (ClientContext &context, TableFunctionBindInput &input, vector<LogicalType> &return_types, vector<string> &names) | 
|  | 20 | +        { | 
|  | 21 | +            // 0. address | 
|  | 22 | +            return_types.emplace_back (LogicalType::VARCHAR); | 
|  | 23 | +            names.emplace_back ("address"); | 
|  | 24 | + | 
|  | 25 | +            // 1. netmask | 
|  | 26 | +            return_types.emplace_back (LogicalType::VARCHAR); | 
|  | 27 | +            names.emplace_back ("netmask"); | 
|  | 28 | + | 
|  | 29 | +            // 2. wildcard | 
|  | 30 | +            return_types.emplace_back (LogicalType::VARCHAR); | 
|  | 31 | +            names.emplace_back ("wildcard"); | 
|  | 32 | + | 
|  | 33 | +            // 3. network | 
|  | 34 | +            return_types.emplace_back (LogicalType::VARCHAR); | 
|  | 35 | +            names.emplace_back ("network"); | 
|  | 36 | + | 
|  | 37 | +            // 4. hostMin | 
|  | 38 | +            return_types.emplace_back (LogicalType::VARCHAR); | 
|  | 39 | +            names.emplace_back ("hostMin"); | 
|  | 40 | + | 
|  | 41 | +            // 5. hostMax | 
|  | 42 | +            return_types.emplace_back (LogicalType::VARCHAR); | 
|  | 43 | +            names.emplace_back ("hostMax"); | 
|  | 44 | + | 
|  | 45 | +            // 6. broadcast | 
|  | 46 | +            return_types.emplace_back (LogicalType::VARCHAR); | 
|  | 47 | +            names.emplace_back ("broadcast"); | 
|  | 48 | + | 
|  | 49 | +            // 7. hostsPerNet | 
|  | 50 | +            return_types.emplace_back (LogicalType::BIGINT); | 
|  | 51 | +            names.emplace_back ("hostsPerNet"); | 
|  | 52 | + | 
|  | 53 | +            // 8. ipClass | 
|  | 54 | +            return_types.emplace_back (LogicalType::VARCHAR); | 
|  | 55 | +            names.emplace_back ("ipClass"); | 
|  | 56 | + | 
|  | 57 | +            return make_uniq<IPCalcData> (); | 
|  | 58 | +        } | 
|  | 59 | + | 
|  | 60 | +        unique_ptr<LocalTableFunctionState> IPCalcFunc::InitLocal (ExecutionContext &context, TableFunctionInitInput &input, GlobalTableFunctionState *global_state_p) | 
|  | 61 | +        { | 
|  | 62 | +            return make_uniq<IPCalcLocalState> (); | 
|  | 63 | +        } | 
|  | 64 | + | 
|  | 65 | +        OperatorResultType IPCalcFunc::Function (ExecutionContext &context, TableFunctionInput &data_p, DataChunk &input, DataChunk &output) | 
|  | 66 | +        { | 
|  | 67 | +            // Check done | 
|  | 68 | +            if (((IPCalcLocalState &)*data_p.local_state).done) | 
|  | 69 | +            { | 
|  | 70 | +                return OperatorResultType::NEED_MORE_INPUT; | 
|  | 71 | +            } | 
|  | 72 | + | 
|  | 73 | +            auto &data        = data_p.bind_data->Cast<IPCalcData> (); | 
|  | 74 | +            auto &local_state = (IPCalcLocalState &)*data_p.local_state; | 
|  | 75 | + | 
|  | 76 | +            auto ip = input.data[0].GetValue (0).GetValue<string> (); | 
|  | 77 | + | 
|  | 78 | +            IPInfo info = IPCalculator::calculate (ip); | 
|  | 79 | + | 
|  | 80 | +            output.data[0].SetValue (0, info.address); | 
|  | 81 | +            output.data[1].SetValue (0, info.netmask); | 
|  | 82 | +            output.data[2].SetValue (0, info.wildcard); | 
|  | 83 | +            output.data[3].SetValue (0, info.network); | 
|  | 84 | +            output.data[4].SetValue (0, info.hostMin); | 
|  | 85 | +            output.data[5].SetValue (0, info.hostMax); | 
|  | 86 | +            output.data[6].SetValue (0, info.broadcast); | 
|  | 87 | +            output.data[7].SetValue (0, info.hostsPerNet); | 
|  | 88 | +            output.data[8].SetValue (0, info.ipClass); | 
|  | 89 | +            output.SetCardinality (1); | 
|  | 90 | + | 
|  | 91 | +            // Set done | 
|  | 92 | +            local_state.done = true; | 
|  | 93 | + | 
|  | 94 | +            return OperatorResultType::NEED_MORE_INPUT; | 
|  | 95 | +        } | 
|  | 96 | +    } // namespace netquack | 
|  | 97 | +} // namespace duckdb | 
0 commit comments