huangcm
2025-07-03 5fc6eec0444a62f7a596240b200dd837059dba70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright 2017, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "socket.h"
 
#include "message.h"
#include "utils.h"
 
#include <errno.h>
#include <linux/if_packet.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
 
// Combine the checksum of |buffer| with |size| bytes with |checksum|. This is
// used for checksum calculations for IP and UDP.
static uint32_t addChecksum(const uint8_t* buffer,
                            size_t size,
                            uint32_t checksum) {
    const uint16_t* data = reinterpret_cast<const uint16_t*>(buffer);
    while (size > 1) {
        checksum += *data++;
        size -= 2;
    }
    if (size > 0) {
        // Odd size, add the last byte
        checksum += *reinterpret_cast<const uint8_t*>(data);
    }
    // msw is the most significant word, the upper 16 bits of the checksum
    for (uint32_t msw = checksum >> 16; msw != 0; msw = checksum >> 16) {
        checksum = (checksum & 0xFFFF) + msw;
    }
    return checksum;
}
 
// Convenienct template function for checksum calculation
template<typename T>
static uint32_t addChecksum(const T& data, uint32_t checksum) {
    return addChecksum(reinterpret_cast<const uint8_t*>(&data), sizeof(T), checksum);
}
 
// Finalize the IP or UDP |checksum| by inverting and truncating it.
static uint32_t finishChecksum(uint32_t checksum) {
    return ~checksum & 0xFFFF;
}
 
Socket::Socket() : mSocketFd(-1) {
}
 
Socket::~Socket() {
    if (mSocketFd != -1) {
        ::close(mSocketFd);
        mSocketFd = -1;
    }
}
 
 
Result Socket::open(int domain, int type, int protocol) {
    if (mSocketFd != -1) {
        return Result::error("Socket already open");
    }
    mSocketFd = ::socket(domain, type, protocol);
    if (mSocketFd == -1) {
        return Result::error("Failed to open socket: %s", strerror(errno));
    }
    return Result::success();
}
 
Result Socket::bind(const void* sockaddr, size_t sockaddrLength) {
    if (mSocketFd == -1) {
        return Result::error("Socket not open");
    }
 
    int status = ::bind(mSocketFd,
                        reinterpret_cast<const struct sockaddr*>(sockaddr),
                        sockaddrLength);
    if (status != 0) {
        return Result::error("Unable to bind raw socket: %s", strerror(errno));
    }
 
    return Result::success();
}
 
Result Socket::bindIp(in_addr_t address, uint16_t port) {
    struct sockaddr_in sockaddr;
    memset(&sockaddr, 0, sizeof(sockaddr));
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(port);
    sockaddr.sin_addr.s_addr = address;
 
    return bind(&sockaddr, sizeof(sockaddr));
}
 
Result Socket::bindRaw(unsigned int interfaceIndex) {
    struct sockaddr_ll sockaddr;
    memset(&sockaddr, 0, sizeof(sockaddr));
    sockaddr.sll_family = AF_PACKET;
    sockaddr.sll_protocol = htons(ETH_P_IP);
    sockaddr.sll_ifindex = interfaceIndex;
 
    return bind(&sockaddr, sizeof(sockaddr));
}
 
Result Socket::sendOnInterface(unsigned int interfaceIndex,
                               in_addr_t destinationAddress,
                               uint16_t destinationPort,
                               const Message& message) {
    if (mSocketFd == -1) {
        return Result::error("Socket not open");
    }
 
    char controlData[CMSG_SPACE(sizeof(struct in_pktinfo))] = { 0 };
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(destinationPort);
    addr.sin_addr.s_addr = destinationAddress;
 
    struct msghdr header;
    memset(&header, 0, sizeof(header));
    struct iovec iov;
    // The struct member is non-const since it's used for receiving but it's
    // safe to cast away const for sending.
    iov.iov_base = const_cast<uint8_t*>(message.data());
    iov.iov_len = message.size();
    header.msg_name = &addr;
    header.msg_namelen = sizeof(addr);
    header.msg_iov = &iov;
    header.msg_iovlen = 1;
    header.msg_control = &controlData;
    header.msg_controllen = sizeof(controlData);
 
    struct cmsghdr* controlHeader = CMSG_FIRSTHDR(&header);
    controlHeader->cmsg_level = IPPROTO_IP;
    controlHeader->cmsg_type = IP_PKTINFO;
    controlHeader->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
    auto packetInfo =
        reinterpret_cast<struct in_pktinfo*>(CMSG_DATA(controlHeader));
    memset(packetInfo, 0, sizeof(*packetInfo));
    packetInfo->ipi_ifindex = interfaceIndex;
 
    ssize_t status = ::sendmsg(mSocketFd, &header, 0);
    if (status <= 0) {
        return Result::error("Failed to send packet: %s", strerror(errno));
    }
    return Result::success();
}
 
Result Socket::sendRawUdp(in_addr_t source,
                          uint16_t sourcePort,
                          in_addr_t destination,
                          uint16_t destinationPort,
                          unsigned int interfaceIndex,
                          const Message& message) {
    struct iphdr ip;
    struct udphdr udp;
 
    ip.version = IPVERSION;
    ip.ihl = sizeof(ip) >> 2;
    ip.tos = 0;
    ip.tot_len = htons(sizeof(ip) + sizeof(udp) + message.size());
    ip.id = 0;
    ip.frag_off = 0;
    ip.ttl = IPDEFTTL;
    ip.protocol = IPPROTO_UDP;
    ip.check = 0;
    ip.saddr = source;
    ip.daddr = destination;
    ip.check = finishChecksum(addChecksum(ip, 0));
 
    udp.source = htons(sourcePort);
    udp.dest = htons(destinationPort);
    udp.len = htons(sizeof(udp) + message.size());
    udp.check = 0;
 
    uint32_t udpChecksum = 0;
    udpChecksum = addChecksum(ip.saddr, udpChecksum);
    udpChecksum = addChecksum(ip.daddr, udpChecksum);
    udpChecksum = addChecksum(htons(IPPROTO_UDP), udpChecksum);
    udpChecksum = addChecksum(udp.len, udpChecksum);
    udpChecksum = addChecksum(udp, udpChecksum);
    udpChecksum = addChecksum(message.data(), message.size(), udpChecksum);
    udp.check = finishChecksum(udpChecksum);
 
    struct iovec iov[3];
 
    iov[0].iov_base = static_cast<void*>(&ip);
    iov[0].iov_len = sizeof(ip);
    iov[1].iov_base = static_cast<void*>(&udp);
    iov[1].iov_len = sizeof(udp);
    // sendmsg requires these to be non-const but for sending won't modify them
    iov[2].iov_base = static_cast<void*>(const_cast<uint8_t*>(message.data()));
    iov[2].iov_len = message.size();
 
    struct sockaddr_ll dest;
    memset(&dest, 0, sizeof(dest));
    dest.sll_family = AF_PACKET;
    dest.sll_protocol = htons(ETH_P_IP);
    dest.sll_ifindex = interfaceIndex;
    dest.sll_halen = ETH_ALEN;
    memset(dest.sll_addr, 0xFF, ETH_ALEN);
 
    struct msghdr header;
    memset(&header, 0, sizeof(header));
    header.msg_name = &dest;
    header.msg_namelen = sizeof(dest);
    header.msg_iov = iov;
    header.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
 
    ssize_t res = ::sendmsg(mSocketFd, &header, 0);
    if (res == -1) {
        return Result::error("Failed to send message: %s", strerror(errno));
    }
    return Result::success();
}
 
Result Socket::receiveFromInterface(Message* message,
                                    unsigned int* interfaceIndex) {
    char controlData[CMSG_SPACE(sizeof(struct in_pktinfo))];
    struct msghdr header;
    memset(&header, 0, sizeof(header));
    struct iovec iov;
    iov.iov_base = message->data();
    iov.iov_len = message->capacity();
    header.msg_iov = &iov;
    header.msg_iovlen = 1;
    header.msg_control = &controlData;
    header.msg_controllen = sizeof(controlData);
 
    ssize_t bytesRead = ::recvmsg(mSocketFd, &header, 0);
    if (bytesRead < 0) {
        return Result::error("Error receiving on socket: %s", strerror(errno));
    }
    message->setSize(static_cast<size_t>(bytesRead));
    if (header.msg_controllen >= sizeof(struct cmsghdr)) {
        for (struct cmsghdr* ctrl = CMSG_FIRSTHDR(&header);
             ctrl;
             ctrl = CMSG_NXTHDR(&header, ctrl)) {
            if (ctrl->cmsg_level == SOL_IP &&
                ctrl->cmsg_type == IP_PKTINFO) {
                auto packetInfo =
                    reinterpret_cast<struct in_pktinfo*>(CMSG_DATA(ctrl));
                *interfaceIndex = packetInfo->ipi_ifindex;
            }
        }
    }
    return Result::success();
}
 
Result Socket::receiveRawUdp(uint16_t expectedPort,
                             Message* message,
                             bool* isValid) {
    struct iphdr ip;
    struct udphdr udp;
 
    struct iovec iov[3];
    iov[0].iov_base = &ip;
    iov[0].iov_len = sizeof(ip);
    iov[1].iov_base = &udp;
    iov[1].iov_len = sizeof(udp);
    iov[2].iov_base = message->data();
    iov[2].iov_len = message->capacity();
 
    ssize_t bytesRead = ::readv(mSocketFd, iov, 3);
    if (bytesRead < 0) {
        return Result::error("Unable to read from socket: %s", strerror(errno));
    }
    if (static_cast<size_t>(bytesRead) < sizeof(ip) + sizeof(udp)) {
        // Not enough bytes to even cover IP and UDP headers
        *isValid = false;
        return Result::success();
    }
    *isValid = ip.version == IPVERSION &&
               ip.ihl == (sizeof(ip) >> 2) &&
               ip.protocol == IPPROTO_UDP &&
               udp.dest == htons(expectedPort);
 
    message->setSize(bytesRead - sizeof(ip) - sizeof(udp));
    return Result::success();
}
 
Result Socket::enableOption(int level, int optionName) {
    if (mSocketFd == -1) {
        return Result::error("Socket not open");
    }
 
    int enabled = 1;
    int status = ::setsockopt(mSocketFd,
                              level,
                              optionName,
                              &enabled,
                              sizeof(enabled));
    if (status == -1) {
        return Result::error("Failed to set socket option: %s",
                             strerror(errno));
    }
    return Result::success();
}