Comment: | Implement OFDDPSocket using SOCK_RAW on macOS
This required quite some reverse engineering, as it is all completely Receiving worked pretty much as expected: Do a readv() and get the raw But sending was tricky and required reading through kernel sources. The only missing part now is to return a proper address a socket was |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
41535b41da23040451a5147175942a49 |
User & Date: | js on 2022-11-01 22:18:34 |
Other Links: | manifest | tags |
2022-11-01
| ||
23:23 | OFDDPSocket: Return correct bound address on macOS check-in: e5075e42ab user: js tags: trunk | |
22:18 | Implement OFDDPSocket using SOCK_RAW on macOS check-in: 41535b41da user: js tags: trunk | |
2022-10-31
| ||
21:41 | Check for and use <netat/appletalk.h> check-in: 019ff77f50 user: js tags: trunk | |
Modified src/OFDDPSocket.m from [b54a9f63cf] to [0702a801d8].
︙ | ︙ | |||
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 | #import "OFDDPSocket.h" #import "OFSocket.h" #import "OFSocket+Private.h" #import "OFAlreadyConnectedException.h" #import "OFBindDDPSocketFailedException.h" @implementation OFDDPSocket @dynamic delegate; - (OFSocketAddress)bindToNetwork: (uint16_t)network node: (uint8_t)node port: (uint8_t)port { OFSocketAddress address; #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL_H) && defined(FD_CLOEXEC) int flags; #endif if (_socket != OFInvalidSocketHandle) @throw [OFAlreadyConnectedException exceptionWithSocket: self]; address = OFSocketAddressMakeAppleTalk(network, node, port); if ((_socket = socket(address.sockaddr.at.sat_family, SOCK_DGRAM | SOCK_CLOEXEC, 0)) == OFInvalidSocketHandle) @throw [OFBindDDPSocketFailedException exceptionWithNetwork: network node: node port: port socket: self errNo: OFSocketErrNo()]; | > > > > > > > > > > > > > > > | 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 | #import "OFDDPSocket.h" #import "OFSocket.h" #import "OFSocket+Private.h" #import "OFAlreadyConnectedException.h" #import "OFBindDDPSocketFailedException.h" #import "OFNotOpenException.h" #import "OFReadFailedException.h" #import "OFWriteFailedException.h" #ifdef OF_HAVE_NETAT_APPLETALK_H # include <netat/ddp.h> #endif @implementation OFDDPSocket @dynamic delegate; - (OFSocketAddress)bindToNetwork: (uint16_t)network node: (uint8_t)node port: (uint8_t)port { #ifdef OF_MACOS const int one = 1; #endif OFSocketAddress address; #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL_H) && defined(FD_CLOEXEC) int flags; #endif if (_socket != OFInvalidSocketHandle) @throw [OFAlreadyConnectedException exceptionWithSocket: self]; address = OFSocketAddressMakeAppleTalk(network, node, port); #ifdef OF_MACOS if ((_socket = socket(address.sockaddr.at.sat_family, SOCK_RAW | SOCK_CLOEXEC, 0)) == OFInvalidSocketHandle) #else if ((_socket = socket(address.sockaddr.at.sat_family, SOCK_DGRAM | SOCK_CLOEXEC, 0)) == OFInvalidSocketHandle) #endif @throw [OFBindDDPSocketFailedException exceptionWithNetwork: network node: node port: port socket: self errNo: OFSocketErrNo()]; |
︙ | ︙ | |||
102 103 104 105 106 107 108 109 110 111 112 | @throw [OFBindDDPSocketFailedException exceptionWithNetwork: network node: node port: port socket: self errNo: EAFNOSUPPORT]; } return address; } @end | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | @throw [OFBindDDPSocketFailedException exceptionWithNetwork: network node: node port: port socket: self errNo: EAFNOSUPPORT]; } #ifdef OF_MACOS if (setsockopt(_socket, ATPROTO_NONE, DDP_HDRINCL, &one, sizeof(one)) != 0) @throw [OFBindDDPSocketFailedException exceptionWithNetwork: network node: node port: port socket: self errNo: OFSocketErrNo()]; #endif return address; } #ifdef OF_MACOS - (size_t)receiveIntoBuffer: (void *)buffer length: (size_t)length sender: (OFSocketAddress *)sender { ssize_t ret; at_ddp_t header; struct iovec iov[2] = { { &header, offsetof(at_ddp_t, type) }, { buffer, length } }; if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; if ((ret = readv(_socket, iov, 2)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length errNo: OFSocketErrNo()]; *sender = OFSocketAddressMakeAppleTalk( header.src_net[0] << 8 | header.src_net[1], header.src_node, header.src_socket); ret -= offsetof(at_ddp_t, type); if (ret < 0) return 0; return ret; } - (void)sendBuffer: (const void *)buffer length: (size_t)length receiver: (const OFSocketAddress *)receiver { at_ddp_t header = { 0 }; struct iovec iov[2] = { { &header, offsetof(at_ddp_t, type) }, { (void *)buffer, length } }; struct msghdr msg = { .msg_name = (struct sockaddr *)&receiver->sockaddr, .msg_namelen = receiver->length, .msg_iov = iov, .msg_iovlen = 2 }; size_t packetLength = length + offsetof(at_ddp_t, type); uint16_t net; ssize_t bytesWritten; if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; if (packetLength > DDP_DATAGRAM_SIZE) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length bytesWritten: 0 errNo: EMSGSIZE]; net = OFSocketAddressAppleTalkNetwork(receiver); header.length_H = (packetLength >> 8) & 3; header.length_L = packetLength & 0xFF; header.dst_net[0] = net >> 8; header.dst_net[1] = net & 0xFF; header.dst_node = OFSocketAddressAppleTalkNode(receiver); header.dst_socket = OFSocketAddressAppleTalkPort(receiver); if ((bytesWritten = sendmsg(_socket, &msg, 0)) < 0) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length bytesWritten: 0 errNo: OFSocketErrNo()]; if ((size_t)bytesWritten != packetLength) { bytesWritten -= offsetof(at_ddp_t, type); if (bytesWritten < 0) bytesWritten = 0; @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length bytesWritten: bytesWritten errNo: 0]; } } #endif @end |