LCOV - code coverage report
Current view: top level - src/platform/posix - socket.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 26 0
Test Date: 2026-05-06 13:17:06 Functions: 0.0 % 6 0

            Line data    Source code
       1              : /* SPDX-License-Identifier: GPL-3.0-or-later */
       2              : /* Copyright 2026 Peter Csaszar */
       3              : 
       4              : /**
       5              :  * @file socket.c
       6              :  * @brief POSIX TCP socket implementation.
       7              :  */
       8              : 
       9              : #include "../socket.h"
      10              : 
      11              : #include <sys/socket.h>
      12              : #include <netinet/in.h>
      13              : #include <netinet/tcp.h>
      14              : #include <arpa/inet.h>
      15              : #include <netdb.h>
      16              : #include <unistd.h>
      17              : #include <fcntl.h>
      18              : #include <stdio.h>
      19              : #include <string.h>
      20              : 
      21            0 : int sys_socket_create(void) {
      22            0 :     int fd = socket(AF_INET, SOCK_STREAM, 0);
      23            0 :     if (fd < 0) return -1;
      24              : 
      25            0 :     int flag = 1;
      26            0 :     setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
      27              : 
      28            0 :     return fd;
      29              : }
      30              : 
      31            0 : int sys_socket_connect(int fd, const char *host, int port) {
      32              :     struct addrinfo hints, *res;
      33            0 :     memset(&hints, 0, sizeof(hints));
      34            0 :     hints.ai_family = AF_INET;
      35            0 :     hints.ai_socktype = SOCK_STREAM;
      36              : 
      37              :     char port_str[16];
      38            0 :     snprintf(port_str, sizeof(port_str), "%d", port);
      39              : 
      40            0 :     int rc = getaddrinfo(host, port_str, &hints, &res);
      41            0 :     if (rc != 0) return -1;
      42              : 
      43            0 :     rc = connect(fd, res->ai_addr, res->ai_addrlen);
      44            0 :     freeaddrinfo(res);
      45            0 :     return rc;
      46              : }
      47              : 
      48            0 : ssize_t sys_socket_send(int fd, const void *buf, size_t len) {
      49            0 :     return send(fd, buf, len, 0);
      50              : }
      51              : 
      52            0 : ssize_t sys_socket_recv(int fd, void *buf, size_t len) {
      53            0 :     return recv(fd, buf, len, 0);
      54              : }
      55              : 
      56            0 : int sys_socket_close(int fd) {
      57            0 :     return close(fd);
      58              : }
      59              : 
      60            0 : int sys_socket_set_nonblocking(int fd) {
      61            0 :     int flags = fcntl(fd, F_GETFL, 0);
      62            0 :     if (flags < 0) return -1;
      63            0 :     return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
      64              : }
        

Generated by: LCOV version 2.0-1