File: teamd_usock_common.h

package info (click to toggle)
libteam 1.12-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,320 kB
  • ctags: 2,189
  • sloc: ansic: 16,808; sh: 1,190; python: 613; makefile: 110
file content (93 lines) | stat: -rw-r--r-- 2,322 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 *   teamd_usock_common.h - Teamd unix socket api common things
 *   Copyright (C) 2013 Jiri Pirko <jiri@resnulli.us>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef _TEAMD_USOCK_COMMON_H_
#define _TEAMD_USOCK_COMMON_H_

#include <stdio.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>

#include "teamd.h"

#define TEAMD_USOCK_REQUEST_PREFIX	"REQUEST"
#define TEAMD_USOCK_REPLY_ERR_PREFIX	"REPLY_ERROR"
#define TEAMD_USOCK_REPLY_SUCC_PREFIX	"REPLY_SUCCESS"

static inline void teamd_usock_get_sockpath(char *sockpath, size_t sockpath_len,
					    const char *team_devname)
{
	snprintf(sockpath, sockpath_len, TEAMD_RUN_DIR"%s.sock", team_devname);
}

static inline int teamd_usock_recv_msg(int sock, char **p_str)
{
	ssize_t len;
	int expected_len;
	char *buf;
	int ret;

	ret = ioctl(sock, SIOCINQ, &expected_len);
	if (ret == -1)
		return -errno;

	buf = malloc(expected_len + 1);
	if (!buf)
		return -ENOMEM;
	len = recv(sock, buf, expected_len, 0);
	switch (len) {
	case -1:
		free(buf);
		return -errno;
	case 0:
		free(buf);
		/* use EPIPE to tell caller the connection was broken */
		return -EPIPE;
	}
	buf[len] = '\0';
	*p_str = buf;
	return 0;
}

static inline char *teamd_usock_msg_getline(char **p_rest)
{
	char *start = NULL;
	char *rest = NULL;
	char *str = *p_rest;

	if (!str)
		return NULL;
	while (1) {
		if (*str == '\0')
			break;
		if ((*str != '\n') && !start)
			start = str;
		if ((*str == '\n') && start) {
			*str = '\0';
			rest = str + 1;
			break;
		}
		str++;
	}
	*p_rest = rest;
	return start;
}

#endif /* _TEAMD_USOCK_COMMON_H_ */