Coverage Report

Created: 2024-02-29 06:05

/src/strongswan/src/libstrongswan/networking/streams/stream_unix.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2013 Martin Willi
3
 *
4
 * Copyright (C) secunet Security Networks AG
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License as published by the
8
 * Free Software Foundation; either version 2 of the License, or (at your
9
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
10
 *
11
 * This program is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
 * for more details.
15
 */
16
17
#include <library.h>
18
#include <errno.h>
19
#include <unistd.h>
20
#include <limits.h>
21
22
#include "stream_unix.h"
23
24
/**
25
 * See header
26
 */
27
int stream_parse_uri_unix(char *uri, struct sockaddr_un *addr)
28
0
{
29
0
  if (!strpfx(uri, "unix://"))
30
0
  {
31
0
    return -1;
32
0
  }
33
0
  uri += strlen("unix://");
34
35
0
  memset(addr, 0, sizeof(*addr));
36
0
  addr->sun_family = AF_UNIX;
37
0
  strncpy(addr->sun_path, uri, sizeof(addr->sun_path));
38
0
  addr->sun_path[sizeof(addr->sun_path)-1] = '\0';
39
40
0
  return offsetof(struct sockaddr_un, sun_path) + strlen(addr->sun_path);
41
0
}
42
43
/**
44
 * See header
45
 */
46
stream_t *stream_create_unix(char *uri)
47
0
{
48
0
  struct sockaddr_un addr;
49
0
  int len, fd;
50
51
0
  len = stream_parse_uri_unix(uri, &addr);
52
0
  if (len == -1)
53
0
  {
54
0
    DBG1(DBG_NET, "invalid stream URI: '%s'", uri);
55
0
    return NULL;
56
0
  }
57
0
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
58
0
  if (fd < 0)
59
0
  {
60
0
    DBG1(DBG_NET, "opening socket '%s' failed: %s", uri, strerror(errno));
61
0
    return NULL;
62
0
  }
63
0
  if (connect(fd, (struct sockaddr*)&addr, len) < 0)
64
0
  {
65
0
    DBG1(DBG_NET, "connecting to '%s' failed: %s", uri, strerror(errno));
66
0
    close(fd);
67
0
    return NULL;
68
0
  }
69
0
  return stream_create_from_fd(fd);
70
0
}