unified socket application interface (for asterisk and maybe other apps)
[lcr.git] / message.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Route                                                          **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** message handling                                                          **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "main.h"
16
17 MESSAGES
18
19 struct message *message_first = NULL;
20 struct message **messagepointer_end = &message_first;
21
22 /* creates a new message with the given attributes. the message must be filled then. after filling, the message_put must be called */
23 struct message *message_create(int id_from, int id_to, int flow, int type)
24 {
25         struct message *message;
26
27         message = (struct message *)MALLOC(sizeof(struct message));
28         if (!message)
29                 FATAL("No memory for message.\n");
30         mmemuse++;
31
32         message->id_from = id_from;
33         message->id_to = id_to;
34         message->flow = flow;
35         message->type = type;
36
37         return(message);
38 }
39
40 /* attaches a message to the end of the message chain */
41 void message_put(struct message *message)
42 {
43         if (message->id_to == 0)
44         {
45                 PDEBUG(DEBUG_MSG, "message %s not written, because destination is 0.\n", messages_txt[message->type]);
46                 message_free(message);
47                 return;
48         }
49         
50         if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
51                 PDEBUG(DEBUG_MSG, "message %s written from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
52
53         *messagepointer_end = message;
54         messagepointer_end = &(message->next);
55 }
56
57 void message_forward(int id_from, int id_to, int flow, union parameter *param)
58 {
59         struct message *message;
60
61         /* get point to message */
62         message = (struct message *)((unsigned long)param - ((unsigned long)(&message->param) - (unsigned long)message));
63
64         /* protect, so forwarded messages are not freed after handling */
65         message->keep = 1;
66
67         message->id_from = id_from;
68         message->id_to = id_to;
69         message->flow = flow;
70         message_put(message);
71 }
72
73 /* detaches the first messages from the message chain */
74 struct message *message_get(void)
75 {
76         struct message *message;
77
78         if (!message_first)
79         {
80                 return(0);
81         }
82
83         message = message_first;
84         message_first = message->next;
85         if (!message_first)
86                 messagepointer_end = &message_first;
87
88         message->keep = 0;
89
90         if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
91
92                 PDEBUG(DEBUG_MSG, "message %s reading from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
93
94         return(message);
95 }
96
97 /* free a message */
98 void message_free(struct message *message)
99 {
100         if (message->keep)
101                 return;
102         FREE(message, sizeof(struct message));
103         mmemuse--;
104 }
105
106