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
|
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "bacon-message-connection.h"
#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
#endif
struct BaconMessageConnection {
gboolean is_server;
int fd;
char *path;
GIOChannel *chan;
/* callback */
void (*func) (const char *message, gpointer user_data);
gpointer data;
};
static gboolean
test_is_socket (const char *path)
{
struct stat s;
if (stat (path, &s) == -1)
return FALSE;
if (S_ISSOCK (s.st_mode))
return TRUE;
return FALSE;
}
#define BUF_SIZE 1024
static gboolean
server_cb (GIOChannel *source, GIOCondition condition, gpointer data)
{
BaconMessageConnection *conn = (BaconMessageConnection *)data;
char *message, buf[BUF_SIZE];
int cd, alen, rc, offset;
message = NULL;
offset = 0;
cd = accept (g_io_channel_unix_get_fd (source), NULL, &alen);
memset (buf, sizeof (buf), '\0');
rc = read (cd, buf, BUF_SIZE);
while (rc != 0)
{
message = g_realloc (message, rc + offset);
memcpy (message + offset, buf, MIN(rc, BUF_SIZE));
offset = offset + rc;
memset (buf, sizeof (buf), '\0');
rc = read (cd, buf, BUF_SIZE);
}
if (message != NULL && conn->func != NULL)
(*conn->func) (message, conn->data);
g_free (message);
return TRUE;
}
static gboolean
try_server (BaconMessageConnection *conn)
{
struct sockaddr_un uaddr;
GError *err = NULL;
uaddr.sun_family = AF_UNIX;
strncpy (uaddr.sun_path, conn->path,
MIN (strlen(conn->path)+1, UNIX_PATH_MAX));
conn->fd = socket (PF_UNIX, SOCK_STREAM, 0);
if (bind (conn->fd, (struct sockaddr *) &uaddr, sizeof (uaddr)) == -1)
{
conn->fd = -1;
return FALSE;
}
listen (conn->fd, 5);
conn->chan = g_io_channel_unix_new (conn->fd);
if (conn->chan == NULL || err != NULL)
{
conn->fd = -1;
return FALSE;
}
g_io_add_watch (conn->chan, G_IO_IN, server_cb, conn);
return TRUE;
}
static gboolean
try_client (BaconMessageConnection *conn)
{
struct sockaddr_un uaddr;
uaddr.sun_family = AF_UNIX;
strncpy (uaddr.sun_path, conn->path,
MIN(strlen(conn->path)+1, UNIX_PATH_MAX));
conn->fd = socket (PF_UNIX, SOCK_STREAM, 0);
if (connect (conn->fd, (struct sockaddr *) &uaddr,
sizeof (uaddr)) == -1)
{
conn->fd = -1;
return FALSE;
}
return TRUE;
}
BaconMessageConnection *
bacon_message_connection_new (const char *prefix)
{
BaconMessageConnection *conn;
char *filename, *path;
g_return_val_if_fail (prefix != NULL, NULL);
filename = g_strdup_printf ("%s.%s", prefix, g_get_user_name ());
path = g_build_filename (G_DIR_SEPARATOR_S, g_get_tmp_dir (),
filename, NULL);
g_free (filename);
conn = g_new0 (BaconMessageConnection, 1);
conn->path = path;
if (test_is_socket (conn->path) == FALSE)
{
try_server (conn);
if (conn->fd == -1)
{
bacon_message_connection_free (conn);
return NULL;
}
conn->is_server = TRUE;
return conn;
}
if (try_client (conn) == FALSE)
{
unlink (path);
try_server (conn);
if (conn->fd == -1)
{
bacon_message_connection_free (conn);
return NULL;
}
conn->is_server = TRUE;
return conn;
}
conn->is_server = FALSE;
return conn;
}
void
bacon_message_connection_free (BaconMessageConnection *conn)
{
g_return_if_fail (conn != NULL);
g_return_if_fail (conn->path != NULL);
if (conn->is_server == TRUE)
{
g_io_channel_shutdown (conn->chan, FALSE, NULL);
g_io_channel_unref (conn->chan);
unlink (conn->path);
} else {
close (conn->fd);
}
g_free (conn->path);
g_free (conn);
}
void
bacon_message_connection_set_callback (BaconMessageConnection *conn,
BaconMessageReceivedFunc func,
gpointer user_data)
{
g_return_if_fail (conn != NULL);
g_assert (conn->is_server == TRUE);
conn->func = func;
conn->data = user_data;
}
void
bacon_message_connection_send (BaconMessageConnection *conn,
const char *message)
{
g_return_if_fail (conn != NULL);
g_assert (conn->is_server == FALSE);
write (conn->fd, message, strlen (message) + 1);
}
gboolean
bacon_message_connection_get_is_server (BaconMessageConnection *conn)
{
g_return_val_if_fail (conn != NULL, FALSE);
return conn->is_server;
}
|