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"
39 int server_socket(void)
41 struct sockaddr_un sa
;
44 TST (strlen(RSTP_SERVER_SOCK_NAME
) < sizeof(sa
.sun_path
), -1);
46 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
48 ERROR("Couldn't open unix socket: %m");
52 set_socket_address(&sa
, RSTP_SERVER_SOCK_NAME
);
54 if (bind(s
, (struct sockaddr
*)&sa
, sizeof(sa
)) != 0) {
55 ERROR("Couldn't bind socket: %m");
64 int handle_message(int cmd
, void *inbuf
, int lin
, void *outbuf
, int *lout
)
67 SERVER_MESSAGE_CASE(enable_bridge_rstp
);
68 SERVER_MESSAGE_CASE(get_bridge_state
);
69 SERVER_MESSAGE_CASE(set_bridge_config
);
70 SERVER_MESSAGE_CASE(get_port_state
);
71 SERVER_MESSAGE_CASE(set_port_config
);
72 SERVER_MESSAGE_CASE(set_debug_level
);
75 ERROR("CTL: Unknown command %d", cmd
);
80 #define msg_buf_len 1024
81 unsigned char msg_inbuf
[1024];
82 unsigned char msg_outbuf
[1024];
84 void ctl_rcv_handler(uint32_t events
, struct epoll_event_handler
*p
)
86 struct ctl_msg_hdr mhdr
;
88 struct sockaddr_un sa
;
92 msg
.msg_name
= &sa
; msg
.msg_namelen
= sizeof(sa
);
93 msg
.msg_iov
= iov
; msg
.msg_iovlen
= 2;
94 msg
.msg_control
= NULL
; msg
.msg_controllen
= 0;
95 iov
[0].iov_base
= &mhdr
; iov
[0].iov_len
= sizeof(mhdr
);
96 iov
[1].iov_base
= msg_inbuf
; iov
[1].iov_len
= msg_buf_len
;
97 l
= recvmsg(p
->fd
, &msg
, MSG_NOSIGNAL
| MSG_DONTWAIT
);
99 if (msg
.msg_flags
!= 0 || l
< sizeof(mhdr
) ||
100 l
!= sizeof(mhdr
) + mhdr
.lin
||
101 mhdr
.lout
> msg_buf_len
|| mhdr
.cmd
< 0) {
102 ERROR("CTL: Unexpected message. Ignoring");
108 mhdr
.res
= handle_message(mhdr
.cmd
, msg_inbuf
, mhdr
.lin
,
109 msg_outbuf
, &mhdr
.lout
);
111 mhdr
.res
= handle_message(mhdr
.cmd
, msg_inbuf
, mhdr
.lin
,
116 iov
[1].iov_base
= msg_outbuf
; iov
[1].iov_len
= mhdr
.lout
;
117 l
= sendmsg(p
->fd
, &msg
, MSG_NOSIGNAL
);
119 ERROR("CTL: Couldn't send response: %m");
120 else if (l
!= sizeof(mhdr
) + mhdr
.lout
) {
121 ERROR("CTL: Couldn't send full response, sent %d bytes instead of %zd.",
122 l
, sizeof(mhdr
) + mhdr
.lout
);
126 struct epoll_event_handler ctl_handler
;
128 int ctl_socket_init(void)
130 int s
= server_socket();
135 ctl_handler
.handler
= ctl_rcv_handler
;
137 TST(add_epoll(&ctl_handler
) == 0, -1);
141 void ctl_socket_cleanup(void)
143 remove_epoll(&ctl_handler
);
144 close(ctl_handler
.fd
);