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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <X11/Xatom.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include "gcin.h"
#include "gcin-protocol.h"
#include "im-srv.h"
#include <gdk/gdk.h>
int im_sockfd, im_tcp_sockfd;
Atom get_gcin_sockpath_atom(Display *dpy);
Server_IP_port srv_ip_port;
static Window prop_win;
static Atom addr_atom;
#ifdef __cplusplus
extern "C" gint gdk_input_add (gint source,
GdkInputCondition condition,
GdkInputFunction function,
gpointer data);
#endif
void gdk_input_remove (gint tag);
void get_gcin_im_srv_sock_path(char *outstr, int outstrN);
void process_client_req(int fd);
#if UNIX
static gboolean cb_read_gcin_client_data(GIOChannel *source, GIOCondition condition, gpointer data)
{
int fd=GPOINTER_TO_INT(data);
process_client_req(fd);
return TRUE;
}
#endif
Atom get_gcin_addr_atom(Display *dpy);
static void gen_passwd_idx()
{
srv_ip_port.passwd.seed = (rand() >> 1) % __GCIN_PASSWD_N_;
Server_IP_port tsrv_ip_port = srv_ip_port;
to_gcin_endian_4(&srv_ip_port.passwd.seed);
XChangeProperty(dpy, prop_win , addr_atom, XA_STRING, 8,
PropModeReplace, (unsigned char *)&tsrv_ip_port, sizeof(srv_ip_port));
XSync(GDK_DISPLAY(), FALSE);
}
static gboolean cb_new_gcin_client(GIOChannel *source, GIOCondition condition, gpointer data)
{
Connection_type type=(Connection_type) GPOINTER_TO_INT(data);
#if 0
dbg("im-srv: cb_new_gcin_client %s\n", type==Connection_type_unix ? "unix":"tcp");
#endif
unsigned int newsockfd;
socklen_t clilen;
if (type==Connection_type_unix) {
struct sockaddr_un cli_addr;
bzero(&cli_addr, sizeof(cli_addr));
clilen=0;
newsockfd = accept(im_sockfd,(struct sockaddr *) & cli_addr, &clilen);
} else
{
struct sockaddr_in cli_addr;
bzero(&cli_addr, sizeof(cli_addr));
clilen=sizeof(cli_addr);
newsockfd = accept(im_tcp_sockfd,(struct sockaddr *) & cli_addr, &clilen);
}
if (newsockfd < 0) {
perror("accept");
return FALSE;
}
#if 1
int flags = fcntl(newsockfd, F_GETFL, 0);
fcntl(newsockfd, F_SETFL, flags|O_NONBLOCK);
#endif
// dbg("newsockfd %d\n", newsockfd);
if (newsockfd >= gcin_clientsN) {
gcin_clients = trealloc(gcin_clients, GCIN_ENT, newsockfd+1);
gcin_clientsN = newsockfd;
}
bzero(&gcin_clients[newsockfd], sizeof(gcin_clients[0]));
gcin_clients[newsockfd].tag = g_io_add_watch(gcin_clients[newsockfd].channel=g_io_channel_unix_new(newsockfd), G_IO_IN, cb_read_gcin_client_data,
GINT_TO_POINTER(newsockfd));
if (type==Connection_type_tcp) {
gcin_clients[newsockfd].seed = srv_ip_port.passwd.seed;
gen_passwd_idx();
}
gcin_clients[newsockfd].type = type;
return TRUE;
}
static int get_ip_address(u_int *ip)
{
#if 0
char hostname[64];
if (gethostname(hostname, sizeof(hostname)) < 0) {
perror("cannot get hostname\n");
return -1;
}
dbg("hostname %s\n", hostname);
struct hostent *hent;
if (!(hent=gethostbyname(hostname))) {
dbg("cannot call gethostbyname to get IP address");
return -1;
}
memcpy(ip, hent->h_addr_list[0], hent->h_length);
#else
struct ifaddrs *ifaddr = NULL, *ifa;
int family;
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr)
continue;
family = ifa->ifa_addr->sa_family;
if (family == AF_INET) {
struct sockaddr_in *padd=(struct sockaddr_in *)ifa->ifa_addr;
char *ipaddr = inet_ntoa(padd->sin_addr);
if (!strcmp(ipaddr, "127.0.0.1"))
continue;
dbg("ip addr %s\n", ipaddr);
memcpy(ip, &padd->sin_addr.s_addr, INET_ADDRSTRLEN);
break;
}
}
freeifaddrs(ifaddr);
#endif
return 0;
}
void start_pipe_svr();
void init_gcin_im_serv(Window win)
{
dbg("init_gcin_im_serv\n");
int servlen;
prop_win = win;
struct sockadd_un;
struct sockaddr_un serv_addr;
// unix socket
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
char sock_path[128];
get_gcin_im_srv_sock_path(sock_path, sizeof(sock_path));
strcpy(serv_addr.sun_path, sock_path);
#ifdef SUN_LEN
servlen = SUN_LEN (&serv_addr);
#else
servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
#endif
dbg("sock -- %s\n",serv_addr.sun_path);
struct stat st;
if (!stat(serv_addr.sun_path, &st)) {
if (unlink(serv_addr.sun_path) < 0) {
char tt[512];
snprintf(tt, sizeof(tt), "unlink error %s", serv_addr.sun_path);
perror(tt);
}
}
if ((im_sockfd = socket(AF_UNIX,SOCK_STREAM,0)) < 0) {
perror("cannot open unix socket");
exit(-1);
}
if (bind(im_sockfd, (struct sockaddr *) &serv_addr, servlen) < 0) {
perror("cannot bind");
exit(-1);
}
listen(im_sockfd,2);
dbg("im_sockfd:%d\n", im_sockfd);
g_io_add_watch(g_io_channel_unix_new(im_sockfd), G_IO_IN, cb_new_gcin_client,
GINT_TO_POINTER(Connection_type_unix));
Display *dpy = GDK_DISPLAY();
Server_sock_path srv_sockpath;
strcpy(srv_sockpath.sock_path, sock_path);
Atom sockpath_atom = get_gcin_sockpath_atom(dpy);
XChangeProperty(dpy, prop_win , sockpath_atom, XA_STRING, 8,
PropModeReplace, (unsigned char *)&srv_sockpath, sizeof(srv_sockpath));
addr_atom = get_gcin_addr_atom(dpy);
XSetSelectionOwner(dpy, addr_atom, win, CurrentTime);
if (!gcin_remote_client) {
dbg("connection via TCP is disabled\n");
return;
}
// tcp socket
if (get_ip_address(&srv_ip_port.ip) < 0)
return;
if ((im_tcp_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("cannot open tcp socket");
exit(-1);
}
// dbg("socket succ\n");
struct sockaddr_in serv_addr_tcp;
u_short port;
for(port=9999; port < 20000; port++)
{
// tcp socket
bzero(&serv_addr_tcp, sizeof(serv_addr_tcp));
serv_addr_tcp.sin_family = AF_INET;
serv_addr_tcp.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr_tcp.sin_port = htons(port);
if (bind(im_tcp_sockfd, (struct sockaddr *) &serv_addr_tcp, sizeof(serv_addr_tcp)) == 0)
break;
}
srv_ip_port.port = serv_addr_tcp.sin_port;
dbg("server port bind to %s:%d\n", inet_ntoa(serv_addr_tcp.sin_addr), port);
time_t t;
srand(time(&t));
int i;
for(i=0; i < __GCIN_PASSWD_N_; i++) {
srv_ip_port.passwd.passwd[i] = (rand()>>2) & 0xff;
}
if (listen(im_tcp_sockfd, 5) < 0) {
perror("cannot listen: ");
exit(1);
}
dbg("after listen\n");
gen_passwd_idx();
g_io_add_watch(g_io_channel_unix_new(im_tcp_sockfd), G_IO_IN, cb_new_gcin_client,
GINT_TO_POINTER(Connection_type_tcp));
}
|