Ports can now be specified by number or by name.
[lcr.git] / crypt.cpp
index 65890c3..87a5dde 100644 (file)
--- a/crypt.cpp
+++ b/crypt.cpp
@@ -140,14 +140,10 @@ the message type is encoded as element
 
 */
 
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <sys/types.h>
+#include "main.h"
 #ifdef CRYPTO
 #include <openssl/rsa.h>
 #endif
-#include "main.h"
 
 
 /* convert key string to binary key vector
@@ -210,7 +206,7 @@ unsigned char *crypt_key(unsigned char *key, int *binary_len)
 /*
  * support routine to get cpu speed
  */
-static unsigned long get_bogomips(void)
+static unsigned int get_bogomips(void)
 {
        FILE *fp;
        char buffer[64], *p;
@@ -248,9 +244,9 @@ static unsigned long get_bogomips(void)
 /*
  * crc 32 stuff
  */
-static unsigned long crc_reflect(unsigned long ref, char ch)
+static unsigned int crc_reflect(unsigned int ref, char ch)
 {
-       unsigned long value = 0;
+       unsigned int value = 0;
        int i;
 
        i = 1;
@@ -264,12 +260,12 @@ static unsigned long crc_reflect(unsigned long ref, char ch)
        return(value);
 }
 
-static unsigned long crc32_table[256];
+static unsigned int crc32_table[256];
 static int crc_initialized = 0;
 
 void crc_init(void)
 {
-       unsigned long ulPolynomial = 0x04c11db7;
+       unsigned int ulPolynomial = 0x04c11db7;
        int i, j;
 
        i = 0;
@@ -288,15 +284,12 @@ void crc_init(void)
        crc_initialized = 1;
 }
 
-unsigned long crc32(unsigned char *data, int len)
+unsigned int crc32(unsigned char *data, int len)
 {
-       unsigned long crc = 0xffffffff;
+       unsigned int crc = 0xffffffff;
 
        if (!crc_initialized)
-       {
-               PERROR("crc not initialized, exitting...");
-               exit(-1);
-       }
+               FATAL("crc not initialized, exitting...");
 
        while (len--)
                crc = (crc >> 8) ^ crc32_table[(crc & 0xFF) ^ *data++];
@@ -307,7 +300,7 @@ unsigned long crc32(unsigned char *data, int len)
 CM_ST_NAMES
 
 /* give name of state */
-static char *statename(int state)
+static const char *statename(int state)
 {
        if (state>=0 && state<cm_st_num)
                return(cm_st_name[state]);
@@ -577,8 +570,7 @@ static void *keyengine_child(void *arg)
 
        /* exit process */
        apppbx->ea_endpoint->ep_use--;
-       memset(args, 0, sizeof(struct auth_args));
-       free(args);
+       FREE(args, sizeof(struct auth_args));
        amemuse--;
        return(NULL);
 }
@@ -595,14 +587,7 @@ void EndpointAppPBX::cryptman_keyengine(int job)
                return;
        }
 
-       arg = (struct auth_args *)calloc(1, sizeof(struct auth_args));
-       if (!arg)
-       {
-               PERROR("failed to alloc memory.\n");
-               e_crypt_keyengine_return = -1;
-               return;
-       }
-
+       arg = (struct auth_args *)MALLOC(sizeof(struct auth_args));
        arg->apppbx = this;
        arg->job = job;
        e_crypt_keyengine_return = 0;
@@ -671,7 +656,7 @@ void EndpointAppPBX::cryptman_handler(void)
 void EndpointAppPBX::cr_ident(int message, unsigned char *param, int len)
 {
        unsigned char buf[4], *p;
-       unsigned long bogomips = 0, ran;
+       unsigned int bogomips = 0, ran;
        int l;
 
        l = CM_SIZEOFINF(CM_INFO_RANDOM);
@@ -1156,7 +1141,7 @@ CM_MSG_NAMES
 
 void EndpointAppPBX::cryptman_message(int message, unsigned char *param, int len)
 {
-       char *msgtext = "<<UNKNOWN MESSAGE>>";
+       const char *msgtext = "<<UNKNOWN MESSAGE>>";
 
        if (message>=0 && message<cm_msg_num)
                msgtext = cm_msg_name[message];
@@ -1462,7 +1447,7 @@ unsigned char *EndpointAppPBX::cryptman_getinf(unsigned char *buf, int element,
  */
 void EndpointAppPBX::cryptman_msg2peer(unsigned char *buf)
 {
-       struct message *message;
+       struct lcr_msg *message;
        unsigned char *p = buf;
        int len = 0;
        int l;
@@ -1504,7 +1489,7 @@ void EndpointAppPBX::cryptman_msg2peer(unsigned char *buf)
  */
 void EndpointAppPBX::cryptman_msg2crengine(int msg, unsigned char *buf, int len)
 {
-       struct message *message;
+       struct lcr_msg *message;
 
        if (len > (int)sizeof(message->param.crypt.data))
        {
@@ -1521,7 +1506,7 @@ void EndpointAppPBX::cryptman_msg2crengine(int msg, unsigned char *buf, int len)
 
        if (options.deb & DEBUG_CRYPT)
        {
-               char *msgtext = "<<UNKNOWN MESSAGE>>";
+               const char *msgtext = "<<UNKNOWN MESSAGE>>";
 
                if (msg>=0 && msg<cm_msg_num)
                        msgtext = cm_msg_name[msg];
@@ -1531,11 +1516,11 @@ void EndpointAppPBX::cryptman_msg2crengine(int msg, unsigned char *buf, int len)
 
 /* send message to user
  */
-void EndpointAppPBX::cryptman_msg2user(int msg, char *text)
+void EndpointAppPBX::cryptman_msg2user(int msg, const char *text)
 {
-       struct message *message;
+       struct lcr_msg *message;
        /* send message */
-       message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_call_id, EPOINT_TO_CALL, MESSAGE_CRYPT);
+       message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_CRYPT);
        message->param.crypt.type = msg;
        if (!text)
                text = "";
@@ -1549,7 +1534,7 @@ void EndpointAppPBX::cryptman_msg2user(int msg, char *text)
 
        if (options.deb & DEBUG_CRYPT)
        {
-               char *msgtext = "<<UNKNOWN MESSAGE>>";
+               const char *msgtext = "<<UNKNOWN MESSAGE>>";
 
                if (msg>=0 && msg<cm_msg_num)
                        msgtext = cm_msg_name[msg];
@@ -1587,7 +1572,7 @@ void EndpointAppPBX::cryptman_timeout(int secs)
  */
 int cryptman_encode_bch(unsigned char *data, int len, unsigned char *buf, int buf_len)
 {
-       unsigned long crc;
+       unsigned int crc;
        int overhead = 18;
 
        len--; /* without null-termination */
@@ -1628,7 +1613,7 @@ int cryptman_encode_bch(unsigned char *data, int len, unsigned char *buf, int bu
 void PmISDN::cryptman_listen_bch(unsigned char *p, int l)
 {
        int i;
-       struct message *message;
+       struct lcr_msg *message;
 
        retry:
        if (!l)
@@ -1802,8 +1787,8 @@ void PmISDN::cryptman_listen_bch(unsigned char *p, int l)
  */
 void EndpointAppPBX::encrypt_shared(void)
 {
-       struct message *message;
-       char *errstr = "";
+       struct lcr_msg *message;
+       const char *errstr = "";
        class Port *port;
        int type, key_len;
        unsigned char *key;
@@ -1814,7 +1799,7 @@ void EndpointAppPBX::encrypt_shared(void)
        if (e_crypt != CRYPT_OFF)
        {
                PDEBUG(DEBUG_EPOINT, "EPOINT(%d) encryption in progress, so we request the current message.\n", ea_endpoint->ep_serial);
-               message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_call_id, EPOINT_TO_CALL, MESSAGE_CRYPT);
+               message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_CRYPT);
                message->param.crypt.type = CU_INFO_REQ;
                message_put(message);
                return;
@@ -1832,8 +1817,8 @@ void EndpointAppPBX::encrypt_shared(void)
        }
 
        /* check the key for the call */
-       if (port->p_type==PORT_TYPE_DSS1_TE_OUT || port->p_type==PORT_TYPE_DSS1_NT_OUT || port->p_type==PORT_TYPE_SIP_OUT)
-               ret = parse_secrets((char *)e_ext.number, (char *)port->p_dialinginfo.number, &auth_pointer, &crypt_pointer, &key_pointer);
+       if (port->p_type==PORT_TYPE_DSS1_TE_OUT || port->p_type==PORT_TYPE_DSS1_NT_OUT)
+               ret = parse_secrets((char *)e_ext.number, (char *)port->p_dialinginfo.id, &auth_pointer, &crypt_pointer, &key_pointer);
        else
        {
                if (!port->p_callerinfo.id[0])
@@ -1842,7 +1827,7 @@ void EndpointAppPBX::encrypt_shared(void)
                        errstr = "No Remote ID";
                        goto reject;
                }
-               ret = parse_secrets((char *)e_ext.number, (char *)numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype), &auth_pointer, &crypt_pointer, &key_pointer);
+               ret = parse_secrets((char *)e_ext.number, (char *)numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international), &auth_pointer, &crypt_pointer, &key_pointer);
        }
        if (!ret)
        {
@@ -1896,7 +1881,7 @@ void EndpointAppPBX::encrypt_shared(void)
 //     SPRINT(e_crypt_display, "Shared Key");
        e_crypt = CRYPT_SWAIT;
        /* sending activation */
-       message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_call_id, EPOINT_TO_CALL, MESSAGE_CRYPT);
+       message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_CRYPT);
        message->param.crypt.type = CU_ACTS_REQ;
        message->param.crypt.len = key_len;
        memcpy(message->param.crypt.data, key, key_len);
@@ -1908,15 +1893,15 @@ void EndpointAppPBX::encrypt_shared(void)
  */
 void EndpointAppPBX::encrypt_keyex(void)
 {
-       struct message *message;
-       char *errstr = "";
+       struct lcr_msg *message;
+       const char *errstr = "";
        class Port *port;
 
        /* redisplay current crypt display */
        if (e_crypt != CRYPT_OFF)
        {
                PDEBUG(DEBUG_EPOINT, "EPOINT(%d) encryption in progress, so we request the current message.\n", ea_endpoint->ep_serial);
-               message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_call_id, EPOINT_TO_CALL, MESSAGE_CRYPT);
+               message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_CRYPT);
                message->param.crypt.type = CU_INFO_REQ;
                message_put(message);
                return;
@@ -1948,7 +1933,7 @@ void EndpointAppPBX::encrypt_keyex(void)
        message_put(message);
        e_crypt = CRYPT_KWAIT;
        /* sending activation */
-       message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_call_id, EPOINT_TO_CALL, MESSAGE_CRYPT);
+       message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_CRYPT);
        message->param.crypt.type = CU_ACTK_REQ;
        message_put(message);
 #endif /* CRYPTO */
@@ -1959,7 +1944,7 @@ void EndpointAppPBX::encrypt_keyex(void)
  */
 void EndpointAppPBX::encrypt_off(void)
 {
-       struct message *message;
+       struct lcr_msg *message;
 
        if (e_crypt!=CRYPT_ON && e_crypt!=CRYPT_OFF)
        {
@@ -1986,7 +1971,7 @@ void EndpointAppPBX::encrypt_off(void)
        message_put(message);
        e_crypt = CRYPT_RELEASE;
        /* sending activation */
-       message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_call_id, EPOINT_TO_CALL, MESSAGE_CRYPT);
+       message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_CRYPT);
        message->param.crypt.type = CU_DACT_REQ;
        message_put(message);
 }
@@ -1996,7 +1981,7 @@ void EndpointAppPBX::encrypt_off(void)
  */
 void EndpointAppPBX::encrypt_result(int msg, char *text)
 {
-       struct message *message;
+       struct lcr_msg *message;
 
        switch(msg)
        {
@@ -2044,3 +2029,4 @@ void EndpointAppPBX::encrypt_result(int msg, char *text)
        }
 }
 
+