1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright 2019 Intel Corporation 3bf215546Sopenharmony_ci * SPDX-License-Identifier: MIT 4bf215546Sopenharmony_ci */ 5bf215546Sopenharmony_ci 6bf215546Sopenharmony_ci#include <errno.h> 7bf215546Sopenharmony_ci 8bf215546Sopenharmony_ci#include "os_socket.h" 9bf215546Sopenharmony_ci 10bf215546Sopenharmony_ci#if defined(__linux__) 11bf215546Sopenharmony_ci 12bf215546Sopenharmony_ci#include <fcntl.h> 13bf215546Sopenharmony_ci#include <poll.h> 14bf215546Sopenharmony_ci#include <stddef.h> 15bf215546Sopenharmony_ci#include <string.h> 16bf215546Sopenharmony_ci#include <sys/socket.h> 17bf215546Sopenharmony_ci#include <sys/un.h> 18bf215546Sopenharmony_ci#include <unistd.h> 19bf215546Sopenharmony_ci 20bf215546Sopenharmony_ciint 21bf215546Sopenharmony_cios_socket_listen_abstract(const char *path, int count) 22bf215546Sopenharmony_ci{ 23bf215546Sopenharmony_ci int s = socket(AF_UNIX, SOCK_STREAM, 0); 24bf215546Sopenharmony_ci if (s < 0) 25bf215546Sopenharmony_ci return -1; 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci struct sockaddr_un addr; 28bf215546Sopenharmony_ci memset(&addr, 0, sizeof(addr)); 29bf215546Sopenharmony_ci addr.sun_family = AF_UNIX; 30bf215546Sopenharmony_ci strncpy(addr.sun_path + 1, path, sizeof(addr.sun_path) - 2); 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci /* Create an abstract socket */ 33bf215546Sopenharmony_ci int ret = bind(s, (struct sockaddr*)&addr, 34bf215546Sopenharmony_ci offsetof(struct sockaddr_un, sun_path) + 35bf215546Sopenharmony_ci strlen(path) + 1); 36bf215546Sopenharmony_ci if (ret < 0) { 37bf215546Sopenharmony_ci close(s); 38bf215546Sopenharmony_ci return -1; 39bf215546Sopenharmony_ci } 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci if (listen(s, count) < 0) { 42bf215546Sopenharmony_ci close(s); 43bf215546Sopenharmony_ci return -1; 44bf215546Sopenharmony_ci } 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci return s; 47bf215546Sopenharmony_ci} 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ciint 50bf215546Sopenharmony_cios_socket_accept(int s) 51bf215546Sopenharmony_ci{ 52bf215546Sopenharmony_ci return accept(s, NULL, NULL); 53bf215546Sopenharmony_ci} 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_cissize_t 56bf215546Sopenharmony_cios_socket_recv(int socket, void *buffer, size_t length, int flags) 57bf215546Sopenharmony_ci{ 58bf215546Sopenharmony_ci return recv(socket, buffer, length, flags); 59bf215546Sopenharmony_ci} 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_cissize_t 62bf215546Sopenharmony_cios_socket_send(int socket, const void *buffer, size_t length, int flags) 63bf215546Sopenharmony_ci{ 64bf215546Sopenharmony_ci return send(socket, buffer, length, flags); 65bf215546Sopenharmony_ci} 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_civoid 68bf215546Sopenharmony_cios_socket_block(int s, bool block) 69bf215546Sopenharmony_ci{ 70bf215546Sopenharmony_ci int old = fcntl(s, F_GETFL, 0); 71bf215546Sopenharmony_ci if (old == -1) 72bf215546Sopenharmony_ci return; 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci /* TODO obey block */ 75bf215546Sopenharmony_ci if (block) 76bf215546Sopenharmony_ci fcntl(s, F_SETFL, old & ~O_NONBLOCK); 77bf215546Sopenharmony_ci else 78bf215546Sopenharmony_ci fcntl(s, F_SETFL, old | O_NONBLOCK); 79bf215546Sopenharmony_ci} 80bf215546Sopenharmony_ci 81bf215546Sopenharmony_civoid 82bf215546Sopenharmony_cios_socket_close(int s) 83bf215546Sopenharmony_ci{ 84bf215546Sopenharmony_ci close(s); 85bf215546Sopenharmony_ci} 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci#else 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ciint 90bf215546Sopenharmony_cios_socket_listen_abstract(const char *path, int count) 91bf215546Sopenharmony_ci{ 92bf215546Sopenharmony_ci errno = -ENOSYS; 93bf215546Sopenharmony_ci return -1; 94bf215546Sopenharmony_ci} 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ciint 97bf215546Sopenharmony_cios_socket_accept(int s) 98bf215546Sopenharmony_ci{ 99bf215546Sopenharmony_ci errno = -ENOSYS; 100bf215546Sopenharmony_ci return -1; 101bf215546Sopenharmony_ci} 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_cissize_t 104bf215546Sopenharmony_cios_socket_recv(int socket, void *buffer, size_t length, int flags) 105bf215546Sopenharmony_ci{ 106bf215546Sopenharmony_ci errno = -ENOSYS; 107bf215546Sopenharmony_ci return -1; 108bf215546Sopenharmony_ci} 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_cissize_t 111bf215546Sopenharmony_cios_socket_send(int socket, const void *buffer, size_t length, int flags) 112bf215546Sopenharmony_ci{ 113bf215546Sopenharmony_ci errno = -ENOSYS; 114bf215546Sopenharmony_ci return -1; 115bf215546Sopenharmony_ci} 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_civoid 118bf215546Sopenharmony_cios_socket_block(int s, bool block) 119bf215546Sopenharmony_ci{ 120bf215546Sopenharmony_ci} 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_civoid 123bf215546Sopenharmony_cios_socket_close(int s) 124bf215546Sopenharmony_ci{ 125bf215546Sopenharmony_ci} 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci#endif 128