File: libipvs.c

package info (click to toggle)
ipvsadm 1.20release6-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 248 kB
  • ctags: 456
  • sloc: ansic: 2,411; sh: 276; makefile: 118
file content (230 lines) | stat: -rw-r--r-- 5,569 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * libipvs:	Library for manipulating IPVS through [gs]etsockopt
 *
 * Version:     $Id: libipvs.c,v 1.4 2001/11/23 14:34:17 wensong Exp $
 *
 * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
 *
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>

#include "libipvs/libipvs.h"

#define SET_CMD(cmd)	(cmd - IP_VS_BASE_CTL)
#define GET_CMD(cmd)	(cmd - IP_VS_BASE_CTL + 128)

static int sockfd = -1;
static int ipvs_cmd = 0;
struct ip_vs_getinfo ipvs_info;


int ipvs_init(void)
{
	socklen_t len;

	len = sizeof(ipvs_info);
	if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1)
		return -1;

	ipvs_cmd = GET_CMD(IP_VS_SO_GET_INFO);
	if (getsockopt(sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
		       (char *)&ipvs_info, &len))
		return -1;

	return 0;
}


int ipvs_getinfo(void)
{
	socklen_t len;

	len = sizeof(ipvs_info);
	ipvs_cmd = GET_CMD(IP_VS_SO_GET_INFO);
	return getsockopt(sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
			  (char *)&ipvs_info, &len);
}


unsigned int ipvs_version(void)
{
	return ipvs_info.version;
}


int ipvs_command(int cmd, struct ip_vs_rule_user *urule)
{
	ipvs_cmd = SET_CMD(cmd);
	return setsockopt(sockfd, IPPROTO_IP,
			  cmd, (char *)urule, sizeof(*urule));
}


struct ip_vs_get_services *ipvs_get_services(void)
{
	struct ip_vs_get_services *get;
	socklen_t len;

	len = sizeof(*get) +
		sizeof(struct ip_vs_service_user)*ipvs_info.num_services;
	if (!(get = malloc(len)))
		return NULL;

	ipvs_cmd = GET_CMD(IP_VS_SO_GET_SERVICES);
	get->num_services = ipvs_info.num_services;
	if (getsockopt(sockfd, IPPROTO_IP,
		       IP_VS_SO_GET_SERVICES, get, &len) < 0) {
		free(get);
		return NULL;
	}
	return get;
}


struct ip_vs_get_dests *ipvs_get_dests(struct ip_vs_service_user *svc)
{
	struct ip_vs_get_dests *d;
	socklen_t len;

	len = sizeof(*d) + sizeof(struct ip_vs_dest_user)*svc->num_dests;
	if (!(d = malloc(len)))
		return NULL;

	ipvs_cmd = GET_CMD(IP_VS_SO_GET_DESTS);
	d->fwmark = svc->fwmark;
	d->protocol = svc->protocol;
	d->addr = svc->addr;
	d->port = svc->port;
	d->num_dests = svc->num_dests;

	if (getsockopt(sockfd, IPPROTO_IP,
		       IP_VS_SO_GET_DESTS, d, &len) < 0) {
		free(d);
		return NULL;
	}
	return d;
}

struct ip_vs_service_user *
ipvs_get_service(__u32 fwmark, __u16 protocol, __u32 vaddr, __u16 vport)
{
	struct ip_vs_service_user *svc;
	socklen_t len;

	len = sizeof(*svc);
	if (!(svc = malloc(len)))
		return NULL;

	ipvs_cmd = GET_CMD(IP_VS_SO_GET_SERVICE);
	svc->fwmark = fwmark;
	svc->protocol = protocol;
	svc->addr = vaddr;
	svc->port = vport;
	if (getsockopt(sockfd, IPPROTO_IP, IP_VS_SO_GET_SERVICE,
		       (char *)svc, &len)) {
		free(svc);
		return NULL;
	}
	return svc;
}


struct ip_vs_timeout_user *ipvs_get_timeouts(void)
{
	struct ip_vs_timeout_user *u;
	socklen_t len;

	len = sizeof(*u);
	if (!(u = malloc(len)))
		return NULL;

	ipvs_cmd = GET_CMD(IP_VS_SO_GET_TIMEOUTS);
	if (getsockopt(sockfd, IPPROTO_IP, IP_VS_SO_GET_TIMEOUTS,
		       (char *)u, &len)) {
		free(u);
		return NULL;
	}
	return u;
}


struct ip_vs_daemon_user *ipvs_get_daemon(void)
{
	struct ip_vs_daemon_user *u;
	socklen_t len;

	len = sizeof(*u);
	if (!(u = malloc(len)))
		return NULL;

	ipvs_cmd = GET_CMD(IP_VS_SO_GET_DAEMON);
	if (getsockopt(sockfd, IPPROTO_IP,
		       IP_VS_SO_GET_DAEMON, (char *)u, &len)) {
		free(u);
		return NULL;
	}
	return u;
}


void ipvs_close(void)
{
	close(sockfd);
}


const char *ipvs_strerror(int err)
{
	unsigned int i;
	struct table_struct {
		int cmd;
		int err;
		const char *message;
	} table [] =
	  { { 0, EPERM, "Permission denied (you must be root)" },
	    { 0, EINVAL, "Module is wrong version" },
	    { 0, ENOPROTOOPT, "Protocol not available" },
	    { 0, ENOMEM, "Memory allocation problem" },
	    { SET_CMD(IP_VS_SO_SET_ADD), EEXIST, "Service already exists" },
	    { SET_CMD(IP_VS_SO_SET_ADD), ENOENT, "Scheduler not found" },
	    { SET_CMD(IP_VS_SO_SET_EDIT), ESRCH, "No such service" },
	    { SET_CMD(IP_VS_SO_SET_EDIT), ENOENT, "Scheduler not found" },
	    { SET_CMD(IP_VS_SO_SET_DEL), ESRCH, "No such service" },
	    { SET_CMD(IP_VS_SO_SET_ADDDEST), ESRCH, "Service not defined" },
	    { SET_CMD(IP_VS_SO_SET_ADDDEST), EEXIST,
	      "Destination already exists" },
	    { SET_CMD(IP_VS_SO_SET_EDITDEST), ESRCH, "Service not defined" },
	    { SET_CMD(IP_VS_SO_SET_EDITDEST), ENOENT, "No such destination" },
	    { SET_CMD(IP_VS_SO_SET_DELDEST), ESRCH, "Service not defined" },
	    { SET_CMD(IP_VS_SO_SET_DELDEST), ENOENT, "No such destination" },
	    { SET_CMD(IP_VS_SO_SET_STARTDAEMON), EEXIST,
	      "Daemon has already run" },
	    { SET_CMD(IP_VS_SO_SET_STOPDAEMON), ESRCH,
	      "No daemon is running" },
	    { SET_CMD(IP_VS_SO_SET_STOPDAEMON), ESRCH,
	      "No daemon is running" },
	    { SET_CMD(IP_VS_SO_SET_ZERO), ESRCH, "No such service" },
	    { GET_CMD(IP_VS_SO_GET_SERVICE), ESRCH, "No such service" },
	    { GET_CMD(IP_VS_SO_GET_DESTS), ESRCH, "No such service" },
	  };

	for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
		if ((!table[i].cmd || table[i].cmd == ipvs_cmd)
		    && table[i].err == err)
			return table[i].message;
	}

	return strerror(err);
}