xref: /f-stack/tools/ipfw/compat.c (revision 9bd490e8)
1 /*
2  * Copyright (C) 2017 THL A29 Limited, a Tencent company.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *   list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *
26  */
27 
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <string.h>
32 
33 #include "ff_ipc.h"
34 
35 static int
36 ipfw_ctl(int cmd, int level, int optname, void *optval, socklen_t *optlen)
37 {
38     struct ff_msg *msg, *retmsg = NULL;
39     int len;
40 
41     switch (cmd) {
42 	case FF_IPFW_GET:
43             if (optval == NULL || optlen == NULL) {
44                 return EINVAL;
45             }
46             break;
47         case FF_IPFW_SET:
48             break;
49         default:
50             return EINVAL;
51     }
52 
53     msg = ff_ipc_msg_alloc();
54     if (msg == NULL) {
55         errno = ENOMEM;
56         return -1;
57     }
58 
59     len = sizeof(struct ff_ipfw_args) + *optlen + sizeof(socklen_t);
60     if (len > msg->buf_len) {
61         errno = EINVAL;
62         ff_ipc_msg_free(msg);
63         return -1;
64     }
65 
66     msg->msg_type = FF_IPFW_CTL;
67     msg->ipfw.cmd = cmd;
68     msg->ipfw.level = level;
69     msg->ipfw.optname = optname;
70     msg->ipfw.optval = (void *)msg->buf_addr;
71     msg->ipfw.optlen = (socklen_t *)(msg->buf_addr + (*optlen));
72 
73     memcpy(msg->ipfw.optval, optval, *optlen);
74     memcpy(msg->ipfw.optlen, optlen, sizeof(socklen_t));
75 
76     int ret = ff_ipc_send(msg);
77     if (ret < 0) {
78         errno = EPIPE;
79         ff_ipc_msg_free(msg);
80         return -1;
81     }
82 
83     do {
84         if (retmsg != NULL) {
85             ff_ipc_msg_free(retmsg);
86         }
87         ret = ff_ipc_recv(&retmsg);
88         if (ret < 0) {
89             errno = EPIPE;
90             ff_ipc_msg_free(msg);
91             return -1;
92         }
93     } while (msg != retmsg);
94 
95     if (retmsg->result != 0) {
96         ret = -1;
97         errno = retmsg->result;
98     } else {
99         ret = 0;
100 
101         if (cmd == FF_IPFW_GET) {
102             memcpy(optval, retmsg->ipfw.optval, *(retmsg->ipfw.optlen));
103             memcpy(optlen, retmsg->ipfw.optlen, sizeof(socklen_t));
104         }
105     }
106 
107     ff_ipc_msg_free(msg);
108 
109     return ret;
110 }
111 
112 int
113 ff_socket(int domain, int type, int protocol)
114 {
115     return 0;
116 }
117 
118 int ff_getsockopt(int sockfd, int level, int optname,
119     void *optval, socklen_t *optlen)
120 {
121     return ipfw_ctl(FF_IPFW_GET, level, optname, optval, optlen);
122 }
123 
124 int ff_setsockopt(int sockfd, int level, int optname,
125     const void *optval, socklen_t optlen)
126 {
127     return ipfw_ctl(FF_IPFW_SET, level, optname, (void *)optval, &optlen);
128 }
129 
130