| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <console.h> |
|---|
| 20 | #include <error.h> |
|---|
| 21 | #include <net/network.h> |
|---|
| 22 | #include <net/socket.h> |
|---|
| 23 | #include <slab.h> |
|---|
| 24 | #include <sys.h> |
|---|
| 25 | #include <task.h> |
|---|
| 26 | #include <typedefs.h> |
|---|
| 27 | #include <wait.h> |
|---|
| 28 | #include <print.h> |
|---|
| 29 | #include <module.h> |
|---|
| 30 | |
|---|
| 31 | struct LocalSocketAddr |
|---|
| 32 | { |
|---|
| 33 | unsigned int family; |
|---|
| 34 | char name[SOCKET_DATA_LENGTH]; |
|---|
| 35 | }; |
|---|
| 36 | |
|---|
| 37 | struct LocalSockInfo |
|---|
| 38 | { |
|---|
| 39 | |
|---|
| 40 | struct Socket* peer; |
|---|
| 41 | struct ListHead waitingSocks; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | WaitQueue waitQueue; |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | LIST_HEAD(localListenSockets); |
|---|
| 48 | struct Cache* localInfoCache=NULL; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | static void LocalSetPeer(struct Socket* first, struct Socket* second) |
|---|
| 67 | { |
|---|
| 68 | struct LocalSockInfo* info; |
|---|
| 69 | |
|---|
| 70 | if (!first) |
|---|
| 71 | return; |
|---|
| 72 | |
|---|
| 73 | info=(struct LocalSockInfo*)(first->protoInfo); |
|---|
| 74 | |
|---|
| 75 | if (!info) |
|---|
| 76 | return; |
|---|
| 77 | |
|---|
| 78 | info->peer=second; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | int LocalBind(struct Socket* socket, struct SocketAddr* addr, int length) |
|---|
| 97 | { |
|---|
| 98 | struct LocalSockInfo* info=(struct LocalSockInfo*)(socket->protoInfo); |
|---|
| 99 | |
|---|
| 100 | memcpy(&socket->addr, addr, length); |
|---|
| 101 | INIT_LIST_HEAD(&info->waitingSocks); |
|---|
| 102 | |
|---|
| 103 | return 0; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | int LocalConnect(struct Socket* socket, struct SocketAddr* addr, int length) |
|---|
| 107 | { |
|---|
| 108 | struct LocalSocketAddr* socketAddr=(struct LocalSocketAddr*)addr; |
|---|
| 109 | struct LocalSocketAddr* serverSockAddr; |
|---|
| 110 | struct LocalSockInfo* serverInfo, *clientInfo; |
|---|
| 111 | struct Socket* curr, *curr2, *serverSock=NULL; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | if (socketAddr->family != PF_LOCAL) |
|---|
| 115 | return -EINVAL; |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | ListForEachEntrySafe(curr, curr2, &localListenSockets, next) |
|---|
| 119 | { |
|---|
| 120 | serverSockAddr=(struct LocalSocketAddr*)&curr->addr; |
|---|
| 121 | if (!strncmp(socketAddr->name, serverSockAddr->name, SOCKET_DATA_LENGTH)) |
|---|
| 122 | { |
|---|
| 123 | serverSock=curr; |
|---|
| 124 | break; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | if (!serverSock) |
|---|
| 129 | return -ENOENT; |
|---|
| 130 | |
|---|
| 131 | clientInfo = (struct LocalSockInfo*)(socket->protoInfo); |
|---|
| 132 | |
|---|
| 133 | serverInfo=(struct LocalSockInfo*)(serverSock->protoInfo); |
|---|
| 134 | socket->state=SOCKET_STATE_WAITING; |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | ListAdd(&socket->next, &serverInfo->waitingSocks); |
|---|
| 141 | |
|---|
| 142 | WakeUp(&serverInfo->waitQueue); |
|---|
| 143 | |
|---|
| 144 | WAIT_ON(&clientInfo->waitQueue, socket->state == SOCKET_STATE_CONNECTED); |
|---|
| 145 | |
|---|
| 146 | return 0; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | int LocalCreate(struct Socket* socket) |
|---|
| 150 | { |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | struct LocalSockInfo* info; |
|---|
| 155 | |
|---|
| 156 | if (socket->type & SOCKET_TYPE_STREAM) |
|---|
| 157 | return -ESOCKTYPE; |
|---|
| 158 | |
|---|
| 159 | info=(struct LocalSockInfo*)MemCacheAlloc(localInfoCache); |
|---|
| 160 | |
|---|
| 161 | if (!info) |
|---|
| 162 | return -ENOMEM; |
|---|
| 163 | |
|---|
| 164 | info->peer=NULL; |
|---|
| 165 | INIT_WAITQUEUE_HEAD(&info->waitQueue); |
|---|
| 166 | |
|---|
| 167 | socket->protoInfo=info; |
|---|
| 168 | |
|---|
| 169 | return 0; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | int LocalClose(struct Socket* socket) |
|---|
| 173 | { |
|---|
| 174 | struct LocalSockInfo* info; |
|---|
| 175 | |
|---|
| 176 | if (!socket) |
|---|
| 177 | return 0; |
|---|
| 178 | |
|---|
| 179 | info=(struct LocalSockInfo*)(socket->protoInfo); |
|---|
| 180 | |
|---|
| 181 | if (!info) |
|---|
| 182 | return 0; |
|---|
| 183 | |
|---|
| 184 | if (!(socket->type & SOCKET_TYPE_SERVER)) |
|---|
| 185 | |
|---|
| 186 | LocalSetPeer(info->peer, NULL); |
|---|
| 187 | |
|---|
| 188 | return 0; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | int LocalListen(struct Socket* socket, int backlog) |
|---|
| 192 | { |
|---|
| 193 | |
|---|
| 194 | ListAdd(&socket->next, &localListenSockets); |
|---|
| 195 | socket->state=SOCKET_STATE_ACCEPTING; |
|---|
| 196 | return 0; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | int LocalAccept(struct Socket* socket, struct SocketAddr* addr, int* length) |
|---|
| 200 | { |
|---|
| 201 | struct Socket* client, *child; |
|---|
| 202 | struct LocalSockInfo* info=(struct LocalSockInfo*)(socket->protoInfo); |
|---|
| 203 | struct LocalSockInfo* clientInfo; |
|---|
| 204 | int ret; |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | WAIT_ON(&info->waitQueue, !ListEmpty(&info->waitingSocks)); |
|---|
| 208 | |
|---|
| 209 | client=ListEntry(info->waitingSocks.next, struct Socket, next); |
|---|
| 210 | |
|---|
| 211 | ListRemove(&client->next); |
|---|
| 212 | |
|---|
| 213 | ret=SocketDoAccept(socket, client, &child); |
|---|
| 214 | |
|---|
| 215 | if (ret < 0) |
|---|
| 216 | return ret; |
|---|
| 217 | |
|---|
| 218 | if (addr) |
|---|
| 219 | { |
|---|
| 220 | |
|---|
| 221 | addr->family=PF_LOCAL; |
|---|
| 222 | ZeroMemory(addr->data, SOCKET_DATA_LENGTH); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | INIT_WAITQUEUE_HEAD(&info->waitQueue); |
|---|
| 226 | |
|---|
| 227 | if (length) |
|---|
| 228 | *length=sizeof(unsigned int); |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | LocalSetPeer(child, client); |
|---|
| 232 | LocalSetPeer(client, child); |
|---|
| 233 | |
|---|
| 234 | clientInfo = (struct LocalSockInfo*)(client->protoInfo); |
|---|
| 235 | WakeUp(&clientInfo->waitQueue); |
|---|
| 236 | |
|---|
| 237 | return ret; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | int LocalSend(struct Socket* socket, const void* buffer, int length, int flags) |
|---|
| 241 | { |
|---|
| 242 | struct LocalSockInfo* info=(struct LocalSockInfo*)(socket->protoInfo); |
|---|
| 243 | struct Socket* peer; |
|---|
| 244 | struct LocalSockInfo* peerInfo; |
|---|
| 245 | struct SocketBuffer* sockBuff; |
|---|
| 246 | DWORD iFlags; |
|---|
| 247 | |
|---|
| 248 | if (socket->state != SOCKET_STATE_CONNECTED) |
|---|
| 249 | return -EINVAL; |
|---|
| 250 | |
|---|
| 251 | peer=info->peer; |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | if (!peer) |
|---|
| 255 | return -ECONNRESET; |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | sockBuff=SocketAllocateBuffer(buffer, length); |
|---|
| 259 | if (!sockBuff) |
|---|
| 260 | return -ENOMEM; |
|---|
| 261 | |
|---|
| 262 | peerInfo=(struct LocalSockInfo*)(peer->protoInfo); |
|---|
| 263 | |
|---|
| 264 | IrqSaveFlags(iFlags); |
|---|
| 265 | |
|---|
| 266 | ListAddTail(&sockBuff->next, &peer->recvPackets); |
|---|
| 267 | IrqRestoreFlags(iFlags); |
|---|
| 268 | |
|---|
| 269 | WakeUp(&peerInfo->waitQueue); |
|---|
| 270 | |
|---|
| 271 | return length; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | int LocalReceive(struct Socket* socket, void* buffer, int length, int flags) |
|---|
| 275 | { |
|---|
| 276 | struct SocketBuffer* sockBuff; |
|---|
| 277 | struct LocalSockInfo* info, *peerInfo; |
|---|
| 278 | char* pos, *orig; |
|---|
| 279 | int toRead=0; |
|---|
| 280 | |
|---|
| 281 | pos=orig=(char*)buffer; |
|---|
| 282 | |
|---|
| 283 | info=(struct LocalSockInfo*)(socket->protoInfo); |
|---|
| 284 | peerInfo=(struct LocalSockInfo*)(info->peer->protoInfo); |
|---|
| 285 | |
|---|
| 286 | if (ListEmpty(&socket->recvPackets)) |
|---|
| 287 | WAIT_ON(&info->waitQueue, !ListEmpty(&socket->recvPackets)); |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | while ((pos-orig) < length) |
|---|
| 291 | { |
|---|
| 292 | if (ListEmpty(&socket->recvPackets)) |
|---|
| 293 | break; |
|---|
| 294 | |
|---|
| 295 | PreemptDisable(); |
|---|
| 296 | sockBuff=ListEntry(socket->recvPackets.next,struct SocketBuffer, next); |
|---|
| 297 | |
|---|
| 298 | toRead=MIN(sockBuff->length-sockBuff->read, length); |
|---|
| 299 | memcpy(pos, sockBuff->data, toRead); |
|---|
| 300 | |
|---|
| 301 | sockBuff->read+=toRead; |
|---|
| 302 | pos+=toRead; |
|---|
| 303 | |
|---|
| 304 | if (sockBuff->read == sockBuff->length) |
|---|
| 305 | SocketDestroyBuffer(sockBuff); |
|---|
| 306 | |
|---|
| 307 | PreemptEnable(); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | return pos-orig; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | int LocalPoll(struct Socket* socket, struct PollItem* item, struct PollQueue* pollQueue) |
|---|
| 314 | { |
|---|
| 315 | struct LocalSockInfo* info=(struct LocalSockInfo*)(socket->protoInfo); |
|---|
| 316 | |
|---|
| 317 | PollAddWait(pollQueue, &info->waitQueue); |
|---|
| 318 | |
|---|
| 319 | if ((socket->type & SOCKET_TYPE_SERVER) && !ListEmpty(&info->waitingSocks)) |
|---|
| 320 | item->revents |= POLL_IN; |
|---|
| 321 | |
|---|
| 322 | if ((!(socket->type & SOCKET_TYPE_SERVER) && !ListEmpty(&socket->recvPackets))) |
|---|
| 323 | item->revents |= POLL_IN; |
|---|
| 324 | |
|---|
| 325 | item->revents |= POLL_OUT; |
|---|
| 326 | |
|---|
| 327 | if (!(socket->type & SOCKET_TYPE_SERVER) && !info->peer) |
|---|
| 328 | item->revents |= POLL_ERR; |
|---|
| 329 | |
|---|
| 330 | return 0; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | struct SocketOps localOps= |
|---|
| 334 | { |
|---|
| 335 | .bind = LocalBind, |
|---|
| 336 | .connect = LocalConnect, |
|---|
| 337 | .create = LocalCreate, |
|---|
| 338 | .close = LocalClose, |
|---|
| 339 | .listen = LocalListen, |
|---|
| 340 | .accept = LocalAccept, |
|---|
| 341 | .send = LocalSend, |
|---|
| 342 | .receive = LocalReceive, |
|---|
| 343 | .poll = LocalPoll, |
|---|
| 344 | }; |
|---|
| 345 | |
|---|
| 346 | int LocalInit() |
|---|
| 347 | { |
|---|
| 348 | localInfoCache=MemCacheCreate("LocalSockets", sizeof(struct LocalSockInfo), NULL, NULL, 0); |
|---|
| 349 | if (!localInfoCache) |
|---|
| 350 | return -ENOMEM; |
|---|
| 351 | |
|---|
| 352 | KePrint("LOCAL: Local network sockets set up.\n"); |
|---|
| 353 | |
|---|
| 354 | SocketRegisterFamily(PF_LOCAL, &localOps); |
|---|
| 355 | return 0; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | ModuleInit(LocalInit); |
|---|