Beispiel #1
0
void
try_next_hub(void)
{
	extern server_t *main_server;
	server_t *next;

	if (me.conn || me.crashing)
		return;
	del_sock(NULL);
	if (!main_server) {
		log("HUB: no C: lines in conf defined! nothing to connect to!");
		return;
	}
	if (me.lasthub.name) {
		if ((next = find_server_t(me.lasthub.name)) && (next = next->next)) {
			me.lasthub.name = leetrestrdup(me.lasthub.name, next->name);
			me.lasthub.addr.s_addr = inet_addr(next->host);
			me.lasthub.port = next->port;
			servpass = next->pass;
		} else {
			leetfree(me.lasthub.name, strlen(me.lasthub.name) + 1);
			goto bleh;
		}
	} else { bleh:
		me.lasthub.name = leetstrdup(main_server->name);
		me.lasthub.addr.s_addr = inet_addr(main_server->host);
		me.lasthub.port = main_server->port;
		servpass = main_server->pass;
	}
	log("HUB: autoconnecting to %s on port %d", me.lasthub.name, me.lasthub.port);
	alldcc("HUB: autoconnecting to %s on port %d", me.lasthub.name, me.lasthub.port);
	me.hub = connect_sock(me.lasthub.name, me.lasthub.addr.s_addr, me.lasthub.port, SOCK_HUB);
}
Beispiel #2
0
/* Start a client to add string */
int add_string(char *str, int str_id)
{
  int s, rv;
  struct sockaddr_un remote;

  printf("str: %s, str_id: %d\n", str, str_id);
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
    {
      perror("socket");
      exit(1);
    }

  rv = -1;
  if((connect_sock(s, remote, SOCK_PATH)) == 0)
    {
      if((recv_ready_signal(s)) == 0)
	{
	  if((send_request(s, "add_string", str, str_id)) == 0)
	    {
	      if((recv_add_string_signal(s)) == 0)
		{
		  rv = 0;
		}
	    }
	  
	}
    }
  close(s);
  return rv;
}
int begin_msg(char cmd)
{
   sock = connect_sock(sock);
   if( sock == -1 ) {
      return 0;
   }
   
   if(write(sock,&cmd,sizeof(cmd)) == -1) {
      perror(MSG_ERR_IO);
      return 0;
   }

   return 1;
}
Beispiel #4
0
int TCP_sock_snd(in_addr_t ip, in_port_t port){
	int sock;
	char ipstr[15];
	struct sockaddr_in Local, To;
	inet_ntop(AF_INET, &ip, ipstr,(socklen_t)15); /* conversione a stringa dell'ip per stamparlo */

	sock = get_sock(SOCK_STREAM);
	set_sock(sock);
	name_sock(&Local, htonl(INADDR_ANY), 0);
	bind_sock(sock, &Local);
	name_sock(&To, ip, port);
	connect_sock(sock, &To);

	printf ("Connessione TCP con %s avvenuta\n",ipstr);

	return sock;
}
int ask_dovecot(const char *credentials, long credentialslen)
{
  struct sockaddr_un dovecot_addr;
  char sockbuf[BUFSIZE] = { 0 };

  int dovesock, readn;

  if (0 >= credentialslen || !credentials)
    return 111;

  memset(&dovecot_addr, 0, sizeof(dovecot_addr));
  dovecot_addr.sun_family = AF_UNIX;

  strncpy(dovecot_addr.sun_path, AUTHSOCKET, sizeof(dovecot_addr.sun_path));

  if (0 > (dovesock = connect_sock(&dovecot_addr)))
    return 111;

  readn = read(dovesock, sockbuf, BUFSIZE - 1);
  if ((11 > readn) || (memcmp(sockbuf + readn - 5, "DONE\n", 5))) {
    close(dovesock);
    return 111;
  }

  sprintf(sockbuf,
    "VERSION\t1\t1\nCPID\t%i\nAUTH\t%i\tPLAIN\tservice=imap\tsecured\tresp=",
    getpid(), getpid() + 1);

  write(dovesock, sockbuf, strlen(sockbuf));
  write(dovesock, credentials, credentialslen);
  write(dovesock, "\n", 1);
  readn = read(dovesock, sockbuf, BUFSIZE - 1);
  close(dovesock);

  if ((3 < readn) && (!memcmp(sockbuf, "OK", 2)))
    return 0;

  return 1;
}
void main(int argc, char *argv[])
{
        int c, s;
        char str[LEN + 9];
        WSADATA wdata;
        WORD wvreq;
        wvreq = MAKEWORD(1, 1);
        if (WSAStartup(wvreq, &wdata) < 0) fatal_error("Unable to initialise Winsock.");

        strcpy(str, "USER ");
        c = 5;
        do
        {
                str[c] = 'x';
                c++;
        } while(c < LEN);
        str[LEN + 6] = 0x0d;
        str[LEN + 7] = 0x0a;
        str[LEN + 8] = 0;

        c = 0;
        if (argc < 2)
        {
                printf("usage: smartftp <host> <port>\n");
                exit(1);
        }

        if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) fatal_error("Could not create socket descriptor.");

        printf("Connecting...");
        connect_sock(s, argv[1], atoi(argv[2]));
        printf("done.\n");

        if (send(s, str, strlen(str), 0) == -1) fatal_error("Error sending.");
        Sleep(6000); // Wait six seconds.

        closesocket(s);
}
Beispiel #7
0
/**
 * スレッド処理
 *
 * @return なし
 */
static void *
client_thread(void *arg)
{
    /* スレッドデータ */
    thread_data *dt = (thread_data *)arg;
    int retval = 0;    /* 戻り値 */
    size_t length = 0; /* 長さ */
    ssize_t slen = 0;  /* 送信するバイト数 */
    struct header hd;  /* ヘッダ */

    /* コネクト */
    dt->sock = connect_sock();
    if (dt->sock < 0) {
        outstd("Connect error");
        pthread_exit((void *)EX_CONNECT_ERR);
    }

    pthread_cleanup_push(thread_cleanup, &dt);

    length = strlen((char *)dt->expr) + 1;

    /* データ設定 */
    slen = set_client_data(&dt->sdata,  dt->expr, length);
    if (slen < 0) /* メモリ確保できない */
        pthread_exit((void *)EX_ALLOC_ERR);

    pthread_cleanup_push(thread_memfree, &dt->sdata);

    /* データ送信 */
    retval = send_data(dt->sock, dt->sdata, (size_t *)&slen);

    /* ヘッダ受信 */
    length = sizeof(struct header);
    (void)memset(&hd, 0, length);
    retval = recv_data(dt->sock, &hd, &length);
    if (retval < 0) /* エラー */
        pthread_exit((void *)EX_RECV_ERR);
    dbglog("recv_data: hd=%p, length=%zu", &hd, length);

    length = (size_t)ntohl((uint32_t)hd.length); /* データ長を保持 */

    /* データ受信 */
    dt->answer = (uchar *)recv_data_new(dt->sock, &length);
    if (!dt->answer) /* メモリ確保できない */
        pthread_exit((void *)EX_ALLOC_ERR);

    pthread_cleanup_push(thread_memfree, &dt->answer);

    if (!length) /* 受信エラー */
        pthread_exit((void *)EX_RECV_ERR);
    dbglog("answer=%p, length=%zu", dt->answer, length);

    stdlog("%s", dt->answer);

    retval = strcmp((char *)dt->expected, (char *)dt->answer);
    stdlog("strcmp=%d", retval);
    assert(0 == retval);

    retval = shutdown(dt->sock, SHUT_RDWR);
    if (retval < 0) {
        outstd("shutdown: sock=%d", dt->sock);
        pthread_exit((void *)EXIT_FAILURE);
    }

    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);

    pthread_exit((void *)EX_SUCCESS);
    return (void *)EX_SUCCESS;
}
Beispiel #8
0
                /*****************************************************************************
                 * Perform DNS lookup
                 ****************************************************************************/
                int 
                socket::dnsLookup(const char *host, int port, int mode)
                {
                    struct sockaddr *sa;
                    struct addrinfo hints;
                    struct addrinfo *res;
                    int ret = 0;
                    int err = 0;
                    int sock = socket_val;
                    size_t salen;
                    char buf[128];
                    int socktype;
                    socklen_t len = sizeof(socklen_t);
#ifndef WIN32
                    struct sockaddr_un local;
#endif /* WIN32 */

                    /* Unix domain sockets */
#ifndef WIN32
                    if (host[0] == '/')
                    {
                        properties["unix_socket"] = new boolean(true);

                        local.sun_family = AF_UNIX;
                        strcpy(local.sun_path, host);

                        if (mode)
                        {
                            unlink(local.sun_path);
                            ret = bind_sock(sock, (struct sockaddr*)&local, sizeof(local));
                        }
                        else
                        {
                            ret = connect_sock(sock, (struct sockaddr*)&local, sizeof(local));
                        }
                    }
                    else
                    {
#endif
                        properties["unix_socket"] = new boolean(false);

                        /* AF_INET[6] */
                        memset(&hints, 0, sizeof(hints));
                        sprintf(buf, "%d", port);

                        getsockopt(sock, SOL_SOCKET, SO_TYPE, (char*)&socktype, &len);

                        /* set-up hints structure */
                        hints.ai_family = AF_UNSPEC;
                        hints.ai_socktype = socktype;
                        if (mode) hints.ai_flags = AI_PASSIVE;
                        err = getaddrinfo(host, buf, &hints, &res);
                        if (err)
                        {
                            if (res) freeaddrinfo(res);
                            
                            exceptions::DnsError *exc = exceptions::DnsError::Create(
                                1,
                                new string((char*)gai_strerror(err))
                            );
                            exc->throw_exception();
                        }
                        else 
                        {
                            struct addrinfo *top = res;
                            while (res) {
                                sa = res->ai_addr;
                                salen = res->ai_addrlen;

                                if (mode)
                                {
                                    /* bind() */
                                    ret = bind_sock(sock, sa, salen);
                                    if (ret < 0) goto end_socket;
                                }
                                else
                                {
                                    /* connect() */
                                    ret = connect_sock(sock, sa, salen);
                                    if (ret == 0) goto end_socket;
                                }

                                res = res->ai_next;
                            }
end_socket:
                            freeaddrinfo(top);
                        }
#ifndef WIN32
                    }
#endif
                    return ret;
                }