Esempio n. 1
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);
			}

		}

	}
}
Esempio n. 2
0
File: main.c Progetto: cthrax/cs457
int main(int argc, char **argv) {
	char* port = NULL;
	char* host = NULL;
	int index;
	int c;
	int test;
	unsigned int msg = 0;
	packet_type type = INVALID_PACKET_TYPE;


	opterr = 0;

	if (argc == 1) {
		fprintf(stderr, "Must specify port, packet type, server, and data\n");
		return 1;
	}

	while ((c = getopt(argc, argv, "p:t:x:s:")) != -1) {
		switch (c) {
			case 'p':
				if (parsePortNumber(LOWER_IP, UPPER_IP, optarg) == NULL) {
					fprintf(stderr, "Invalid port %s. Must use ephemeral or dynamic port within range of 49152-65535 so as to prevent stepping on registered ports.\n", optarg);
				} else {
					port = optarg;
				}
				break;
			case 't':
				//Value ucp or tcp
				type = parsePacketType(optarg);
				break;
			case 'x':
				// Data to send
				test = atoi(optarg);

				if ((*optarg != '0' && test == 0) ||
				    //(test == INT_MAX || test == INT_MIN) || Need to find these consts
				    (test < 0)) {

					fprintf(stderr, "Invalid data. Data must be a positive 32 bit integer.");
					return 1;
				}
				msg = (unsigned int)test;
				break;
			case 's':
				if (gethostbyname(optarg) == NULL) {
					fprintf(stderr, "Invalid hostname.\n");
					return 2;
				}
				host = optarg;
				break;
			case '?':
				if (optopt == 't') {
					fprintf(stderr, "Option -%c requires an argument. (tcp | udp)\n", optopt);
				} else if (optopt == 'p') {
					fprintf(stderr, "Option -%c requires an argument (-p <port number from 1-65535>)\n", optopt);
				} else if (optopt == 's') {
					fprintf(stderr, "Option -%c requires an argument (-s <valid hostname>)\n", optopt);
				} else if (optopt == 'x') {
					fprintf(stderr, "Option -%c requires an argument (-x <some positive 32 bit int>)\n", optopt);
				} else if (isprint(optopt)) {
					fprintf(stderr, "Unknown option `-%c'.\n", optopt);
				} else {
					fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
				}
				return 1;
			default:
				abort();
		}
	}

	for (index = optind; index < argc; index++) {
		printf("Non-option argument %s, ignoring.\n", argv[index]);
	}

	if (type == UDP) {
		start_udp_client(host, port, msg);
	} else if (type == TCP) {
		start_tcp_client(host, port, msg);
	} else {
		fprintf(stderr, "Invalid packet type, must be tcp or udp.");
	}
	return 0;
}