LCOV - code coverage report
Current view: top level - gio - giptosmessage.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 97.2 % 36 35
Test Date: 2026-05-12 05:15:29 Functions: 100.0 % 13 13
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* GIO - GLib Input, Output and Streaming Library
       2                 :             :  *
       3                 :             :  * Copyright © 2025 Collabora Ltd.
       4                 :             :  *
       5                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :             :  *
       7                 :             :  * This library is free software; you can redistribute it and/or
       8                 :             :  * modify it under the terms of the GNU Lesser General Public
       9                 :             :  * License as published by the Free Software Foundation; either
      10                 :             :  * version 2.1 of the License, or (at your option) any later version.
      11                 :             :  *
      12                 :             :  * See the included COPYING file for more information.
      13                 :             :  *
      14                 :             :  * Authors: Jakub Adam <jakub.adam@collabora.com>
      15                 :             :  */
      16                 :             : 
      17                 :             : /**
      18                 :             :  * GIPTosMessage:
      19                 :             :  *
      20                 :             :  * Contains the type of service (ToS) byte of an IPv4 header.
      21                 :             :  * 
      22                 :             :  * This consists of the DSCP field as per
      23                 :             :  * [RFC 2474](https://www.rfc-editor.org/rfc/rfc2474#section-3),
      24                 :             :  * and the ECN field as per
      25                 :             :  * [RFC 3168](https://www.rfc-editor.org/rfc/rfc3168#section-5).
      26                 :             :  * 
      27                 :             :  * It may be received using [method@Gio.Socket.receive_message] over UDP sockets
      28                 :             :  * (i.e. sockets in the `G_SOCKET_FAMILY_IPV4` family with
      29                 :             :  * `G_SOCKET_TYPE_DATAGRAM` type). The message is not meant for sending. To set
      30                 :             :  * ToS field to be used in datagrams sent on a [class@Gio.Socket] use:
      31                 :             :  * ```c
      32                 :             :  * g_socket_set_option (socket, IPPROTO_IP, IP_TOS, <ToS value>, &error);
      33                 :             :  * ```
      34                 :             :  *
      35                 :             :  * Since: 2.88
      36                 :             :  */
      37                 :             : 
      38                 :             : #include "giptosmessage.h"
      39                 :             : 
      40                 :             : #include <gio/gnetworking.h>
      41                 :             : 
      42                 :             : struct _GIPTosMessage
      43                 :             : {
      44                 :             :   GSocketControlMessage parent_instance;
      45                 :             : 
      46                 :             :   /*< private >*/
      47                 :             : 
      48                 :             :   guint8 tos;
      49                 :             : };
      50                 :             : 
      51                 :          34 : G_DEFINE_TYPE (GIPTosMessage, g_ip_tos_message, G_TYPE_SOCKET_CONTROL_MESSAGE);
      52                 :             : 
      53                 :             : static void
      54                 :           3 : g_ip_tos_message_init (GIPTosMessage *message)
      55                 :             : {
      56                 :           3 : }
      57                 :             : 
      58                 :             : static gsize
      59                 :           2 : g_ip_tos_message_get_size (GSocketControlMessage *message)
      60                 :             : {
      61                 :           2 :   return sizeof (guint8);
      62                 :             : }
      63                 :             : 
      64                 :             : static int
      65                 :           1 : g_ip_tos_message_get_level (GSocketControlMessage *message)
      66                 :             : {
      67                 :           1 :   return IPPROTO_IP;
      68                 :             : }
      69                 :             : 
      70                 :             : static int
      71                 :           1 : g_ip_tos_message_get_msg_type (GSocketControlMessage *message)
      72                 :             : {
      73                 :           1 :   return IP_TOS;
      74                 :             : }
      75                 :             : 
      76                 :             : static void
      77                 :           1 : g_ip_tos_message_serialize (GSocketControlMessage *message, gpointer data)
      78                 :             : {
      79                 :           1 :   *(guint8 *) data = G_IP_TOS_MESSAGE (message)->tos;
      80                 :           1 : }
      81                 :             : 
      82                 :             : static GSocketControlMessage *
      83                 :           2 : g_ip_tos_message_deserialize (gint level, gint type, gsize size, gpointer data)
      84                 :             : {
      85                 :             :   GIPTosMessage *message;
      86                 :             : 
      87                 :           2 :   if (level != IPPROTO_IP || type != IP_TOS)
      88                 :           1 :     return NULL;
      89                 :             : 
      90                 :           1 :   if (size != sizeof (guint8))
      91                 :           0 :     return NULL;
      92                 :             : 
      93                 :           1 :   message = g_object_new (G_TYPE_IP_TOS_MESSAGE, NULL);
      94                 :           1 :   message->tos = *(guint8 *) data;
      95                 :             : 
      96                 :           1 :   return G_SOCKET_CONTROL_MESSAGE (message);
      97                 :             : }
      98                 :             : 
      99                 :             : static void
     100                 :           3 : g_ip_tos_message_class_init (GIPTosMessageClass *klass)
     101                 :             : {
     102                 :           3 :   GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (klass);
     103                 :             : 
     104                 :           3 :   scm_class->get_size = g_ip_tos_message_get_size;
     105                 :           3 :   scm_class->get_level = g_ip_tos_message_get_level;
     106                 :           3 :   scm_class->get_type = g_ip_tos_message_get_msg_type;
     107                 :           3 :   scm_class->serialize = g_ip_tos_message_serialize;
     108                 :           3 :   scm_class->deserialize = g_ip_tos_message_deserialize;
     109                 :           3 : }
     110                 :             : 
     111                 :             : /**
     112                 :             :  * g_ip_tos_message_new:
     113                 :             :  * @dscp: the DSCP value of the message
     114                 :             :  * @ecn: the ECN value of the message
     115                 :             :  *
     116                 :             :  * Creates a new type-of-service message with given DSCP and ECN values.
     117                 :             :  *
     118                 :             :  * Returns: (transfer full): a new type-of-service message
     119                 :             :  * Since: 2.88
     120                 :             :  */
     121                 :             : GSocketControlMessage *
     122                 :           1 : g_ip_tos_message_new (guint8 dscp, GEcnCodePoint ecn)
     123                 :             : {
     124                 :           1 :   GIPTosMessage *msg = g_object_new (G_TYPE_IP_TOS_MESSAGE, NULL);
     125                 :             : 
     126                 :           1 :   msg->tos = (dscp << 2) | ecn;
     127                 :             : 
     128                 :           1 :   return (GSocketControlMessage *) msg;
     129                 :             : }
     130                 :             : 
     131                 :             : /**
     132                 :             :  * g_ip_tos_message_get_dscp:
     133                 :             :  * @message: a type-of-service message
     134                 :             :  *
     135                 :             :  * Gets the differentiated services code point stored in @message.
     136                 :             :  *
     137                 :             :  * Returns: A DSCP value as described in [RFC 2474](https://www.rfc-editor.org/rfc/rfc2474.html#section-3).
     138                 :             :  *
     139                 :             :  * Since: 2.88
     140                 :             :  */
     141                 :             : guint8
     142                 :           2 : g_ip_tos_message_get_dscp (GIPTosMessage *message)
     143                 :             : {
     144                 :           2 :   return message->tos >> 2;
     145                 :             : }
     146                 :             : 
     147                 :             : /**
     148                 :             :  * g_ip_tos_message_get_ecn:
     149                 :             :  * @message: a type-of-service message
     150                 :             :  *
     151                 :             :  * Gets the Explicit Congestion Notification code point stored in @message.
     152                 :             :  *
     153                 :             :  * Returns: An ECN value as described in [RFC 3168](https://www.rfc-editor.org/rfc/rfc3168#section-5).
     154                 :             :  *
     155                 :             :  * Since: 2.88
     156                 :             :  */
     157                 :             : GEcnCodePoint
     158                 :           2 : g_ip_tos_message_get_ecn (GIPTosMessage *message)
     159                 :             : {
     160                 :           2 :   return message->tos & 0x3;
     161                 :             : }
        

Generated by: LCOV version 2.0-1