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