"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.4.178/src/ncdrivers/anysms/anysms.cpp" (16 Jun 2020, 6731 Bytes) of package /linux/misc/netxms-3.4.178.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "anysms.cpp" see the Fossies "Dox" file reference documentation.

    1 /* 
    2 ** NetXMS - Network Management System
    3 ** SMS driver for any-sms.biz service
    4 ** Copyright (C) 2014-2019 Raden Solutions
    5 **
    6 ** This program is free software; you can redistribute it and/or modify
    7 ** it under the terms of the GNU Lesser General Public License as published by
    8 ** the Free Software Foundation; either version 3 of the License, or
    9 ** (at your option) any later version.
   10 **
   11 ** This program is distributed in the hope that it will be useful,
   12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 ** GNU General Public License for more details.
   15 **
   16 ** You should have received a copy of the GNU Lesser General Public License
   17 ** along with this program; if not, write to the Free Software
   18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   19 **
   20 ** File: anysms.cpp
   21 **
   22 **/
   23 
   24 #include <ncdrv.h>
   25 #include <nms_util.h>
   26 #include <curl/curl.h>
   27 
   28 #ifndef CURL_MAX_HTTP_HEADER
   29 // workaround for older cURL versions
   30 #define CURL_MAX_HTTP_HEADER CURL_MAX_WRITE_SIZE
   31 #endif
   32 
   33 #define DEBUG_TAG _T("ncd.anysms")
   34 
   35 /**
   36  * Request data for cURL call
   37  */
   38 struct RequestData
   39 {
   40    size_t size;
   41    size_t allocated;
   42    char *data;
   43 };
   44 
   45 /**
   46  * Any SMS driver class
   47  */
   48 class AnySMSDriver : public NCDriver
   49 {
   50 private:
   51    char m_login[128];
   52    char m_password[128];
   53    char m_sender[64];
   54    char m_gateway[64];
   55 
   56 public:
   57    AnySMSDriver(Config *config);
   58    virtual bool send(const TCHAR *recipient, const TCHAR *subject, const TCHAR *body) override;
   59 };
   60 
   61 /**
   62  * Init driver
   63  */
   64 AnySMSDriver::AnySMSDriver(Config *config)
   65 {
   66    strcpy(m_login, "user");
   67    strcpy(m_password, "password");
   68    strcpy(m_sender, "NETXMS");
   69    strcpy(m_gateway, "28");
   70 
   71    nxlog_debug_tag(DEBUG_TAG, 1, _T("Driver loaded"));
   72    nxlog_debug_tag(DEBUG_TAG, 3, _T("cURL version: %hs"), curl_version());
   73 #if defined(_WIN32) || HAVE_DECL_CURL_VERSION_INFO
   74    curl_version_info_data *version = curl_version_info(CURLVERSION_NOW);
   75    char protocols[1024] = {0};
   76    const char * const *p = version->protocols;
   77    while (*p != NULL)
   78    {
   79       strncat(protocols, *p, strlen(protocols) - 1);
   80       strncat(protocols, " ", strlen(protocols) - 1);
   81       p++;
   82    }
   83    nxlog_debug_tag(DEBUG_TAG, 3, _T("cURL supported protocols: %hs"), protocols);
   84 #endif
   85    NX_CFG_TEMPLATE configTemplate[] = 
   86     {
   87         { _T("login"), CT_MB_STRING, 0, 0, sizeof(m_login), 0, m_login },   
   88         { _T("password"), CT_MB_STRING, 0, 0, sizeof(m_password), 0, m_password },  
   89         { _T("sender"), CT_MB_STRING, 0, 0, sizeof(m_sender), 0,    m_sender }, 
   90         { _T("gateway"), CT_MB_STRING, 0, 0, sizeof(m_gateway), 0,  m_gateway },
   91         { _T(""), CT_END_OF_LIST, 0, 0, 0, 0, NULL }
   92     };
   93 
   94     config->parseTemplate(_T("AnySMS"), configTemplate);
   95 }
   96 
   97 /**
   98  * Callback for processing data received from cURL
   99  */
  100 static size_t OnCurlDataReceived(char *ptr, size_t size, size_t nmemb, void *userdata)
  101 {
  102    RequestData *data = (RequestData *)userdata;
  103    if ((data->allocated - data->size) < (size * nmemb))
  104    {
  105       char *newData = (char *)realloc(data->data, data->allocated + CURL_MAX_HTTP_HEADER);
  106       if (newData == NULL)
  107       {
  108          return 0;
  109       }
  110       data->data = newData;
  111       data->allocated += CURL_MAX_HTTP_HEADER;
  112    }
  113 
  114    memcpy(data->data + data->size, ptr, size * nmemb);
  115    data->size += size * nmemb;
  116 
  117    return size * nmemb;
  118 }
  119 
  120 /**
  121  * Send SMS
  122  */
  123 bool AnySMSDriver::send(const TCHAR *recipient, const TCHAR *subject, const TCHAR *body)
  124 {
  125    bool success = false;
  126 
  127     nxlog_debug_tag(DEBUG_TAG, 4, _T("phone=\"%s\", text=\"%s\""), recipient, body);
  128 
  129    CURL *curl = curl_easy_init();
  130    if (curl != NULL)
  131    {
  132 #if HAVE_DECL_CURLOPT_NOSIGNAL
  133       curl_easy_setopt(curl, CURLOPT_NOSIGNAL, (long)1);
  134 #endif
  135 
  136       curl_easy_setopt(curl, CURLOPT_HEADER, (long)0); // do not include header in data
  137       curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
  138       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &OnCurlDataReceived);
  139       curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  140 
  141       RequestData *data = (RequestData *)malloc(sizeof(RequestData));
  142       memset(data, 0, sizeof(RequestData));
  143       curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);
  144 
  145 #ifdef UNICODE
  146       char *mbphone = MBStringFromWideString(recipient);
  147       char *mbmsg = MBStringFromWideString(body);
  148       char *phone = curl_easy_escape(curl, mbphone, 0);
  149       char *msg = curl_easy_escape(curl, mbmsg, 0);
  150       MemFree(mbphone);
  151       MemFree(mbmsg);
  152 #else
  153       char *phone = curl_easy_escape(curl, recipient, 0);
  154       char *msg = curl_easy_escape(curl, body, 0);
  155 #endif
  156 
  157       char url[4096];
  158       snprintf(url, 4096, "http://gateway.any-sms.biz/send_sms.php?id=%s&pass=%s&text=%s&nummer=%s&gateway=%s&absender=%s",
  159                m_login, m_password, msg, phone, m_gateway, m_sender);
  160       nxlog_debug_tag(DEBUG_TAG, 4, _T("URL set to \"%hs\""), url);
  161 
  162       curl_free(phone);
  163       curl_free(msg);
  164 
  165       if (curl_easy_setopt(curl, CURLOPT_URL, url) == CURLE_OK)
  166       {
  167          if (curl_easy_perform(curl) == CURLE_OK)
  168          {
  169             nxlog_debug_tag(DEBUG_TAG, 4, _T("%d bytes received"), data->size);
  170             if (data->allocated > 0)
  171             {
  172                data->data[data->size] = 0;
  173                if (!strncmp(data->data, "Err:", 4))
  174                {
  175                   int code = strtol(data->data + 4, NULL, 10);
  176                   if (code == 0)
  177                   {
  178                    nxlog_debug_tag(DEBUG_TAG, 4, _T("Success"));
  179                      success = true;
  180                   }
  181                   else
  182                   {
  183                    nxlog_debug_tag(DEBUG_TAG, 4, _T("Error response (%d)"), code);
  184                   }
  185                }
  186                else
  187                {
  188                 nxlog_debug_tag(DEBUG_TAG, 4, _T("Unexpected response (%hs)"), data->data);
  189                }
  190             }
  191          }
  192          else
  193          {
  194             nxlog_debug_tag(DEBUG_TAG, 4, _T("Call to curl_easy_perform() failed"));
  195          }
  196       }
  197       else
  198       {
  199         nxlog_debug_tag(DEBUG_TAG, 4, _T("Call to curl_easy_setopt(CURLOPT_URL) failed"));
  200       }
  201       MemFree(data->data);
  202       MemFree(data);
  203       curl_easy_cleanup(curl);
  204    }
  205    else
  206    {
  207     nxlog_debug_tag(DEBUG_TAG, 4, _T("Call to curl_easy_init() failed"));
  208    }
  209 
  210    return success;
  211 }
  212 
  213 /**
  214  * Driver entry point
  215  */
  216 DECLARE_NCD_ENTRY_POINT(AnySMS, NULL)
  217 {
  218    if (!InitializeLibCURL())
  219    {
  220       nxlog_debug_tag(DEBUG_TAG, 1, _T("cURL initialization failed"));
  221       return NULL;
  222    }
  223    return new AnySMSDriver(config);
  224 }
  225 
  226 #ifdef _WIN32
  227 
  228 /**
  229  * DLL Entry point
  230  */
  231 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  232 {
  233     if (dwReason == DLL_PROCESS_ATTACH)
  234         DisableThreadLibraryCalls(hInstance);
  235     return TRUE;
  236 }
  237 
  238 #endif