Hi,
try this store.c
and feedback result.

Aiko Berge wrote:

> yes, with telnet I get a connection. But not with indexer.
> 
> I think it's a problem in indexer. Indexer use not the adress in StoredAddr
> but localhost. Any adresses in StoredAddr and indexer will connect to
> 127.0.0.1. If stored run on the same machine and connect not to the adress
> in indexer.conf.



-- 
Maxim Zakharov            http://sochi.net.ru/~maxime/
  Sochi, Russia               http://www.sochi.com/

#include <udm_config.h>

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <sys/types.h>
#ifdef   HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_WINSOCK_H
#include <winsock.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif

#include "udm_store.h"
#include "udm_services.h"
#include "udm_xmalloc.h"

#ifdef HAVE_ZLIB

#ifndef MSG_WAITALL
#define MSG_WAITALL 0
#endif

ssize_t UdmRecvall(int s, void *buf, size_t len, int flags) {
  size_t received = 0, r;
  char *b = buf;
  while ( (received < len) && ((r = recv(s, &b[received], len - received, flags)) >= 0 
) ) {
    received += r;
  }
  return received;
}
#endif


__INDLIB__ int UdmStoreDoc(UDM_ENV *Conf, const char *Doc, size_t DocSize, unsigned 
int rec_id) {

#ifdef HAVE_ZLIB
  const char *hello = "S\0";
  char result[8];
  int s;

  if ((s = UdmStoreOpen(Conf)) < 0) {
    return -1;
  }

  send (s, hello, 1, 0);
  send (s, &rec_id, sizeof(rec_id), 0);
  send (s, &DocSize, sizeof(DocSize), 0);
  send (s, Doc, DocSize, 0);

  if (UdmRecvall(s, result, 6, MSG_WAITALL) < 0) {
    closesocket(s);
    return -1;
  }
  
  closesocket(s);

  if (strncmp(result, "STORED", 6) != 0) {
    return -1;
  }

  return 0;

#else
  return -1;

#endif

}


__INDLIB__ char * UdmUnStoreDoc(UDM_ENV *Conf, unsigned int rec_id, size_t *DS) {

#ifdef HAVE_ZLIB
  const char *hello = "G\0";
  size_t DocSize;
  char *Doc = NULL;
  int s;

  if ((s = UdmStoreOpen(Conf)) < 0) {
    return NULL;
  }

  send (s, hello, 1, 0);
  send (s, &rec_id, sizeof(rec_id), 0);
  if (UdmRecvall(s, &DocSize, sizeof(DocSize), MSG_WAITALL) < 0) {
    closesocket(s);
    return NULL;
  }
  if (DocSize == 0) {
    closesocket(s);
    return NULL;
  }
  if ((Doc = UdmXmalloc(DocSize)) == NULL) {
    closesocket(s);
    return NULL;
  }
  if (UdmRecvall(s, Doc, DocSize, MSG_WAITALL) < 0) {
    closesocket(s);
    return NULL;
  }
  closesocket(s);
  *DS = DocSize;
  return Doc;

#else
  return NULL;
#endif
}

int UdmStoreOpen(UDM_ENV *Conf) {

#ifdef HAVE_ZLIB
  struct hostent *hp;
  struct sockaddr_in server_addr;
  int s, nport = UDM_STORED_PORT;
    char * cport;
    const char * stored_addr;

  if(!Conf->stored_addr){
    return -1;
/*    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);*/
  }
  stored_addr = strdup(Conf->stored_addr);

  if((cport=strchr(stored_addr,':'))){
    *cport='\0';
    nport=atoi(cport+1);
  }

  if ((hp = gethostbyname(stored_addr)) == 0 ) {
    free(stored_addr);
    fprintf(stderr, "StoreOpen ERR gethostbyname: %s\n", hstrerror(h_errno));
    return -1;
  }
    free(stored_addr);
  bzero(&server_addr, sizeof(server_addr));
  memmove(&server_addr.sin_addr, hp->h_addr, (size_t)hp->h_length);
  server_addr.sin_family = hp->h_addrtype;
  server_addr.sin_port = htons((u_short)nport);

  if((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "StoreOpen ERR socket: %s\n", strerror(errno));
    return -1;
  }
  
  if(connect(s, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
    fprintf(stderr, "StoreOpen ERR connect to %s: %s\n", 
inet_ntoa(server_addr.sin_addr), strerror(errno));
    closesocket(s);
    return -1;
  }

  return s;

#else
  return -1;
#endif

}

Reply via email to