LCOV - code coverage report
Current view: top level - shared - ip-protocol-list.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 28 29 96.6 %
Date: 2019-08-22 15:41:25 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <errno.h>
       4             : #include <netinet/in.h>
       5             : 
       6             : #include "alloc-util.h"
       7             : #include "ip-protocol-list.h"
       8             : #include "macro.h"
       9             : #include "parse-util.h"
      10             : #include "string-util.h"
      11             : 
      12             : static const struct ip_protocol_name* lookup_ip_protocol(register const char *str, register GPERF_LEN_TYPE len);
      13             : 
      14             : #include "ip-protocol-from-name.h"
      15             : #include "ip-protocol-to-name.h"
      16             : 
      17          20 : const char *ip_protocol_to_name(int id) {
      18             : 
      19          20 :         if (id < 0)
      20           4 :                 return NULL;
      21             : 
      22          16 :         if ((size_t) id >= ELEMENTSOF(ip_protocol_names))
      23           4 :                 return NULL;
      24             : 
      25          12 :         return ip_protocol_names[id];
      26             : }
      27             : 
      28          25 : int ip_protocol_from_name(const char *name) {
      29             :         const struct ip_protocol_name *sc;
      30             : 
      31          25 :         assert(name);
      32             : 
      33          25 :         sc = lookup_ip_protocol(name, strlen(name));
      34          25 :         if (!sc)
      35          14 :                 return -EINVAL;
      36             : 
      37          11 :         return sc->id;
      38             : }
      39             : 
      40          17 : int parse_ip_protocol(const char *s) {
      41          17 :         _cleanup_free_ char *str = NULL;
      42             :         int i, r;
      43             : 
      44          17 :         assert(s);
      45             : 
      46          17 :         if (isempty(s))
      47           1 :                 return IPPROTO_IP;
      48             : 
      49             :         /* Do not use strdupa() here, as the input string may come from *
      50             :          * command line or config files. */
      51          16 :         str = strdup(s);
      52          16 :         if (!str)
      53           0 :                 return -ENOMEM;
      54             : 
      55          16 :         i = ip_protocol_from_name(ascii_strlower(str));
      56          16 :         if (i >= 0)
      57           5 :                 return i;
      58             : 
      59          11 :         r = safe_atoi(str, &i);
      60          11 :         if (r < 0)
      61           1 :                 return r;
      62             : 
      63          10 :         if (!ip_protocol_to_name(i))
      64           6 :                 return -EINVAL;
      65             : 
      66           4 :         return i;
      67             : }

Generated by: LCOV version 1.14