ota_prov.c
上传用户:gzpyjq
上传日期:2013-01-31
资源大小:1852k
文件大小:17k
源码类别:

手机WAP编程

开发平台:

WINDOWS

  1. /*
  2.  * ota_prov.c: OTA settings and bookmarks provisioning routines
  3.  *
  4.  * This module contains routines for the SMS OTA (auto configuration) message 
  5.  * creation and manipulation for the sendota HTTP interface.
  6.  *
  7.  * Official Nokia and Ericsson WAP OTA configuration settings coded 
  8.  * by Stipe Tolj <tolj@wapme-systems.de>, Wapme Systems AG.
  9.  * 
  10.  * XML compiler by Aarno Syv鋘en <aarno@wiral.com>, Wiral Ltd.
  11.  */
  12. #include <string.h>
  13. #include "gwlib/gwlib.h"
  14. #include "msg.h"
  15. #include "sms.h"
  16. #include "ota_prov.h"
  17. #include "ota_prov_attr.h"
  18. #include "ota_compiler.h"
  19. /***************************************************************************
  20.  *
  21.  * Implementation of the internal function
  22.  */
  23. /*
  24.  * Append the User Data Header (UDH) including the lenght (UDHL). Only ports 
  25.  * UDH here - SAR UDH is added when (or if) we split the message. This is our
  26.  * *specific* WDP layer.
  27.  */
  28. static void ota_pack_udh(Msg **msg)
  29. {
  30.     (*msg)->sms.udhdata = octstr_create("");
  31.     octstr_append_from_hex((*msg)->sms.udhdata, "060504C34FC002");
  32. }
  33. /*
  34.  * Our WSP headers: Push Id, PDU type, headers, charset.
  35.  */
  36. static int ota_pack_push_headers(Msg **msg, Octstr *mime_type)
  37. {    
  38.     (*msg)->sms.msgdata = octstr_create("");
  39.     if (octstr_case_compare(mime_type, octstr_imm("settings")) == 0) {
  40.         /* PUSH ID, PDU type, header length, value length */
  41.         octstr_append_from_hex((*msg)->sms.msgdata, "01062C1F2A");
  42.         /* MIME type for settings */
  43.         octstr_format_append((*msg)->sms.msgdata, "%s", 
  44.                              "application/x-wap-prov.browser-settings");
  45.         octstr_append_from_hex((*msg)->sms.msgdata, "00");
  46.     } else if (octstr_case_compare(mime_type, octstr_imm("bookmarks")) == 0) {
  47.         /* PUSH ID, PDU type, header length, value length */
  48.         octstr_append_from_hex((*msg)->sms.msgdata, "01062D1F2B");
  49.         /* MIME type for bookmarks */
  50.         octstr_format_append((*msg)->sms.msgdata, "%s", 
  51.                              "application/x-wap-prov.browser-bookmarks");
  52.         octstr_append_from_hex((*msg)->sms.msgdata, "00");
  53.     } else {
  54.         warning(0, "Unknown MIME type in OTA request, type '%s' is unsupported.", 
  55.                 octstr_get_cstr(mime_type));
  56.         return 0;
  57.     }
  58.     /* charset UTF-8 */
  59.     octstr_append_from_hex((*msg)->sms.msgdata, "81EA");
  60.     return 1;
  61. }
  62. /***************************************************************************
  63.  *
  64.  * Implementation of the external function
  65.  */
  66. int ota_pack_message(Msg **msg, Octstr *ota_doc, Octstr *doc_type, 
  67.                      Octstr *from, Octstr *phone_number)
  68. {
  69.     Octstr *ota_binary;
  70.     *msg = msg_create(sms);
  71.     (*msg)->sms.sms_type = mt_push;
  72.     ota_pack_udh(msg);
  73.     if (!ota_pack_push_headers(msg, doc_type))
  74.         goto herror;
  75.     if (ota_compile(ota_doc, octstr_imm("UTF-8"), &ota_binary) == -1)
  76.         goto cerror;
  77.     octstr_format_append((*msg)->sms.msgdata, "%S", ota_binary);
  78.     (*msg)->sms.sender = octstr_duplicate(from);
  79.     (*msg)->sms.receiver = octstr_duplicate(phone_number);
  80.     (*msg)->sms.coding = DC_8BIT;
  81.     (*msg)->sms.time = time(NULL);
  82.     octstr_dump((*msg)->sms.msgdata, 0);
  83.     info(0, "/cgi-bin/sendota: XML request for target <%s>", octstr_get_cstr(phone_number));
  84.     octstr_destroy(ota_binary);
  85.     octstr_destroy(ota_doc);
  86.     octstr_destroy(doc_type);
  87.     octstr_destroy(from);
  88.     return 0;
  89. herror:
  90.     octstr_destroy(ota_doc);
  91.     octstr_destroy(doc_type);
  92.     octstr_destroy(from);
  93.     return -2;
  94. cerror:
  95.     octstr_destroy(ota_doc);
  96.     octstr_destroy(doc_type);
  97.     octstr_destroy(from);
  98.     return -1;
  99. }
  100. Msg *ota_tokenize_settings(CfgGroup *grp, Octstr *from, Octstr *receiver)
  101. {
  102.     Octstr *url, *desc, *ipaddr, *phonenum, *username, *passwd;
  103.     int speed, bearer, calltype, connection, security, authent;
  104.     Msg *msg;
  105.     Octstr *p;
  106.     
  107.     url = NULL;
  108.     desc = NULL;
  109.     ipaddr = NULL;
  110.     phonenum = NULL;
  111.     username = NULL;
  112.     passwd = NULL;
  113.     bearer = -1;
  114.     calltype = -1;
  115.     connection = WBXML_TOK_VALUE_PORT_9201;
  116.     security = 0;
  117.     authent = WBXML_TOK_VALUE_AUTH_PAP;
  118.     url = cfg_get(grp, octstr_imm("location"));
  119.     desc = cfg_get(grp, octstr_imm("service"));
  120.     ipaddr = cfg_get(grp, octstr_imm("ipaddress"));
  121.     phonenum = cfg_get(grp, octstr_imm("phonenumber"));
  122.     p = cfg_get(grp, octstr_imm("bearer"));
  123.     if (p != NULL) {
  124.         if (strcasecmp(octstr_get_cstr(p), "data") == 0)
  125.             bearer = WBXML_TOK_VALUE_GSM_CSD;
  126.         else
  127.             bearer = -1;
  128.         octstr_destroy(p);
  129.     }
  130.     p = cfg_get(grp, octstr_imm("calltype"));
  131.     if (p != NULL) {
  132.         if (strcasecmp(octstr_get_cstr(p), "calltype") == 0)
  133.             calltype = WBXML_TOK_VALUE_CONN_ISDN;
  134.         else
  135.             calltype = -1;
  136.         octstr_destroy(p);
  137.     }
  138.     speed = WBXML_TOK_VALUE_SPEED_9600;
  139.     p = cfg_get(grp, octstr_imm("speed"));
  140.     if (p != NULL) {
  141.         if (octstr_compare(p, octstr_imm("14400")) == 0)
  142.             speed = WBXML_TOK_VALUE_SPEED_14400;
  143.         octstr_destroy(p);
  144.     }
  145.     /* connection mode: UDP (port 9200) or TCP (port 9201)*/
  146.     p = cfg_get(grp, octstr_imm("connection"));
  147.     if (p != NULL) {
  148.         if (strcasecmp(octstr_get_cstr(p), "temp") == 0)
  149.             connection = WBXML_TOK_VALUE_PORT_9200;
  150.         else
  151.             connection = WBXML_TOK_VALUE_PORT_9201;
  152.         octstr_destroy(p);
  153.     }
  154.     /* dial in security: CHAP or PAP */
  155.     p = cfg_get(grp, octstr_imm("pppsecurity"));
  156.     if (p != NULL) {
  157.         if (strcasecmp(octstr_get_cstr(p), "on") == 0)
  158.             authent = WBXML_TOK_VALUE_AUTH_CHAP;
  159.         else
  160.             authent = WBXML_TOK_VALUE_AUTH_PAP;
  161.         octstr_destroy(p);
  162.     }
  163.     
  164.     /* WTLS: for UDP (port 9202) or TCP (port 9203) */
  165.     p = cfg_get(grp, octstr_imm("authentication"));
  166.     if (p != NULL) {
  167.         if (strcasecmp(octstr_get_cstr(p), "secure") == 0)
  168.             security = 1;
  169.         else
  170.             security = WBXML_TOK_VALUE_PORT_9201;
  171.         octstr_destroy(p);
  172.     }
  173.     if (security == 1)
  174.         connection = (connection == WBXML_TOK_VALUE_PORT_9201)? 
  175.             WBXML_TOK_VALUE_PORT_9203 : WBXML_TOK_VALUE_PORT_9202;
  176.     
  177.     username = cfg_get(grp, octstr_imm("login"));
  178.     passwd = cfg_get(grp, octstr_imm("secret"));
  179.     
  180.     msg = msg_create(sms);
  181.     /*
  182.      * Append the User Data Header (UDH) including the lenght (UDHL)
  183.      * WDP layer (start WDP headers)
  184.      */
  185.     
  186.     msg->sms.sms_type = mt_push;
  187.     msg->sms.udhdata = octstr_create("");
  188.     /* 
  189.      * Within OTA spec this is "0B0504C34FC0020003040201", but it works
  190.      * with the following too?!
  191.      */
  192.     octstr_append_from_hex(msg->sms.udhdata, "060504C34FC002");
  193.     /* WDP layer (end WDP headers) */         
  194.     /*
  195.      * WSP layer (start WSP headers)
  196.      */
  197.     
  198.     msg->sms.msgdata = octstr_create("");
  199.     /* PUSH ID, PDU type, header length, value length */
  200.     octstr_append_from_hex(msg->sms.msgdata, "01062C1F2A");
  201.     /* MIME-type: application/x-wap-prov.browser-settings */
  202.     octstr_format_append(msg->sms.msgdata, "%s", 
  203.                          "application/x-wap-prov.browser-settings");
  204.     octstr_append_from_hex(msg->sms.msgdata, "00");
  205.     /* charset UTF-8 */
  206.     octstr_append_from_hex(msg->sms.msgdata, "81EA");
  207.     /* WSP layer (end WSP headers) */
  208.     /*
  209.      * WSP layer (start WSP data field)
  210.      */
  211.     /* WBXML version 1.1 */
  212.     octstr_append_from_hex(msg->sms.msgdata, "0101");
  213.     /* charset UTF-8 */
  214.     octstr_append_from_hex(msg->sms.msgdata, "6A00");
  215.     /* CHARACTERISTIC_LIST */
  216.     octstr_append_from_hex(msg->sms.msgdata, "45");
  217.     /* CHARACTERISTIC with content and attributes */
  218.     octstr_append_from_hex(msg->sms.msgdata, "C6");
  219.     /* TYPE=ADDRESS */
  220.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_TYPE_ADDRESS);
  221.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  222.     /* bearer type */
  223.     if (bearer != -1) {
  224.         /* PARM with attributes */
  225.         octstr_append_from_hex(msg->sms.msgdata, "87");
  226.         /* NAME=BEARER, VALUE=GSM_CSD */
  227.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_BEARER);
  228.         octstr_append_char(msg->sms.msgdata, bearer);
  229.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  230.     }
  231.     /* IP address */
  232.     if (ipaddr != NULL) {
  233.         /* PARM with attributes */
  234.         octstr_append_from_hex(msg->sms.msgdata, "87");
  235.         /* NAME=PROXY, VALUE, inline string */
  236.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_PROXY);
  237.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_VALUE);
  238.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_STR_I);
  239.         octstr_append(msg->sms.msgdata, octstr_duplicate(ipaddr));
  240.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END_STR_I);
  241.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  242.     }
  243.     /* connection type */
  244.     if (connection != -1) {
  245.         /* PARM with attributes */
  246.         octstr_append_from_hex(msg->sms.msgdata, "87");
  247.         /* NAME=PORT, VALUE */
  248.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_PORT);
  249.         octstr_append_char(msg->sms.msgdata, connection);
  250.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  251.     }
  252.     /* phone number */
  253.     if (phonenum != NULL) {
  254.         /* PARM with attributes */
  255.         octstr_append_from_hex(msg->sms.msgdata, "87");
  256.         /* NAME=CSD_DIALSTRING, VALUE, inline string */
  257.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_CSD_DIALSTRING);
  258.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_VALUE);
  259.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_STR_I);
  260.         octstr_append(msg->sms.msgdata, octstr_duplicate(phonenum));
  261.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END_STR_I);
  262.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  263.     }
  264.     /* authentication */
  265.     /* PARM with attributes */
  266.     octstr_append_from_hex(msg->sms.msgdata, "87");
  267.      /* NAME=PPP_AUTHTYPE, VALUE */
  268.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_PPP_AUTHTYPE);
  269.     octstr_append_char(msg->sms.msgdata, authent);
  270.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  271.     /* user name */
  272.     if (username != NULL) {
  273.         /* PARM with attributes */
  274.         octstr_append_from_hex(msg->sms.msgdata, "87");
  275.         /* NAME=PPP_AUTHNAME, VALUE, inline string */
  276.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_PPP_AUTHNAME);
  277.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_VALUE);
  278.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_STR_I);
  279.         octstr_append(msg->sms.msgdata, octstr_duplicate(username));
  280.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END_STR_I);
  281.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  282.     }
  283.     /* password */
  284.     if (passwd != NULL) {
  285.         /* PARM with attributes */
  286.         octstr_append_from_hex(msg->sms.msgdata, "87");
  287.         /* NAME=PPP_AUTHSECRET, VALUE, inline string */
  288.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_PPP_AUTHSECRET);
  289.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_VALUE);
  290.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_STR_I);
  291.         octstr_append(msg->sms.msgdata, octstr_duplicate(passwd));
  292.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END_STR_I);
  293.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  294.     }
  295.     /* data call type */
  296.     if (calltype != -1) {
  297.         /* PARM with attributes */
  298.         octstr_append_from_hex(msg->sms.msgdata, "87");
  299.         /* NAME=CSD_CALLTYPE, VALUE */
  300.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_CSD_CALLTYPE);
  301.         octstr_append_char(msg->sms.msgdata, calltype);
  302.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  303.     }
  304.     /* speed */
  305.     /* PARM with attributes */
  306.     octstr_append_from_hex(msg->sms.msgdata, "87");
  307.     /* NAME=CSD_CALLSPEED, VALUE */
  308.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_CSD_CALLSPEED);
  309.     octstr_append_char(msg->sms.msgdata, speed);
  310.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  311.     /* end CHARACTERISTIC TYPE=ADDRESS */
  312.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  313.     /* homepage */
  314.     if (url != NULL) {
  315.         /* CHARACTERISTIC with attributes */
  316.         octstr_append_from_hex(msg->sms.msgdata, "86");
  317.         /* TYPE=URL */
  318.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_TYPE_URL);
  319.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_VALUE);
  320.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_STR_I);
  321.         octstr_append(msg->sms.msgdata, octstr_duplicate(url));
  322.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END_STR_I);
  323.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  324.     }
  325.     /* CHARACTERISTIC with content and attributes */
  326.     octstr_append_from_hex(msg->sms.msgdata, "C6");
  327.     /* TYPE=NAME */
  328.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_TYPE_NAME);
  329.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  330.     /* service description */
  331.     if (desc != NULL) {
  332.         /* PARAM with attributes */
  333.         octstr_append_from_hex(msg->sms.msgdata, "87");
  334.         /* NAME=NAME, VALUE, inline */
  335.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_NAME);
  336.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_VALUE);
  337.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_STR_I);
  338.         octstr_append(msg->sms.msgdata, octstr_duplicate(desc));
  339.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END_STR_I);
  340.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  341.     }
  342.     /* end of CHARACTERISTIC */
  343.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  344.     /* end of CHARACTERISTIC-LIST */
  345.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  346.     /* WSP layer (end WSP data field) */
  347.     msg->sms.sender = from;
  348.     msg->sms.receiver = octstr_duplicate(receiver);
  349.     msg->sms.coding = DC_8BIT;
  350.     
  351.     msg->sms.time = time(NULL);
  352.                               
  353.     octstr_destroy(url);
  354.     octstr_destroy(desc);
  355.     octstr_destroy(ipaddr);
  356.     octstr_destroy(phonenum);
  357.     octstr_destroy(username);
  358.     octstr_destroy(passwd);
  359.     return msg;
  360. }
  361. Msg *ota_tokenize_bookmarks(CfgGroup *grp, Octstr *from, Octstr *receiver)
  362. {
  363.     Octstr *url, *name;
  364.     Msg *msg;
  365.     
  366.     url = NULL;
  367.     name = NULL;
  368.     url = cfg_get(grp, octstr_imm("url"));
  369.     name = cfg_get(grp, octstr_imm("name"));
  370.     
  371.     msg = msg_create(sms);
  372.     /*
  373.      * Append the User Data Header (UDH) including the lenght (UDHL)
  374.      * WDP layer (start WDP headers)
  375.      */
  376.     
  377.     msg->sms.sms_type = mt_push;
  378.     msg->sms.udhdata = octstr_create("");
  379.     octstr_append_from_hex(msg->sms.udhdata, "060504C34FC002");
  380.     /* WDP layer (end WDP headers) */
  381.     /*
  382.      * WSP layer (start WSP headers)
  383.      */
  384.     
  385.     msg->sms.msgdata = octstr_create("");
  386.     /* PUSH ID, PDU type, header length, value length */
  387.     octstr_append_from_hex(msg->sms.msgdata, "01062D1F2B");
  388.     /* MIME-type: application/x-wap-prov.browser-bookmarks */
  389.     octstr_format_append(msg->sms.msgdata, "%s", 
  390.                          "application/x-wap-prov.browser-bookmarks");
  391.     octstr_append_from_hex(msg->sms.msgdata, "00");
  392.     /* charset UTF-8 */
  393.     octstr_append_from_hex(msg->sms.msgdata, "81EA");
  394.     /* WSP layer (end WSP headers) */
  395.     /*
  396.      * WSP layer (start WSP data field)
  397.      */
  398.     /* WBXML version 1.1 */
  399.     octstr_append_from_hex(msg->sms.msgdata, "0101");
  400.     /* charset UTF-8 */
  401.     octstr_append_from_hex(msg->sms.msgdata, "6A00");
  402.     /* CHARACTERISTIC_LIST */
  403.     octstr_append_from_hex(msg->sms.msgdata, "45");
  404.     /* CHARACTERISTIC with content and attributes */
  405.     octstr_append_from_hex(msg->sms.msgdata, "C6");
  406.     /* TYPE=BOOKMARK */
  407.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_TYPE_BOOKMARK);
  408.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  409.     /* name */
  410.     if (name != NULL) {
  411.         /* PARM with attributes */
  412.         octstr_append_from_hex(msg->sms.msgdata, "87");
  413.         /* NAME=PROXY, VALUE, inline string */
  414.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_NAME);
  415.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_VALUE);
  416.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_STR_I);
  417.         octstr_append(msg->sms.msgdata, octstr_duplicate(name));
  418.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END_STR_I);
  419.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  420.     }
  421.     /* URL */
  422.     if (url != NULL) {
  423.         /* PARM with attributes */
  424.         octstr_append_from_hex(msg->sms.msgdata, "87");
  425.         /* NAME=PROXY, VALUE, inline string */
  426.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_NAME_URL);
  427.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_VALUE);
  428.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_STR_I);
  429.         octstr_append(msg->sms.msgdata, octstr_duplicate(url));
  430.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END_STR_I);
  431.         octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  432.     }
  433.     /* end of CHARACTERISTIC */
  434.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  435.     /* end of CHARACTERISTIC-LIST */
  436.     octstr_append_char(msg->sms.msgdata, WBXML_TOK_END);
  437.     /* WSP layer (end WSP data field) */
  438.     msg->sms.sender = from;
  439.     msg->sms.receiver = octstr_duplicate(receiver);
  440.     msg->sms.coding = DC_8BIT;
  441.     
  442.     msg->sms.time = time(NULL);
  443.                               
  444.     octstr_destroy(name);
  445.     octstr_destroy(url);
  446.     return msg;
  447. }