Exemplo n.º 1
0
Arquivo: eyed.c Projeto: texane/eyed
static cio_err_t on_server_accept(cio_handle_t* h, cio_dispatcher_t* d, void* p)
{
  socket_fd_t new_fd;
  struct sockaddr_in inaddr;
  socklen_t addrlen;
  cio_handle_t* new_handle;
  cio_err_t err;

  DEBUG_ENTER();

  /* new connection
   */

  addrlen = sizeof(struct sockaddr_in);
  new_fd = accept(h->fd, (struct sockaddr*)&inaddr, &addrlen);
  if (socket_fd_is_invalid(new_fd))
    return CIO_ERR_SUCCESS;

  new_handle = malloc(sizeof(cio_handle_t));
  if (new_handle == NULL)
    {
      close_tcp_socket(new_fd);
      return CIO_ERR_SUCCESS;
    }

  err = cio_handle_init(new_handle,
			new_fd,
			&inaddr,
			on_client_read,
			on_client_write,
			on_client_close,
			p);

  if (cio_err_is_failure(err))
    {
      free(new_handle);
      close_tcp_socket(new_fd);
      return CIO_ERR_SUCCESS;
    }

  err = cio_dispatcher_add_handle(d, new_handle);
  if (cio_err_is_failure(err))
    {
      cio_handle_release(new_handle);
      free(new_handle);
      close_tcp_socket(new_fd);
      return CIO_ERR_SUCCESS;
    }

  return CIO_ERR_SUCCESS;
}
void unabto_tcp_fallback_handle_write(nabto_connect* con) {
	ssize_t status;
    int dataToSend;
    unabto_tcp_fallback_connection* fbConn = &fbConns[nabto_connection_index(con)];
    UNABTO_ASSERT(fbConn->sendBufferSent <= fbConn->sendBufferLength); 
    dataToSend = fbConn->sendBufferLength - fbConn->sendBufferSent;
    
    status = send(fbConn->socket, fbConn->sendBuffer + fbConn->sendBufferSent, dataToSend, MSG_NOSIGNAL);
    if (status > 0) {
        fbConn->sendBufferSent += status;
    } else if (status < 0) {
        int err = errno;
        if ((err == EAGAIN) || err == EWOULDBLOCK) {
        } else {
            NABTO_LOG_ERROR((PRI_tcp_fb "Send of tcp packet failed", TCP_FB_ARGS(con)));
            close_tcp_socket(con);
            return; 
        }
    }

    if (fbConn->sendBufferSent > fbConn->sendBufferLength) {
        NABTO_LOG_FATAL(("fbConn->sendBufferSent(%" PRIsize ") > fbConn->sendBufferLength(%" PRIsize "), that should not be possible", fbConn->sendBufferSent, fbConn->sendBufferLength));
    }
    
    if (fbConn->sendBufferSent == fbConn->sendBufferLength) {
        fbConn->sendBufferLength = 0;
    }
}
/**
 * This is called by unabto when the resource is released. All
 * resources must have been released when returning.
 */
bool unabto_tcp_fallback_close(nabto_connect* con) {
    unabto_tcp_fallback_connection* fbConn = &fbConns[nabto_connection_index(con)];

    if (con->tcpFallbackConnectionState != UTFS_CLOSED) {
        close_tcp_socket(con);
    }
    return true;
}
Exemplo n.º 4
0
Arquivo: eyed.c Projeto: texane/eyed
static cio_err_t on_server_close(cio_handle_t* h, cio_dispatcher_t* d, void* p)
{
  DEBUG_ENTER();

  close_tcp_socket(h->fd);

  return CIO_ERR_SUCCESS;
}
Exemplo n.º 5
0
bool unabto_tcp_fallback_handle_write(nabto_connect* con) {
    ssize_t status;
    int dataToSend;
    bool canMaybeSendMoreData = false;
    unabto_tcp_fallback_connection* fbConn = &fbConns[nabto_connection_index(con)];
    UNABTO_ASSERT(fbConn->sendBufferSent <= fbConn->sendBufferLength); 
    dataToSend = fbConn->sendBufferLength - fbConn->sendBufferSent;

    if (dataToSend == 0) {
        return false;
    }
    
    NABTO_LOG_TRACE(("data to send %i, sendBufferLength %i, sendBufferSent %i", dataToSend, fbConn->sendBufferLength, fbConn->sendBufferSent));
    
    status = send(fbConn->socket, fbConn->sendBuffer + fbConn->sendBufferSent, dataToSend, MSG_NOSIGNAL);
    NABTO_LOG_TRACE(("tcp send status: %i", status));
    if (status > 0) {
        fbConn->sendBufferSent += status;
        canMaybeSendMoreData = true;
    } else if (status < 0) {
        int err = errno;
        if ((err == EAGAIN) || err == EWOULDBLOCK) {
            canMaybeSendMoreData = false;
        } else {
            NABTO_LOG_ERROR((PRI_tcp_fb "Send of tcp packet failed", TCP_FB_ARGS(con)));
            close_tcp_socket(con);
            canMaybeSendMoreData = false;
            return canMaybeSendMoreData; 
        }
    }

    if (fbConn->sendBufferSent > fbConn->sendBufferLength) {
        NABTO_LOG_FATAL(("fbConn->sendBufferSent(%" PRIsize ") > fbConn->sendBufferLength(%" PRIsize "), that should not be possible", fbConn->sendBufferSent, fbConn->sendBufferLength));
    }
    
    if (fbConn->sendBufferSent == fbConn->sendBufferLength) {
        fbConn->sendBufferLength = 0;
        fbConn->sendBufferSent = 0;
        canMaybeSendMoreData = false;
    }

    NABTO_LOG_TRACE(("state after send, sendBufferLength %i, sendBufferSent %i", fbConn->sendBufferLength, fbConn->sendBufferSent));
    
    return canMaybeSendMoreData;
}
Exemplo n.º 6
0
/**
 * This is called by unabto when the resource is released. All
 * resources must have been released when returning.
 */
bool unabto_tcp_fallback_close(nabto_connect* con) {
    if (con->tcpFallbackConnectionState != UTFS_CLOSED) {
        close_tcp_socket(con);
    }
    return true;
}
Exemplo n.º 7
0
int 
main (int argc, char *argv[])
{
	int 			sd,res;
	struct sockaddr_in	sin_buf;
	struct sockaddr_in	* sin = &sin_buf;
	char			buffer [BUFSIZ];
	char			dishname [BUFSIZ];
	char 			* ptr;

	isodetailor (argv[0], 1);

	if ((sd = start_tcp_client ((struct sockaddr_in *) 0, 0)) == NOTOK) {
		perror("start_tcp_client");
		exit(-20);
	}

	if (get_dish_sock (sin, 0, 1) != 0)
		exit (-21);

	if (join_tcp_server (sd, sin) == NOTOK) {
		int	pid;
		 close_tcp_socket (sd);

fork_again:
		;
		switch (pid = vfork ()) {
		case 0:
			/* child */
			 close_tcp_socket (sd);
			 strcpy (dishname,
						   _isodefile (isodebinpath, "dish"));
			{
				int i, nfds = getdtablesize ();

				for (i = fileno(stderr) + 1; i < nfds; i++)
					 close (i);
			}
			 execl (dishname, "dish","-pipe",NULLCP);
			 fprintf (stderr, "unable to exec ");
			perror (dishname);
			_exit (-21);
		case -1:
			perror ("fork");
			 close_tcp_socket (sd);
			exit (-22);
		default:
			/* parent */
			for (;;) {
				if ((sd = start_tcp_client ((struct sockaddr_in *) 0,
											0)) == NOTOK) {
					perror("start_tcp_client");
					exit(-23);
				}
				if (join_tcp_server (sd, sin) != NOTOK)
					break;

				/* need to introduce a timeout !!! */
				 close_tcp_socket (sd);

				sleep (5);

				if (kill (pid, 0) == NOTOK) {
					 fprintf (stderr,"Trying again...\n");
					goto fork_again;
				}
			}
			break;
		}
	}

	if ((ptr = rindex (argv[0], '/')) == NULLCP)
		 strcpy (buffer,argv[0]);
	else
		 strcpy (buffer,++ptr);

	argc--,argv++;

	while (argc--) {
		 strcat (buffer, " \"");
		 strcat (buffer, *argv++);
		 strcat (buffer, "\"");
	}
	 strcat (buffer, "\n");

	if (send(sd, buffer, strlen(buffer), 0) == -1) {
		perror("send");
		 close_tcp_socket (sd);
		exit (-25);
	}

	for (;;) {
		if ((res = recv(sd, buffer, BUFSIZ - 1, 0)) == -1) {
err_recv:
			;
			perror ("recv");
			 close_tcp_socket (sd);
			exit (-26);
		}
		*(buffer + res) = 0;
		if (res == 0) {
			 close_tcp_socket (sd);
			exit (0);
		}

		if (*buffer == '2') {
			if (res > 1)
				 write (2,&buffer[1],--res);
			while ( (res = recv(sd, buffer, BUFSIZ, 0)) > 0)
				 write (2,buffer,res);
			 close_tcp_socket (sd);
			exit (1);
		} else if ((*buffer == '1') || (*buffer == '3')) {
			int eval;
			eval = (*buffer == '1' ? 0 : 2);
			if (res > 1)
				 write (1,&buffer[1],--res);
			while ( (res = recv(sd, buffer, BUFSIZ, 0)) > 0)
				 write (1,buffer,res);
			 close_tcp_socket (sd);
			exit (eval);
		} else {		/* 'e', 'y', 'm', or 'p' */
			char  *cp, *ep;
			char            where[BUFSIZ];

			cp = buffer + res - 1;
			ep = buffer + sizeof buffer - 1;
			while (*cp != '\n') {
				++cp;
				switch (res = recv (sd, cp, ep - cp, 0)) {
				case NOTOK:
					goto err_recv;
				case OK:
					 fprintf (stderr,
									"eof reading '%c' directive\n",
									*buffer);
					exit (-28);
				default:
					cp += res - 1;
					if (cp < ep)
						continue;
					 fprintf (stderr,
									"'%c' directive exceeds %d octets\n",
									*buffer, sizeof buffer - 1);
					exit(-29);
				}
			}
			*cp = NULL;

			if (*buffer == 'e') {
				if (system (&buffer[1]))
					 strcpy (where, "e");
				else
					 getcwd (where, sizeof where);
			} else if (*buffer == 'm') {
				 fprintf (stderr, "\n%s\n", buffer + 1);
				 strcpy (where, "m");
			} else if (*buffer == 'y') {
				 fprintf (stderr,"%s",buffer + 1);
				 fgets (where, sizeof where, stdin);
				if (cp = index (where, '\n'))
					*cp = NULL;
			} else {	/* 'p' */
				 sprintf (where,
								"Enter password for \"%s\": ",
								buffer + 1);
				 sprintf (where, "p%s",
								getpassword (where));
			}
			 strcat (where, "\n");

			if (send(sd, where, strlen(where), 0) == -1) {
				perror("send");
				 close_tcp_socket (sd);
				exit (-27);
			}

		}

	}
}