|
Lines 38-49
Link Here
|
| 38 |
#include <signal.h> |
38 |
#include <signal.h> |
| 39 |
|
39 |
|
| 40 |
/* From pi-csd */ |
40 |
/* From pi-csd */ |
| 41 |
#include <signal.h> |
|
|
| 42 |
#include <sys/socket.h> |
41 |
#include <sys/socket.h> |
| 43 |
#include <netinet/in.h> |
42 |
#include <netinet/in.h> |
| 44 |
#include <net/if.h> |
43 |
/*#include <net/if.h>*/ |
| 45 |
#include <arpa/inet.h> |
44 |
#include <arpa/inet.h> |
| 46 |
#include <netdb.h> |
45 |
#include <netdb.h> |
|
|
46 |
#include <pi-source.h> |
| 47 |
|
47 |
|
| 48 |
#define LOCK_DIR "/var/lock" |
48 |
#define LOCK_DIR "/var/lock" |
| 49 |
#define LOCK_BINARY 0 |
49 |
#define LOCK_BINARY 0 |
|
Lines 380-533
Link Here
|
| 380 |
|
380 |
|
| 381 |
#ifdef WITH_NETWORK |
381 |
#ifdef WITH_NETWORK |
| 382 |
|
382 |
|
| 383 |
/* |
|
|
| 384 |
* pi-csd.c: Connection Service Daemon, required for accepting |
| 385 |
* logons via NetSync (tm) |
| 386 |
* Copyright (c) 1997, Kenneth Albanowski |
| 387 |
* |
| 388 |
* While this function is useful in pi-csd, it is intended also to |
| 389 |
* be a demonstration of the proper (or improper, if I'm unlucky) techniques |
| 390 |
* to retrieve networking information. |
| 391 |
*/ |
| 392 |
static void |
| 393 |
fetch_host (char *hostname, int hostlen, struct in_addr *address, |
| 394 |
struct in_addr *mask) |
| 395 |
{ |
| 396 |
#if defined (SIOCGIFCONF) && defined (SIOCGIFFLAGS) |
| 397 |
int s, n, i; |
| 398 |
struct ifconf ifc; |
| 399 |
struct ifreq *ifr, ifreqaddr, ifreqmask; |
| 400 |
#endif |
| 401 |
struct hostent *hent; |
| 402 |
|
| 403 |
#ifdef HAVE_GETHOSTNAME |
| 404 |
/* Get host name the easy way */ |
| 405 |
|
| 406 |
gethostname (hostname, hostlen); |
| 407 |
#else |
| 408 |
# ifdef HAVE_UNAME |
| 409 |
struct utsname uts; |
| 410 |
|
| 411 |
if (uname (&uts) == 0) { |
| 412 |
strncpy (hostname, uts.nodename, hostlen - 1); |
| 413 |
hostname[hostlen - 1] = '\0'; |
| 414 |
} |
| 415 |
# endif /*def HAVE_UNAME */ |
| 416 |
#endif /*def HAVE_GETHOSTNAME */ |
| 417 |
|
| 418 |
/* Get host address through DNS */ |
| 419 |
hent = gethostbyname (hostname); |
| 420 |
|
| 421 |
if (hent) { |
| 422 |
while (*hent->h_addr_list) { |
| 423 |
struct in_addr haddr; |
| 424 |
|
| 425 |
memcpy (&haddr, *(hent->h_addr_list++), sizeof (haddr)); |
| 426 |
if (haddr.s_addr != inet_addr ("127.0.0.1")) |
| 427 |
memcpy (address, &haddr, sizeof (haddr)); |
| 428 |
} |
| 429 |
} |
| 430 |
#if defined (SIOCGIFCONF) && defined (SIOCGIFFLAGS) |
| 431 |
s = socket (AF_INET, SOCK_DGRAM, 0); |
| 432 |
|
| 433 |
if (s < 0) |
| 434 |
return; |
| 435 |
|
| 436 |
ifc.ifc_buf = calloc (1024, 1); |
| 437 |
ifc.ifc_len = 1024; |
| 438 |
|
| 439 |
if (ioctl (s, SIOCGIFCONF, (char *) &ifc) < 0) |
| 440 |
goto done; |
| 441 |
|
| 442 |
n = ifc.ifc_len; |
| 443 |
for (i = 0; i < n; i += ifreq_size (*ifr)) { |
| 444 |
struct sockaddr_in *a; |
| 445 |
struct sockaddr_in *b; |
| 446 |
|
| 447 |
ifr = (struct ifreq *) ((caddr_t) ifc.ifc_buf + i); |
| 448 |
a = (struct sockaddr_in *) &ifr->ifr_addr; |
| 449 |
strncpy (ifreqaddr.ifr_name, ifr->ifr_name, sizeof (ifreqaddr.ifr_name)); |
| 450 |
strncpy (ifreqmask.ifr_name, ifr->ifr_name, sizeof (ifreqmask.ifr_name)); |
| 451 |
|
| 452 |
if (ioctl (s, SIOCGIFFLAGS, (char *) &ifreqaddr) < 0) |
| 453 |
continue; |
| 454 |
|
| 455 |
/* Reject loopback device */ |
| 456 |
#ifdef IFF_LOOPBACK |
| 457 |
if (ifreqaddr.ifr_flags & IFF_LOOPBACK) |
| 458 |
continue; |
| 459 |
#endif /*def IFF_LOOPBACK */ |
| 460 |
|
| 461 |
#ifdef IFF_UP |
| 462 |
/* Reject down devices */ |
| 463 |
if (!(ifreqaddr.ifr_flags & IFF_UP)) |
| 464 |
continue; |
| 465 |
#endif /*def IFF_UP */ |
| 466 |
|
| 467 |
if (ifr->ifr_addr.sa_family != AF_INET) |
| 468 |
continue; |
| 469 |
|
| 470 |
/* If it is a point-to-point device, use the dest address */ |
| 471 |
#if defined (IFF_POINTOPOINT) && defined (SIOCGIFDSTADDR) |
| 472 |
if (ifreqaddr.ifr_flags & IFF_POINTOPOINT) { |
| 473 |
if (ioctl (s, SIOCGIFDSTADDR, (char *) &ifreqaddr) < 0) |
| 474 |
break; |
| 475 |
|
| 476 |
a = (struct sockaddr_in *) &ifreqaddr.ifr_dstaddr; |
| 477 |
|
| 478 |
if (address->s_addr == 0) { |
| 479 |
memcpy (address, &a->sin_addr, sizeof (struct in_addr)); |
| 480 |
} |
| 481 |
} |
| 482 |
else |
| 483 |
#endif /*defined (IFF_POINTOPOINT) && defined (SIOCGIFDSTADDR) */ |
| 484 |
/* If it isn't a point-to-point device, use the address */ |
| 485 |
#ifdef SIOCGIFADDR |
| 486 |
{ |
| 487 |
if (ioctl (s, SIOCGIFADDR, (char *) &ifreqaddr) < 0) |
| 488 |
break; |
| 489 |
|
| 490 |
a = (struct sockaddr_in *) &ifreqaddr.ifr_addr; |
| 491 |
|
| 492 |
if (address->s_addr == 0) { |
| 493 |
memcpy (address, &a->sin_addr, sizeof (struct in_addr)); |
| 494 |
} |
| 495 |
} |
| 496 |
#endif /*def SIOCGIFADDR */ |
| 497 |
/* OK, we've got an address */ |
| 498 |
|
| 499 |
/* Compare netmask against the current address and see if it seems to match. */ |
| 500 |
#ifdef SIOCGIFNETMASK |
| 501 |
if (ioctl (s, SIOCGIFNETMASK, (char *) &ifreqmask) < 0) |
| 502 |
break; |
| 503 |
|
| 504 |
/* Is there any system where we need to use ifr_netmask? */ |
| 505 |
#if 1 |
| 506 |
b = (struct sockaddr_in *) &ifreqmask.ifr_addr; |
| 507 |
#else |
| 508 |
b = (struct sockaddr_in *) &ifreqmask.ifr_netmask; |
| 509 |
#endif |
| 510 |
|
| 511 |
if ((mask->s_addr == 0) && (address->s_addr != 0)) { |
| 512 |
if ((b->sin_addr.s_addr & a->sin_addr.s_addr) == |
| 513 |
(b->sin_addr.s_addr & address->s_addr)) { |
| 514 |
memcpy (mask, &b->sin_addr, sizeof (struct in_addr)); |
| 515 |
|
| 516 |
/* OK, we've got a netmask */ |
| 517 |
|
| 518 |
break; |
| 519 |
} |
| 520 |
} |
| 521 |
#endif /*def SIOCGIFNETMASK */ |
| 522 |
|
| 523 |
} |
| 524 |
|
| 525 |
done: |
| 526 |
free (ifc.ifc_buf); |
| 527 |
close (s); |
| 528 |
#endif /*defined (SIOCGIFCONF) && defined (SIOCGIFFLAGS) */ |
| 529 |
} |
| 530 |
|
| 531 |
/* gpilot_network_device_init (GPilotDevice *device) |
383 |
/* gpilot_network_device_init (GPilotDevice *device) |
| 532 |
* pi-csd.c: Connection Service Daemon, required for accepting |
384 |
* pi-csd.c: Connection Service Daemon, required for accepting |
| 533 |
* logons via NetSync (tm) |
385 |
* logons via NetSync (tm) |
|
Lines 537-620
Link Here
|
| 537 |
static gint |
389 |
static gint |
| 538 |
gpilot_network_device_init (GPilotDevice *device) |
390 |
gpilot_network_device_init (GPilotDevice *device) |
| 539 |
{ |
391 |
{ |
| 540 |
/* |
392 |
struct pi_sockaddr addr; |
| 541 |
* Open a file descriptor. device->fd |
|
|
| 542 |
* Check fd. return -1 if fail. |
| 543 |
* device->io = g_io_channel_unix_new (device->fd); |
| 544 |
* g_io_channel_ref (device->io); |
| 545 |
* return 0; |
| 546 |
* |
| 547 |
*/ |
| 548 |
|
393 |
|
| 549 |
/* char hostname[130]; this is device->host */ |
394 |
char pi_net[100]; |
| 550 |
struct in_addr address, netmask; |
|
|
| 551 |
|
395 |
|
| 552 |
|
396 |
memset(pi_net, 0, sizeof(pi_net)); |
| 553 |
/* int sockfd; this is device->fd */ |
397 |
strncpy(pi_net, "net:", 4); |
| 554 |
struct sockaddr_in serv_addr; |
398 |
if (device->ip != NULL && (strnlen(device->ip, 1) > 0)) { |
| 555 |
|
399 |
struct sockaddr_in serv_addr; |
| 556 |
struct hostent *hent; |
400 |
/* Verify the IP address is valid */ |
| 557 |
/* int quiet = 0; It's Always quiet here */ |
401 |
memset(&serv_addr, 0, sizeof(serv_addr)); |
| 558 |
|
402 |
serv_addr.sin_family = AF_INET; |
| 559 |
memset (&address, 0, sizeof (address)); |
403 |
serv_addr.sin_addr.s_addr = inet_addr(device->ip); |
| 560 |
memset (&netmask, 0, sizeof (netmask)); |
404 |
if (serv_addr.sin_addr.s_addr == (in_addr_t)-1) { |
| 561 |
|
405 |
struct hostent *hostent = gethostbyname(device->ip); |
| 562 |
/* hostname[0] = 0; */ |
406 |
|
| 563 |
/* device->host = g_new (gchar, 130); |
407 |
if (!hostent) { |
| 564 |
g_free (device->host); |
408 |
g_warning ("Device [%s]: Bad IP address/hostname: %s", |
| 565 |
*/ |
409 |
device->name, device->ip); |
| 566 |
|
410 |
return -1; |
| 567 |
/* This is used if the hostname is not correct in the conf file |
411 |
} |
| 568 |
I'll move it to the conf file saving later */ |
|
|
| 569 |
fetch_host (device->host, 128, &address, &netmask); |
| 570 |
|
| 571 |
/* device->host I already have from the config file */ |
| 572 |
|
| 573 |
if (inet_aton (device->ip, &address) == 0) /* Invalid */ |
| 574 |
{ |
| 575 |
if ((hent = gethostbyname (device->ip))) { |
| 576 |
memcpy (&address.s_addr, hent->h_addr, sizeof (address)); |
| 577 |
} else { |
| 578 |
g_message ("Invalid ip address '%s'", device->ip); |
| 579 |
return -1; |
| 580 |
} |
412 |
} |
| 581 |
} |
|
|
| 582 |
|
413 |
|
| 583 |
if (inet_aton (device->netmask, &netmask) == 0) { |
414 |
strncat(pi_net, device->ip, sizeof(pi_net) - 2 - strlen(pi_net)); |
| 584 |
g_message ("Invalid netmask '%s'", device->netmask); |
415 |
} else { |
| 585 |
return -1; |
416 |
strncat(pi_net, "any", 3); |
| 586 |
} |
417 |
} |
| 587 |
|
418 |
device->fd = pi_socket (PI_AF_PILOT, PI_SOCK_STREAM, PI_PF_NET); |
| 588 |
/* cannot execute without address and hostname */ |
419 |
if (device->fd < 0) { |
| 589 |
if ((address.s_addr == 0) || (strlen (device->host) == 0)) |
420 |
g_warning ("Device [%s, %s]: Unable to get socket: %s", |
| 590 |
{ |
421 |
device->name, pi_net, strerror(errno)); |
| 591 |
g_message ("Cannot execute without ip address and hostname."); |
|
|
| 592 |
return -1; |
422 |
return -1; |
| 593 |
} |
423 |
} |
| 594 |
|
424 |
|
| 595 |
device->fd = socket (AF_INET, SOCK_DGRAM, 0); |
425 |
strncpy (addr.pi_device,pi_net, strlen(pi_net)); |
| 596 |
if (device->fd < 0) { |
426 |
|
| 597 |
g_message ("Unable to get socket"); |
427 |
if (pi_bind (device->fd, (struct sockaddr*)&addr, sizeof (addr)) < 0) { |
|
|
428 |
g_warning ("Device [%s, %s]: Unable to bind socket", |
| 429 |
device->name, pi_net); |
| 598 |
return -1; |
430 |
return -1; |
| 599 |
} |
431 |
} |
| 600 |
|
432 |
|
| 601 |
memset (&serv_addr, 0, sizeof (serv_addr)); |
433 |
/* Now listen for incoming connections */ |
| 602 |
serv_addr.sin_family = AF_INET; |
434 |
if (pi_listen (device->fd, 1) != 0) { |
| 603 |
serv_addr.sin_addr.s_addr = htonl (INADDR_ANY); |
435 |
g_warning ("Device [%s, %s]: Error from listen: %s", |
| 604 |
serv_addr.sin_port = htons (14237); |
436 |
device->name, pi_net, strerror (errno)); |
| 605 |
|
437 |
close(device->fd); |
| 606 |
if (bind (device->fd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) { |
|
|
| 607 |
g_message ("Unable to bind socket"); |
| 608 |
return -1; |
438 |
return -1; |
| 609 |
} |
439 |
} |
| 610 |
/* |
|
|
| 611 |
g_message ("Connection Service Daemon for Palm Computing (tm) device active."); |
| 612 |
g_message ("Accepting connection requests for '%s' at %s with mask %s", |
| 613 |
device->host, inet_ntoa (address), inet_ntoa (netmask)); |
| 614 |
*/ |
| 615 |
|
440 |
|
| 616 |
/* We don't want to infinite loops here */ |
441 |
/* Register an interest in the socket, to get events when devices connect */ |
| 617 |
/* Lets try something else instead */ |
|
|
| 618 |
device->io = g_io_channel_unix_new (device->fd); |
442 |
device->io = g_io_channel_unix_new (device->fd); |
| 619 |
g_io_channel_ref (device->io); |
443 |
g_io_channel_ref (device->io); |
| 620 |
|
444 |
|
|
Lines 630-642
Link Here
|
| 630 |
} |
454 |
} |
| 631 |
|
455 |
|
| 632 |
g_free(device->ip); |
456 |
g_free(device->ip); |
| 633 |
g_free(device->host); |
|
|
| 634 |
g_free(device->netmask); |
| 635 |
} |
457 |
} |
| 636 |
|
458 |
|
| 637 |
static void |
459 |
static void |
| 638 |
gpilot_network_device_deinit (GPilotDevice *device) |
460 |
gpilot_network_device_deinit (GPilotDevice *device) |
| 639 |
{ |
461 |
{ |
|
|
462 |
if (device->io) { |
| 463 |
g_source_remove (device->in_handle); |
| 464 |
g_source_remove (device->err_handle); |
| 465 |
g_io_channel_close (device->io); |
| 466 |
g_io_channel_unref (device->io); |
| 467 |
} |
| 640 |
} |
468 |
} |
| 641 |
#endif /* WITH_NETWORK */ |
469 |
#endif /* WITH_NETWORK */ |
| 642 |
|
470 |
|
|
Lines 680-687
Link Here
|
| 680 |
gpilot_network_device_load (GPilotDevice *device) |
508 |
gpilot_network_device_load (GPilotDevice *device) |
| 681 |
{ |
509 |
{ |
| 682 |
device->ip = gnome_config_get_string ("ip"); |
510 |
device->ip = gnome_config_get_string ("ip"); |
| 683 |
device->host = gnome_config_get_string ("host"); |
|
|
| 684 |
device->netmask = gnome_config_get_string ("netmask"); |
| 685 |
} |
511 |
} |
| 686 |
|
512 |
|
| 687 |
gint |
513 |
gint |