1 /*
2  * virnetsocket.h: generic network socket handling
3  *
4  * Copyright (C) 2006-2011 Red Hat, Inc.
5  * Copyright (C) 2006 Daniel P. Berrange
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library.  If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
22 #pragma once
23 
24 #include "virsocketaddr.h"
25 #include "vircommand.h"
26 #include "virnettlscontext.h"
27 #include "virobject.h"
28 #ifdef WITH_SASL
29 # include "virnetsaslcontext.h"
30 #endif
31 #include "virjson.h"
32 #include "viruri.h"
33 
34 typedef struct _virNetSocket virNetSocket;
35 
36 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetSocket, virObjectUnref);
37 
38 typedef void (*virNetSocketIOFunc)(virNetSocket *sock,
39                                    int events,
40                                    void *opaque);
41 
42 
43 int virNetSocketCheckProtocols(bool *hasIPv4,
44                                bool *hasIPv6);
45 
46 int virNetSocketNewListenTCP(const char *nodename,
47                              const char *service,
48                              int family,
49                              virNetSocket ***addrs,
50                              size_t *naddrs);
51 
52 int virNetSocketNewListenUNIX(const char *path,
53                               mode_t mask,
54                               uid_t user,
55                               gid_t grp,
56                               virNetSocket **addr);
57 
58 int virNetSocketNewListenFD(int fd,
59                             bool unlinkUNIX,
60                             virNetSocket **addr);
61 
62 int virNetSocketNewConnectTCP(const char *nodename,
63                               const char *service,
64                               int family,
65                               virNetSocket **addr);
66 
67 int virNetSocketNewConnectUNIX(const char *path,
68                                const char *spawnDaemonPath,
69                                virNetSocket **addr);
70 
71 int virNetSocketNewConnectCommand(virCommand *cmd,
72                                   virNetSocket **retsock);
73 
74 int virNetSocketNewConnectSSH(const char *nodename,
75                               const char *service,
76                               const char *binary,
77                               const char *username,
78                               bool noTTY,
79                               bool noVerify,
80                               const char *keyfile,
81                               const char *command,
82                               virNetSocket **addr);
83 
84 int virNetSocketNewConnectLibSSH2(const char *host,
85                                   const char *port,
86                                   int family,
87                                   const char *username,
88                                   const char *privkey,
89                                   const char *knownHosts,
90                                   const char *knownHostsVerify,
91                                   const char *authMethods,
92                                   const char *command,
93                                   virConnectAuthPtr auth,
94                                   virURI *uri,
95                                   virNetSocket **retsock);
96 
97 int virNetSocketNewConnectLibssh(const char *host,
98                                  const char *port,
99                                  int family,
100                                  const char *username,
101                                  const char *privkey,
102                                  const char *knownHosts,
103                                  const char *knownHostsVerify,
104                                  const char *authMethods,
105                                  const char *command,
106                                  virConnectAuthPtr auth,
107                                  virURI *uri,
108                                  virNetSocket **retsock);
109 
110 int virNetSocketNewConnectExternal(const char **cmdargv,
111                                    virNetSocket **addr);
112 
113 int virNetSocketNewConnectSockFD(int sockfd,
114                                  virNetSocket **retsock);
115 
116 virNetSocket *virNetSocketNewPostExecRestart(virJSONValue *object);
117 
118 virJSONValue *virNetSocketPreExecRestart(virNetSocket *sock);
119 
120 int virNetSocketGetFD(virNetSocket *sock);
121 int virNetSocketDupFD(virNetSocket *sock, bool cloexec);
122 
123 bool virNetSocketIsLocal(virNetSocket *sock);
124 
125 bool virNetSocketHasPassFD(virNetSocket *sock);
126 
127 char *virNetSocketGetPath(virNetSocket *sock);
128 int virNetSocketGetPort(virNetSocket *sock);
129 
130 int virNetSocketGetUNIXIdentity(virNetSocket *sock,
131                                 uid_t *uid,
132                                 gid_t *gid,
133                                 pid_t *pid,
134                                 unsigned long long *timestamp)
135     G_GNUC_NO_INLINE;
136 int virNetSocketGetSELinuxContext(virNetSocket *sock,
137                                   char **context)
138     G_GNUC_NO_INLINE;
139 
140 int virNetSocketSetBlocking(virNetSocket *sock,
141                             bool blocking);
142 
143 void virNetSocketSetQuietEOF(virNetSocket *sock);
144 
145 ssize_t virNetSocketRead(virNetSocket *sock, char *buf, size_t len);
146 ssize_t virNetSocketWrite(virNetSocket *sock, const char *buf, size_t len);
147 
148 int virNetSocketSendFD(virNetSocket *sock, int fd);
149 int virNetSocketRecvFD(virNetSocket *sock, int *fd);
150 
151 void virNetSocketSetTLSSession(virNetSocket *sock,
152                                virNetTLSSession *sess);
153 
154 #ifdef WITH_SASL
155 void virNetSocketSetSASLSession(virNetSocket *sock,
156                                 virNetSASLSession *sess);
157 #endif
158 bool virNetSocketHasCachedData(virNetSocket *sock);
159 bool virNetSocketHasPendingData(virNetSocket *sock);
160 
161 const char *virNetSocketLocalAddrStringSASL(virNetSocket *sock);
162 const char *virNetSocketRemoteAddrStringSASL(virNetSocket *sock);
163 const char *virNetSocketRemoteAddrStringURI(virNetSocket *sock);
164 
165 int virNetSocketListen(virNetSocket *sock, int backlog);
166 int virNetSocketAccept(virNetSocket *sock,
167                        virNetSocket **clientsock);
168 
169 int virNetSocketAddIOCallback(virNetSocket *sock,
170                               int events,
171                               virNetSocketIOFunc func,
172                               void *opaque,
173                               virFreeCallback ff);
174 
175 void virNetSocketUpdateIOCallback(virNetSocket *sock,
176                                   int events);
177 
178 void virNetSocketRemoveIOCallback(virNetSocket *sock);
179 
180 void virNetSocketClose(virNetSocket *sock);
181