Merge branch 'master' of ssh://schlaile@git.misdn.org/var/git/lcr
[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 }
53
54 struct lcr_msg *message_forward(int id_from, int id_to, int flow, union parameter *param)
55 {
56         struct lcr_msg *message;
57
58         /* get point to message */
59         message = (struct lcr_msg *)((unsigned long)param - ((unsigned long)(&message->param) - (unsigned long)message));
60
61         /* protect, so forwarded messages are not freed after handling */
62         message->keep = 1;
63
64         message->id_from = id_from;
65         message->id_to = id_to;
66         message->flow = flow;
67         message_put(message);
68
69         return(message);
70 }
71
72 /* detaches the first messages from the message chain */
73 struct lcr_msg *message_get(void)
74 {
75         struct lcr_msg *message;
76
77         if (!message_first)
78         {
79                 return(0);
80         }
81
82         message = message_first;
83         message_first = message->next;
84         if (!message_first)
85                 messagepointer_end = &message_first;
86
87         message->keep = 0;
88
89         if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
90
91                 PDEBUG(DEBUG_MSG, "message %s reading from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
92
93         return(message);
94 }
95
96 /* free a message */
97 void message_free(struct lcr_msg *message)
98 {
99         if (message->keep)
100                 return;
101         FREE(message, sizeof(struct lcr_msg));
102         mmemuse--;
103 }
104
105