Example #1
0
int main (int argc, char **argv)
{

    char *fname = NULL;
    char *hostname = NULL;
    char *progname = argv[0];
    int retval = -1;
    int type = -1;
    struct tftp_conn *tc;

    /* Check whether the user wants to put or get a file. */
    while (argc > 0) {

        if (strcmp("-g", argv[0]) == 0) {
            fname = argv[1];
            hostname = argv[2];

            type = TFTP_TYPE_GET;
            break;
        } else if (strcmp("-p", argv[0]) == 0) {
            fname = argv[1];
            hostname = argv[2];

            type = TFTP_TYPE_PUT;
            break;

        }
        argc--;
        argv++;
    }

    /* Print usage message */
    if (!fname || !hostname) {
        fprintf(stderr, "Usage: %s [-g|-p] FILE HOST\n",
                progname);
        return -1;
    }

    /* Connect to the remote server */
    tc = tftp_connect(type, fname, MODE_OCTET, hostname);

    if (!tc) {
        fprintf(stderr, "Failed to connect!\n");
        return -1;
    }

    /* Transfer the file to or from the server */
    retval = tftp_transfer(tc);

    if (retval < 0) {
        fprintf(stderr, "File transfer failed!\n");
    }

    /* We are done. Cleanup our state. */
    tftp_close(tc);

    return retval;
}
Example #2
0
uint8_t udp_packet_demultiplexer(uint8_t* src, uint16_t src_port, uint8_t* dst, uint16_t dst_port, uint8_t* payload, uint16_t len)
{
	uint8_t retVal;
//    printf("== udp packet handler ==\n");
//    printf("== SRC: %02x.%02x.%02x.%02x.%02x.%02x:%d ==\n", src[0], src[1], src[2], src[3], src[4], src[5], src_port);
//    printf("== DST: %02x.%02x.%02x.%02x.%02x.%02x:%d ==\n", dst[0], dst[1], dst[2], dst[3], dst[4], dst[5], dst_port);

	uint8_t different=0;

	//check for address match
	different=memcmp(udp_get_localhost_ip(NULL), dst, IPV4_DESTINATION_LENGTH);
	if(different)
	{
		different=memcmp(udp_get_broadcast_ip(NULL), dst, IPV4_DESTINATION_LENGTH);
	}

	if(!different)
	{
		if(dst_port == 69)
		{
//			printf("tftp negotiate port\n");
			retVal=tftp_negotiate(src, src_port, dst, dst_port, payload, len - 8);
			if(retVal)
				printf("tftp_negotiate(), error=%d\n", retVal);
		}
		else if(dst_port == tftp_transfer_src_port())
		{
//			printf("tftp transfer port\n");
			retVal=tftp_transfer(src, src_port, dst, dst_port, payload, len - 8);
			if(retVal)
				printf("tftp_transfer(), error=%d\n", retVal);
		}
		else if(dst_port == HELLO_WORLD_PORT)
		{
			printf("New neighbour:\nIP = %d.%d.%d.%d\n", src[0], src[1], src[2], src[3]);
		}
		else
		{
			printf("packet received for unassigned port: %d from ", dst_port);
			print_addr_dec(src);
		}
	}
	else
	{
		//nothing to do here yet
		//may be for routing purposes...
		printf("packet for someone else: ");
		print_addr_dec(dst);
		printf("port: %d\n", dst_port);

	}
	return 0;
}