Пример #1
0
static int rpc2_CheckFDs(int (*select_func)(int n, fd_set *r, fd_set *w,
                                            fd_set *e, struct timeval *t),
                         struct timeval *tvp)
{
    fd_set rmask;
    int nfds;

    FD_ZERO(&rmask);

    if (rpc2_v4RequestSocket != -1)
        FD_SET(rpc2_v4RequestSocket, &rmask);

    if (rpc2_v6RequestSocket != -1)
        FD_SET(rpc2_v6RequestSocket, &rmask);

    nfds = rpc2_v4RequestSocket + 1;
    if (rpc2_v6RequestSocket >= nfds)
        nfds = rpc2_v6RequestSocket + 1;

    if (select_func(nfds, &rmask, NULL, NULL, tvp) > 0) {
        if (rpc2_v4RequestSocket != -1 &&
            FD_ISSET(rpc2_v4RequestSocket, &rmask))
            return rpc2_v4RequestSocket;

        if (rpc2_v6RequestSocket != -1 &&
            FD_ISSET(rpc2_v6RequestSocket, &rmask))
            return rpc2_v6RequestSocket;
    }
    return (-1);
}
Пример #2
0
int main(int argc, char *argv[]) {
    int i;

    init();                                 /* Init some variables (like malloc timestamp string, encrypt text string, etc.) */

    check_par(argc, argv);                  /* Check command arguments number */
    open_config(argv);                      /* Open config file and check if it failed */
    open_log(argv);                         /* Open log file and check if it failed */

    get_ipaddr();                           /* Get server IP address */

    create_socket();                        /* Create a socket */
    bind_socket();                          /* Bind the socket */
    listen_socket();                        /* Listen at the socket */

    print_server_info();                    /* Print server information */

    while (TRUE) {                          /* Read until the end of file */
        if (read_flag) {
            if (fscanf(fcfg, "%s", enc_txt) == EOF) {
                finish_flag = 1;
                break;
            } else {
                fscanf(fcfg, "%s", dec_txt);
            }
        }
        read_flag = 0;

        init_select();                      /* Select function */
        if (select_func() == -1) break;

        for (i = 0; i < max_fds + 1; i++) {
            if (FD_ISSET(i, &rfds)) {
                if (i == sockfd) {                              /* If have a new client connect */
                    if (accept_new_cli() == -1) break;          /* Try to accept new client */
                    if (check_connect() == -1) break;           /* Check connect message from client */
                    if (print_client_info() == -1) break;       /* Print the information of client side */
                    store_client_ip();                          /* Store the client ip address */
                    break;
                } else {                                        /* If have new message from client side */
                    client_ip = get_host_by_sockfd(i);          /* Get the client ip address by socket */
                    recv_socket_msg(i, recv_mark);              /* Get the message from socket */
                    handle_client_msg(i);                       /* Handle client message (SUCCESS_MSG, FAILURE_MSG, DISPATCH_MSG, etc.) */
                    break;
                }
            }
            if (main_flag == EXIT_FAILURE) break;
        }
        if (main_flag == EXIT_FAILURE) break;
    }

    remained_cli = ask_clients_quit();                          /* Ask clients quit and count the remain clients number */
    wait_clients_quit();                                        /* Wait for all clients quit */
    quit_server();                                              /* Clean up and quit server, also print the message to log */

    return main_flag;
}
Пример #3
0
/*
  ==============================
  CL_SwitchableMapSelectAtiWhereSwitch

  ==============================
*/
void CL_SwitchableMapSelectAtiWhereSwitch( cl_ati_t *_switch, void (*select_func)(cl_ati_t *_switch, cl_ati_t *switchable ) )
{
	int		i;

	__chkptr( select_func );

	for ( i = 0; i < l_num_switchable_obj; i++ )
	{
		if ( l_switch_id_tbl[i] == _switch->id )
		{
			select_func( _switch, l_switchable_obj_tbl[i] );
		}
	}
}
Пример #4
0
DepthConvert2::DepthConvert2(unsigned width, unsigned height, const PixelFormat &pixel_in, const PixelFormat &pixel_out, CPUClass cpu) :
    m_func{},
    m_f16c{},
    m_pixel_in{ pixel_in.type },
    m_pixel_out{ pixel_out.type },
    m_scale{},
    m_offset{},
    m_width{ width },
    m_height{ height }
{
    if (pixel_out.type != PixelType::HALF && pixel_out.type != PixelType::FLOAT)
        throw zimg::error::InternalError{ "DepthConvert only converts to floating point types" };

    int32_t range = pixel_in.type < PixelType::HALF ? integer_range(pixel_in.depth, pixel_in.fullrange, pixel_in.chroma) : 1;
    int32_t offset = pixel_in.type < PixelType::HALF ? integer_offset(pixel_in.depth, pixel_in.fullrange, pixel_in.chroma) : 0;

    auto impl = select_func(pixel_in, pixel_out, cpu);

    m_func = impl.first;
    m_f16c = impl.second;

    m_scale = (float)(1.0 / range);
    m_offset = (float)(-offset * (1.0 / range));
}
Пример #5
0
            if (_limit_counter > 0)
            {
                size_t limit = std::min(_limit_counter, _limt_per_table);
                std::stringstream sout;
                sout <<"LIMIT "<< limit;
                limit_clause = sout.str();
            }
            else if (_limt_per_table)
            {
                std::stringstream sout;
                sout <<"LIMIT "<< _limt_per_table;
                limit_clause = sout.str();
            }

            _progress = (cntr*1.f)/(schemas_tables.size());
            select_func(schema_name, table_name, pk_columns, select_columns, limit_clause, match_PK);
            _searched_tables++;

            if ((_limit_total > 0) && (_limit_counter <= 0))
                break;
        }
    }
    if (_searched_tables == 0)
        _state = "No tables were searched";
    else
        _state = base::strfmt("Search completed in %i tables", _searched_tables);
    _progress = 1;
    _working = false;
}

DBSearchPanel::DBSearchPanel(bec::GRTManager* grtm): Box(false),
Пример #6
0
static void test_hash_func(const struct hash_func_s *func)
{
	if (!func->supported) {
		g_test_skip("not supported");
		return;
	}

	bool tested = false;

#define t(FUNC, TEXT, DIGEST) \
	if (func->id == G_PASTE(HASH_FUNC_, FUNC)) { \
		select_func(func->id, true); \
		test_hash_func_digest(func->id, TEXT, NULL, DIGEST); \
		tested = true; \
	}

	t(ADLER32,    "", "00000001");
	t(BLAKE2B,    "", "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce");
	t(BLAKE2S,    "", "69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9");
	t(BLAKE2BP,   "", "b5ef811a8038f70b628fa8b294daae7492b1ebe343a80eaabbf1f6ae664dd67b9d90b0120791eab81dc96985f28849f6a305186a85501b405114bfa678df9380");
	t(BLAKE2SP,   "", "dd0e891776933f43c7d032b08a917e25741f8aa9a12c12e1cac8801500f2ca4f");
	t(CRC32,      "", "00000000");
	t(GOST,       "", "ce85b99cc46752fffee35cab9a7b0278abb4c2d2055cff685af4912c49490f8d");
	t(HAVAL128_3, "", "c68f39913f901f3ddf44c707357a7d70");
	t(HAVAL160_3, "", "d353c3ae22a25401d257643836d7231a9a95f953");
	t(HAVAL192_3, "", "e9c48d7903eaf2a91c5b350151efcb175c0fc82de2289a4e");
	t(HAVAL224_3, "", "c5aae9d47bffcaaf84a8c6e7ccacd60a0dd1932be7b1a192b9214b6d");
	t(HAVAL256_3, "", "4f6938531f0bc8991f62da7bbd6f7de3fad44562b8c6f4ebf146d5b4e46f7c17");
	t(MD2,        "", "8350e5a3e24c153df2275c9f80692773");
	t(MD4,        "", "31d6cfe0d16ae931b73c59d7e0c089c0");
	t(MD5,        "", "d41d8cd98f00b204e9800998ecf8427e");
	t(MD6_224,    "", "d2091aa2ad17f38c51ade2697f24cafc3894c617c77ffe10fdc7abcb");
	t(MD6_256,    "", "bca38b24a804aa37d821d31af00f5598230122c5bbfc4c4ad5ed40e4258f04ca");
	t(MD6_384,    "", "b0bafffceebe856c1eff7e1ba2f539693f828b532ebf60ae9c16cbc3499020401b942ac25b310b2227b2954ccacc2f1f");
	t(MD6_512,    "", "6b7f33821a2c060ecdd81aefddea2fd3c4720270e18654f4cb08ece49ccb469f8beeee7c831206bd577f9f2630d9177979203a9489e47e04df4e6deaa0f8e0c0");
	t(RIPEMD128,  "", "cdf26213a150dc3ecb610f18f6b38b46");
	t(RIPEMD160,  "", "9c1185a5c5e9fc54612808977ee8f548b2258d31");
	t(RIPEMD256,  "", "02ba4c4e5f8ecd1877fc52d64d30e37a2d9774fb1e5d026380ae0168e3c5522d");
	t(RIPEMD320,  "", "22d65d5661536cdc75c1fdf5c6de7b41b9f27325ebc61e8557177d705a0ec880151c3a32a00899b8");
	t(SHA1,       "", "da39a3ee5e6b4b0d3255bfef95601890afd80709");
	t(SHA224,     "", "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f");
	t(SHA256,     "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
	t(SHA384,     "", "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b");
	t(SHA512,     "", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
	t(SHA3_224,   "", "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7");
	t(SHA3_256,   "", "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a");
	t(SHA3_384,   "", "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004");
	t(SHA3_512,   "", "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26");
	t(SNEFRU128,  "", "8617f366566a011837f4fb4ba5bedea2");
	t(SNEFRU256,  "", "8617f366566a011837f4fb4ba5bedea2b892f3ed8b894023d16ae344b2be5881");
	t(TIGER192,   "", "24f0130c63ac933216166e76b1bb925ff373de2d49584e7a");
	t(WHIRLPOOL,  "", "19fa61d75522a4669b44e39c1d2e1726c530232130d407f89afee0964997f7a73e83be698b288febcf88e3e03c4f0757ea8964e59b63d93708b138cc42a66eb3");

	t(CRC32,     "The quick brown fox jumps over the lazy dog", "414fa339");
	t(BLAKE2B,   "The quick brown fox jumps over the lazy dog", "a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918");
	t(GOST,      "The quick brown fox jumps over the lazy dog", "77b7fa410c9ac58a25f49bca7d0468c9296529315eaca76bd1a10f376d1f4294");
	t(MD2,       "The quick brown fox jumps over the lazy dog", "03d85a0d629d2c442e987525319fc471");
	t(MD4,       "The quick brown fox jumps over the lazy dog", "1bee69a46ba811185c194762abaeae90");
	t(MD5,       "The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6");
	t(RIPEMD160, "The quick brown fox jumps over the lazy dog", "37f332f68db77bd9d7edd4969571ad671cf9dd3b");
	t(SHA1,      "The quick brown fox jumps over the lazy dog", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
	t(SHA224,    "The quick brown fox jumps over the lazy dog", "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525");
	t(SHA256,    "The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
	t(SHA384,    "The quick brown fox jumps over the lazy dog", "ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1");
	t(SHA512,    "The quick brown fox jumps over the lazy dog", "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6");
	t(SHA3_224,  "The quick brown fox jumps over the lazy dog", "d15dadceaa4d5d7bb3b48f446421d542e08ad8887305e28d58335795");
	t(SHA3_256,  "The quick brown fox jumps over the lazy dog", "69070dda01975c8c120c3aada1b282394e7f032fa9cf32f4cb2259a0897dfc04");
	t(SHA3_384,  "The quick brown fox jumps over the lazy dog", "7063465e08a93bce31cd89d2e3ca8f602498696e253592ed26f07bf7e703cf328581e1471a7ba7ab119b1a9ebdf8be41");
	t(SHA3_512,  "The quick brown fox jumps over the lazy dog", "01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450");
	t(SNEFRU128, "The quick brown fox jumps over the lazy dog", "59d9539d0dd96d635b5bdbd1395bb86c");
	t(SNEFRU256, "The quick brown fox jumps over the lazy dog", "674caa75f9d8fd2089856b95e93a4fb42fa6c8702f8980e11d97a142d76cb358");
	t(WHIRLPOOL, "The quick brown fox jumps over the lazy dog", "b97de512e91e3828b40d2b0fdce9ceb3c4a71f9bea8d88e75c4fa854df36725fd2b52eb6544edcacd6f8beddfea403cb55ae31f03ad62a5ef54e42ee82c3fb35");

	t(RIPEMD128, "message digest", "9e327b3d6e523062afc1132d7df9d1b8");
	t(RIPEMD160, "message digest", "5d0689ef49d2fae572b881b123a85ffa21595f36");
	t(RIPEMD256, "message digest", "87e971759a1ce47a514d5c914c392c9018c7c46bc14465554afcdf54a5070c0e");
	t(RIPEMD320, "message digest", "3a8e28502ed45d422f68844f9dd316e7b98533fa3f2a91d29f84d425c88d6b4eff727df66a7c0197");
	t(WHIRLPOOL, "message digest", "378c84a4126e2dc6e56dcc7458377aac838d00032230f53ce1f5700c0ffb4d3b8421557659ef55c106b4b52ac5a4aaa692ed920052838f3362e86dbd37a8903e");

	t(ADLER32,  "abc", "024d0127");
	t(CRC32,    "abc", "352441c2");
	t(TIGER192, "abc", "f258c1e88414ab2a527ab541ffc5b8bf935f7b951c132951");

#undef t

	if (!tested)
		g_test_incomplete("not implemented");

	select_func(func->id, false);
}
Пример #7
0
static void test_hash_func_hmac(const struct hash_func_s *func)
{
	bool tested = false;

#define t(FUNC, TEXT, HMAC, DIGEST) \
	if (func->id == G_PASTE(HASH_FUNC_, FUNC)) { \
		select_func(func->id, true); \
		test_hash_func_digest(func->id, TEXT, HMAC, DIGEST); \
		tested = true; \
	}

	// RFC 2202
	t(MD5,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"9294727a3638bb1c13f48ef8158bfc9d");
	t(MD5,
		"what do ya want for nothing?",
		"Jefe",
		"750c783e6ab0b503eaa86e310a5db738");
	t(SHA1,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"b617318655057264e28bc0b6fb378c8ef146be00");
	t(SHA1,
		"what do ya want for nothing?",
		"Jefe",
		"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79");

	// RFC 2286
	t(RIPEMD128,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"fbf61f9492aa4bbf81c172e84e0734db");
	t(RIPEMD128,
		"what do ya want for nothing?",
		"Jefe",
		"875f828862b6b334b427c55f9f7ff09b");
	t(RIPEMD160,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668");
	t(RIPEMD160,
		"what do ya want for nothing?",
		"Jefe",
		"dda6c0213a485a9e24f4742064a7f033b43c4069");

	// RFC 4231
	t(SHA224,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22");
	t(SHA224,
		"what do ya want for nothing?",
		"Jefe",
		"a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44");
	t(SHA256,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7");
	t(SHA256,
		"what do ya want for nothing?",
		"Jefe",
		"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");
	t(SHA384,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6");
	t(SHA384,
		"what do ya want for nothing?",
		"Jefe",
		"af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649");
	t(SHA512,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854");
	t(SHA512,
		"what do ya want for nothing?",
		"Jefe",
		"164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737");

	// http://wolfgang-ehrhardt.de/hmac-sha3-testvectors.html
	t(SHA3_224,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"3b16546bbc7be2706a031dcafd56373d9884367641d8c59af3c860f7");
	t(SHA3_224,
		"what do ya want for nothing?",
		"Jefe",
		"7fdb8dd88bd2f60d1b798634ad386811c2cfc85bfaf5d52bbace5e66");
	t(SHA3_256,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"ba85192310dffa96e2a3a40e69774351140bb7185e1202cdcc917589f95e16bb");
	t(SHA3_256,
		"what do ya want for nothing?",
		"Jefe",
		"c7d4072e788877ae3596bbb0da73b887c9171f93095b294ae857fbe2645e1ba5");
	t(SHA3_384,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"68d2dcf7fd4ddd0a2240c8a437305f61fb7334cfb5d0226e1bc27dc10a2e723a20d370b47743130e26ac7e3d532886bd");
	t(SHA3_384,
		"what do ya want for nothing?",
		"Jefe",
		"f1101f8cbf9766fd6764d2ed61903f21ca9b18f57cf3e1a23ca13508a93243ce48c045dc007f26a21b3f5e0e9df4c20a");
	t(SHA3_512,
		"Hi There",
		"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		"eb3fbd4b2eaab8f5c504bd3a41465aacec15770a7cabac531e482f860b5ec7ba47ccb2c6f2afce8f88d22b6dc61380f23a668fd3888bb80537c0a0b86407689e");
	t(SHA3_512,
		"what do ya want for nothing?",
		"Jefe",
		"5a4bfeab6166427c7a3647b747292b8384537cdb89afb3bf5665e4c5e709350b287baec921fd7ca0ee7a0c31d022a95e1fc92ba9d77df883960275beb4e62024");

#undef t

	if (!tested)
		g_test_incomplete("not implemented");

	select_func(func->id, false);
}
Пример #8
0
void wrqAction(int sockID, struct sockaddr_in sockInfo, struct PARAMS *params)
{
	tftp_rwq_hdr rwq;			bzero(&rwq, sizeof(tftp_rwq_hdr));
	tftp_data_hdr data;			bzero(&data, sizeof(tftp_data_hdr));
	char buffer[MAX_BUFFER];	bzero(buffer, MAX_BUFFER);

	int sz = 0;
	int fack = 0;
	int t_out = 0;
	int intents = 5;
	int blocknum = 1;
	socklen_t socklen = sizeof(struct sockaddr_in);

	FILE *pFile = NULL;
	if((pFile = open_file(params->file, "rb", params)) == NULL)
	{
		exit(EXIT_FAILURE);
	}

	
	rwq.opcode = RFC1350_OP_WRQ;
	strcpy(rwq.filename, params->file);
	strcpy(rwq.mode, "octet");

	/*if(params->rexmt != 5)
	{
		strcpy(rwq.tout, "timeout");
		snprintf(rwq.toutv, 4, "%d", params->rexmt);
	}*/

	sz = rwq_serialize(&rwq, buffer);
	sendInfo(sockID, sockInfo, buffer, sz);
	

	do
	{
		sz = 0;
		intents = 5;

		do
		{
			if((t_out = select_func(sockID, params->rexmt)) == 1)
			{
				sz = recvfrom(sockID, (char *) buffer, MAX_BUFFER, 0, (struct sockaddr *) &sockInfo, &socklen);
				switch(ldshort(buffer))
				{
					case RFC1350_OP_ACK:
					{
						tftp_ack_hdr ack;
						ack_deserialize(&ack, buffer);
						if(fack == 0)
						{
							fack++;
							if(ack.num_block != 0)
							{
								exit(EXIT_FAILURE);
								fclose(pFile);
							}
						}

						data.num_block = blocknum++;
						sz = fread(data.data, 1, RFC1350_BLOCKSIZE, pFile);
						sz = data_serialize(&data, buffer, sz);
						sendInfo(sockID, sockInfo, buffer, sz);	

						if(params->verbose)
						{
							printf("S\t %s: ACK [%d#]\n", inet_ntoa(sockInfo.sin_addr), ack.num_block);
							printf("C\t DATA [%d#] [%lu]\n", data.num_block, sz - SHORT_SIZE * 2);
						}
					}break;

					case RFC1350_OP_ERROR:
					{
						tftp_error_hdr error;
						error_deserialize(&error, buffer, sz);
						printf("Error Code: %d\n", error.code);
						printf("Error Text: %s\n", error.message);

					}break;

					default:
					{
						exit(EXIT_FAILURE);
						fclose(pFile);
					}
				}
			}
		}while(t_out == 0 && --intents);
	}while((sz - SHORT_SIZE * 2) == RFC1350_BLOCKSIZE && t_out != -1);

	if(intents == 0)
	{
		printf("Transfer timed out\n");
	}

	fclose(pFile);
}
Пример #9
0
void rwqAction(int sockID, struct sockaddr_in sockInfo, struct PARAMS *params)
{
	unsigned short int bnum = 1;
	unsigned short int sz  = 0;

	tftp_ack_hdr ack;
	tftp_rwq_hdr rwq;
	tftp_data_hdr data;
	char buffer[MAX_BUFFER];
	
	bzero(&data, sizeof(tftp_data_hdr));
	bzero(&rwq, sizeof(tftp_rwq_hdr));

	rwq_deserialize(&rwq, buffer);

	if(mode_transfer(rwq.mode, "octet", params) == -1)
	{
		sendError(sockID, sockInfo, RFC1350_OP_ERROR, RFC1350_ERR_NOTDEF, "Set transfer mode to octet");
		return;
	}

	FILE *pFile = NULL;
	if((pFile = open_file(rwq.filename, "rb", params)) == NULL)
	{
		return;
	}

	if(timeout_option(&rwq, params) == 1)
	{
		sendACKOpt(sockID, sockInfo, "timeout", params->rexmt);
	}

	int t_out = 0;
	int intents = 5;
	
	do
	{
		bzero(buffer, MAX_BUFFER);
		bzero(&data, sizeof(tftp_data_hdr));

		data.opcode = RFC1350_OP_DATA;
		data.num_block = bnum++;

		sz = fread(data.data, 1, RFC1350_BLOCKSIZE, pFile);
		sz = data_serialize(&data, buffer, sz);		
	
		bzero(&ack, sizeof(tftp_ack_hdr));
		intents = 5;
		
		do
		{
			sendInfo(sockID, sockInfo, buffer, sz);			
			if((t_out = select_func(sockID, params->rexmt)) == 1)
			{
				get_ack(&ack, sockID, sockInfo, params);
				ack_serialize(&ack, buffer);
			}
		}while(t_out == 0 && --intents);
	}while((sz - SHORT_SIZE * 2) == RFC1350_BLOCKSIZE && t_out != -1);

	if(intents == 0)
	{
		printf("Transfer timed out\n");
	}

	fclose(pFile);
}
int main(int argc, char *argv[])
{
	/* conbine socket and bind and listen */
	int lisfd;
	lisfd = create_lisfd();
/* choise select or siganl for operation */
#define SELECT_SIGNAL_FLAG	1
#if SELECT_SIGNAL_FLAG
	/* set nonbloc for listen file descriptor */
	int opt;
	opt = fcntl_func(lisfd, F_GETFL, 0);
	fcntl_func(lisfd, F_SETFL, opt | O_NONBLOCK);
	/* set received buffer size */
	int recvbufsize = 1024;
	setsockopt_func(lisfd, SOL_SOCKET, SO_RCVBUF, &recvbufsize, sizeof(recvbufsize));
	/* waiting for connect of client */
	fd_set accset;
	FD_ZERO(&accset);
	FD_SET(lisfd, &accset);
	select_func(lisfd + 1, &accset, NULL, NULL, NULL);
#endif
	/* accept */
	confd = accept(lisfd, NULL, NULL);
/* choise select or siganl for operation */
#if SELECT_SIGNAL_FLAG
	int en = 1;
	signal_func(SIGURG, sig_urg);
	fcntl_func(confd, F_SETOWN, getpid());
	char buf[1024];
	int i = 0;
	for ( ; ; )
	{
		bzero(buf, sizeof(buf));
		if ((recv_func(confd, buf, sizeof(buf), 0)) == 0)
		{
			fprintf(stdout, "client close \n");
			close(confd);
			confd = -1;
			break;
		}
		fprintf(stdout, "%d :normal data buf :%s\n", ++i, buf);
		sleep(1);
	}
#else
	char buf[1024];
	fd_set rset;
	fd_set xset;
	FD_ZERO(&rset);
	FD_ZERO(&xset);
	int i = 0;
	int n = 0;
	for ( ; ; )
	{
		FD_SET(confd, &rset);
		FD_SET(confd, &xset);
		select_func(confd + 1, &rset, NULL, &xset, NULL);
		if (FD_ISSET(confd, &rset))
		{
			bzero(buf, sizeof(buf));
			if ((recv_func(confd, buf, sizeof(buf), 0)) == 0)
			{
				fprintf(stdout, "client close\n");
				close(confd);
				confd = -1;
				break;
			}
			fprintf(stdout, "this select operated %d :buf: %s\n", ++i, buf);
		}
		if (FD_ISSET(confd, &xset))
		{
			bzero(buf, sizeof(buf));
			/* enforce set not in first byte of buffer */
			buf[0] = 'x';
			n = recv_func(confd, buf, sizeof(buf), MSG_OOB);
			/* not urgent data also return 1 */
			fprintf(stdout, "n:%d\n", n);
			/* show recv buf content, already buf[0] rewritten */
			fprintf(stdout, "is urgent data :%s\n", buf);
		}
		sleep(1);
	}
#endif
	close(confd);
	close(lisfd);
	
	
	return 0;
}