2 * libvdeplug - A library to connect to a VDE Switch.
3 * Copyright (C) 2006 Renzo Davoli, University of Bologna
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation version 2.1 of the License, or (at
8 * your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
13 * General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "libvdeplug.h"
22 #include <sys/socket.h>
29 #include <sys/types.h>
34 #define VDESTDSOCK "/var/run/vde.ctl"
35 #define VDETMPSOCK "/tmp/vde.ctl"
41 struct sockaddr_un inpath
;
44 #define SWITCH_MAGIC 0xfeedface
47 enum request_type
{ REQ_NEW_CONTROL
};
52 enum request_type type
;
53 struct sockaddr_un sock
;
54 char description
[MAXDESCR
];
57 VDECONN
*vde_open_real(char *sockname
,char *descr
,int interface_version
,
58 struct vde_open_args
*open_args
)
61 struct passwd
*callerpwd
;
62 struct request_v3 req
;
64 static struct sockaddr_un sockun
;
65 static struct sockaddr_un dataout
;
72 if (open_args
!= NULL
) {
73 if (interface_version
== 1) {
75 group
=open_args
->group
;
84 if ((conn
=calloc(1,sizeof(struct vdeconn
)))==NULL
)
90 callerpwd
=getpwuid(getuid());
91 if((conn
->fddata
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) < 0){
97 if((conn
->fdctl
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0){
104 if (sockname
== NULL
|| *sockname
== NULL
)
108 if(sockname
[strlen(sockname
)-1] == ']' && (split
=rindex(sockname
,'[')) != NULL
) {
112 if (*sockname
==0) sockname
=VDESTDSOCK
;
115 sockun
.sun_family
= AF_UNIX
;
116 snprintf(sockun
.sun_path
, sizeof(sockun
.sun_path
), "%s/ctl", sockname
);
117 if(connect(conn
->fdctl
, (struct sockaddr
*) &sockun
, sizeof(sockun
))){
118 if (sockname
== VDESTDSOCK
) {
120 snprintf(sockun
.sun_path
, sizeof(sockun
.sun_path
), "%s/ctl", sockname
);
121 if(connect(conn
->fdctl
, (struct sockaddr
*) &sockun
, sizeof(sockun
))){
122 snprintf(sockun
.sun_path
, sizeof(sockun
.sun_path
), "%s", sockname
);
123 if(connect(conn
->fdctl
, (struct sockaddr
*) &sockun
, sizeof(sockun
))){
134 req
.magic
=SWITCH_MAGIC
;
136 req
.type
=REQ_NEW_CONTROL
+(port
<< 8);
137 req
.sock
.sun_family
=AF_UNIX
;
139 /* First choice, store the return socket from the switch in the control dir*/
140 memset(req
.sock
.sun_path
, 0, sizeof(req
.sock
.sun_path
));
143 sprintf(req
.sock
.sun_path
, "%s.%05d-%05d", sockname
, pid
, sockno
++);
144 res
=bind(conn
->fddata
, (struct sockaddr
*) &req
.sock
, sizeof (req
.sock
));
146 while (res
< 0 && errno
== EADDRINUSE
);
148 /* if it is not possible -> /tmp */
149 memset(req
.sock
.sun_path
, 0, sizeof(req
.sock
.sun_path
));
152 sprintf(req
.sock
.sun_path
, "/tmp/vde.%05d-%05d", pid
, sockno
++);
153 res
=bind(conn
->fddata
, (struct sockaddr
*) &req
.sock
, sizeof (req
.sock
));
155 while (res
< 0 && errno
== EADDRINUSE
);
167 memcpy(&(conn
->inpath
),&req
.sock
,sizeof(req
.sock
));
171 if ((gs
=getgrnam(group
)) == NULL
)
175 chown(conn
->inpath
.sun_path
,-1,gid
);
178 chmod(conn
->inpath
.sun_path
,mode
);
180 snprintf(req
.description
,MAXDESCR
,"%s user=%s PID=%d %s SOCK=%s",
181 descr
,(callerpwd
!= NULL
)?callerpwd
->pw_name
:"??",
182 pid
,getenv("SSH_CLIENT")?getenv("SSH_CLIENT"):"",req
.sock
.sun_path
);
184 if (send(conn
->fdctl
,&req
,sizeof(req
)-MAXDESCR
+strlen(req
.description
),0) < 0) {
193 if (recv(conn
->fdctl
,&(dataout
),sizeof(struct sockaddr_un
),0)<0) {
202 if (connect(conn
->fddata
,(struct sockaddr
*)&(dataout
),sizeof(struct sockaddr_un
))<0) {
210 chmod(dataout
.sun_path
,mode
);
215 ssize_t
vde_recv(VDECONN
*conn
,char *buf
,size_t len
,int flags
)
217 if (__builtin_expect(conn
!=0,1))
218 return recv(conn
->fddata
,buf
,len
,0);
225 ssize_t
vde_send(VDECONN
*conn
,const char *buf
,size_t len
,int flags
)
227 if (__builtin_expect(conn
!=0,1))
228 return send(conn
->fddata
,buf
,len
,0);
235 int vde_datafd(VDECONN
*conn
)
237 if (__builtin_expect(conn
!=0,1))
245 int vde_ctlfd(VDECONN
*conn
)
247 if (__builtin_expect(conn
!=0,1))
255 int vde_close(VDECONN
*conn
)
257 if (__builtin_expect(conn
!=0,1)) {
258 unlink(conn
->inpath
.sun_path
);