1 /*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc., 59
16 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 The full GNU General Public License is included in this distribution in the
21 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
23 ******************************************************************************/
25 #include "ctl_socket.h"
26 #include "ctl_socket_server.h"
28 #include <sys/types.h>
29 #include <sys/socket.h>
35 #include "epoll_loop.h"
38 int server_socket(void)
40 struct sockaddr_un sa
;
43 TST(strlen(RSTP_SERVER_SOCK_NAME
) < sizeof(sa
.sun_path
), -1);
45 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
47 ERROR("Couldn't open unix socket: %m");
51 set_socket_address(&sa
, RSTP_SERVER_SOCK_NAME
);
53 if (bind(s
, (struct sockaddr
*)&sa
, sizeof(sa
)) != 0) {
54 ERROR("Couldn't bind socket: %m");
62 int handle_message(int cmd
, void *inbuf
, int lin
, void *outbuf
, int *lout
)
65 SERVER_MESSAGE_CASE(enable_bridge_rstp
);
66 SERVER_MESSAGE_CASE(get_bridge_state
);
67 SERVER_MESSAGE_CASE(set_bridge_config
);
68 SERVER_MESSAGE_CASE(get_port_state
);
69 SERVER_MESSAGE_CASE(set_port_config
);
70 SERVER_MESSAGE_CASE(set_debug_level
);
73 ERROR("CTL: Unknown command %d", cmd
);
78 #define msg_buf_len 1024
79 unsigned char msg_inbuf
[1024];
80 unsigned char msg_outbuf
[1024];
82 void ctl_rcv_handler(uint32_t events
, struct epoll_event_handler
*p
)
84 struct ctl_msg_hdr mhdr
;
86 struct sockaddr_un sa
;
91 msg
.msg_namelen
= sizeof(sa
);
94 msg
.msg_control
= NULL
;
95 msg
.msg_controllen
= 0;
96 iov
[0].iov_base
= &mhdr
;
97 iov
[0].iov_len
= sizeof(mhdr
);
98 iov
[1].iov_base
= msg_inbuf
;
99 iov
[1].iov_len
= msg_buf_len
;
100 l
= recvmsg(p
->fd
, &msg
, MSG_NOSIGNAL
| MSG_DONTWAIT
);
102 if (msg
.msg_flags
!= 0 || l
< sizeof(mhdr
) ||
103 l
!= sizeof(mhdr
) + mhdr
.lin
||
104 mhdr
.lout
> msg_buf_len
|| mhdr
.cmd
< 0) {
105 ERROR("CTL: Unexpected message. Ignoring");
110 mhdr
.res
= handle_message(mhdr
.cmd
, msg_inbuf
, mhdr
.lin
,
111 msg_outbuf
, &mhdr
.lout
);
113 mhdr
.res
= handle_message(mhdr
.cmd
, msg_inbuf
, mhdr
.lin
,
118 iov
[1].iov_base
= msg_outbuf
;
119 iov
[1].iov_len
= mhdr
.lout
;
120 l
= sendmsg(p
->fd
, &msg
, MSG_NOSIGNAL
);
122 ERROR("CTL: Couldn't send response: %m");
123 else if (l
!= sizeof(mhdr
) + mhdr
.lout
) {
125 ("CTL: Couldn't send full response, sent %d bytes instead of %zd.",
126 l
, sizeof(mhdr
) + mhdr
.lout
);
130 struct epoll_event_handler ctl_handler
;
132 int ctl_socket_init(void)
134 int s
= server_socket();
139 ctl_handler
.handler
= ctl_rcv_handler
;
141 TST(add_epoll(&ctl_handler
) == 0, -1);
145 void ctl_socket_cleanup(void)
147 remove_epoll(&ctl_handler
);
148 close(ctl_handler
.fd
);