backup
[lcr.git] / asterisk_client.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** Asterisk socket client                                                    **
9 **                                                                           **
10 \*****************************************************************************/
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include "macro.h"
25 #include "extension.h"
26 #include "message.h"
27 #include "admin.h"
28
29 int sock;
30
31 struct admin_list {
32         struct admin_list *next;
33         struct admin_msg msg;
34 } *admin_first = NULL;
35
36 /*
37  * enque message from asterisk
38  */
39 int admin_asterisk(int message_type, union parameter *param)
40 {
41         struct admin_list *admin, **adminp;
42
43         adminp = &admin_first;
44         while(*adminp)
45                 adminp = &((*adminp)->next);
46         admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
47         *adminp = admin;
48
49         admin->msg.type = message_type;
50         memcpy(&admin->msg.param, param, sizeof(union parameter));
51
52         return(0);
53 }
54
55
56 /* asterisk handler
57  * warning! not thread safe
58  * returns -1 for socket error, 0 for no work, 1 for work
59  */
60 int handle_socket(void)
61 {
62         int work = 0;
63         int len;
64         struct admin_message msg;
65         struct admin_list *admin;
66
67         /* read from socket */
68         len = read(sock, &msg, sizeof(msg));
69         if (len == 0)
70         {
71                 printf("Socket closed\n");
72                 return(-1); // socket closed
73         }
74         if (len > 0)
75         {
76                 if (len != sizeof(msg))
77                 {
78                         fprintf(stderr, "Socket short read (%d)\n", len);
79                         return(-1); // socket error
80                 }
81                 if (msg.message != ADMIN_MESSAGE)
82                 {
83                         fprintf(stderr, "Socket received illegal message %d\n", msg.message);
84                         return(-1); // socket error
85                 }
86                 printf("message received %d\n", msg.u.msg.type);
87                 work = 1;
88         } else
89         {
90                 if (errno != EWOULDBLOCK)
91                 {
92                         fprintf(stderr, "Socket error %d\n", errno);
93                         return(-1);
94                 }
95         }
96
97         /* write to socket */
98         if (!admin_first)
99                 return(work);
100         admin = admin_first;
101         len = write(sock, &admin->msg, sizeof(msg));
102         if (len == 0)
103         {
104                 printf("Socket closed\n");
105                 return(-1); // socket closed
106         }
107         if (len > 0)
108         {
109                 if (len != sizeof(msg))
110                 {
111                         fprintf(stderr, "Socket short write (%d)\n", len);
112                         return(-1); // socket error
113                 }
114                 /* free head */
115                 admin_first = admin->next;
116                 FREE(admin, 0);
117
118                 work = 1;
119         } else
120         {
121                 if (errno != EWOULDBLOCK)
122                 {
123                         fprintf(stderr, "Socket error %d\n", errno);
124                         return(-1);
125                 }
126         }
127
128         return(work);
129 }
130
131 /*
132  * main function
133  */
134 int main(int argc, char *argv[])
135 {
136         char *socket_name = SOCKET_NAME;
137         int conn;
138         struct sockaddr_un sock_address;
139         int ret;
140         unsigned long on = 1;
141         union parameter hello_param;
142
143         /* open socket */
144         if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
145         {
146                 fprintf(stderr, "Failed to create socket.\n");
147                 exit(EXIT_FAILURE);
148         }
149
150         /* set socket address and name */
151         memset(&sock_address, 0, sizeof(sock_address));
152         sock_address.sun_family = PF_UNIX;
153         UCPY(sock_address.sun_path, socket_name);
154
155         /* connect socket */
156         if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
157         {
158                 close(sock);
159                 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
160                 exit(EXIT_FAILURE);
161         }
162
163         /* set non-blocking io */
164         if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
165         {
166                 close(sock);
167                 fprintf(stderr, "Failed to set socket into non-blocking IO.\n");
168                 exit(EXIT_FAILURE);
169         }
170
171         /* enque hello message */
172         memset(&hello_param, 0, sizeof(hello_param));
173         admin_asterisk(MESSAGE_HELLO, &hello_param);
174
175         while(42)
176         {
177                 ret = handle_socket();
178                 if (ret < 0)
179                         break;
180                 if (!ret)
181                         usleep(30000);
182         }
183         
184         /* close socket */      
185         close(sock);
186         /* now we say good bye */
187         if (ret)
188         {
189                 printf("%s\n", ret);
190                 exit(EXIT_FAILURE);
191         }
192 }
193
194
195
196
197