修訂 | 9aa0fe4571a4ce0f3f3a4309d5b2ad5471a6bf1c (tree) |
---|---|
時間 | 2015-03-15 18:12:49 |
作者 | ornse01 <ornse01@user...> |
Commiter | ornse01 |
implement port number handling.
git-svn-id: http://svn.sourceforge.jp/svnroot/bchan/bchan/trunk@660 20a0b8eb-f62a-4a12-8fe1-b598822500fb
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * cache.c |
3 | 3 | * |
4 | - * Copyright (c) 2009-2012 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -65,6 +65,7 @@ struct datcache_t_ { | ||
65 | 65 | W retrinfo_len; |
66 | 66 | UB *host; |
67 | 67 | W host_len; |
68 | + UH port; | |
68 | 69 | UB *board; |
69 | 70 | W board_len; |
70 | 71 | UB *thread; |
@@ -393,6 +394,11 @@ EXPORT VOID datcache_gethost(datcache_t *cache, UB **host, W *len) | ||
393 | 394 | *len = cache->host_len; |
394 | 395 | } |
395 | 396 | |
397 | +EXPORT VOID datcache_getport(datcache_t *cache, UH *port) | |
398 | +{ | |
399 | + *port = cache->port; | |
400 | +} | |
401 | + | |
396 | 402 | EXPORT VOID datcache_getborad(datcache_t *cache, UB **borad, W *len) |
397 | 403 | { |
398 | 404 | *borad = cache->board; |
@@ -408,11 +414,13 @@ EXPORT VOID datcache_getthread(datcache_t *cache, UB **thread, W *len) | ||
408 | 414 | LOCAL VOID datcache_setupretrinfo(datcache_t *cache, UB *retrinfo, W len) |
409 | 415 | { |
410 | 416 | W i=0; |
417 | + Bool has_port = False; | |
411 | 418 | |
412 | 419 | cache->retrinfo = retrinfo; |
413 | 420 | cache->retrinfo_len = len; |
414 | 421 | cache->host = NULL; |
415 | 422 | cache->host_len = 0; |
423 | + cache->port = 80; | |
416 | 424 | cache->board = NULL; |
417 | 425 | cache->board_len = 0; |
418 | 426 | cache->thread = NULL; |
@@ -427,9 +435,23 @@ LOCAL VOID datcache_setupretrinfo(datcache_t *cache, UB *retrinfo, W len) | ||
427 | 435 | if (cache->retrinfo[i] == '\n') { |
428 | 436 | break; |
429 | 437 | } |
438 | + if (cache->retrinfo[i] == ':') { | |
439 | + has_port = True; | |
440 | + break; | |
441 | + } | |
430 | 442 | cache->host_len++; |
431 | 443 | } |
432 | 444 | |
445 | + if (has_port != False) { | |
446 | + i++; | |
447 | + cache->port = atoi(cache->retrinfo + i); | |
448 | + for (; i < len; i++) { | |
449 | + if (cache->retrinfo[i] == '\n') { | |
450 | + break; | |
451 | + } | |
452 | + } | |
453 | + } | |
454 | + | |
433 | 455 | i++; |
434 | 456 | cache->board = cache->retrinfo + i; |
435 | 457 | for (; i < len; i++) { |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * cache.h |
3 | 3 | * |
4 | - * Copyright (c) 2009-2011 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -54,6 +54,7 @@ IMPORT VOID datcache_cleardata(datcache_t *cache); | ||
54 | 54 | IMPORT VOID datcache_getlatestheader(datcache_t *cache, UB **header, W *len); |
55 | 55 | IMPORT W datcache_updatelatestheader(datcache_t *cache, UB *header, W len); |
56 | 56 | IMPORT VOID datcache_gethost(datcache_t *cache, UB **header, W *len); |
57 | +IMPORT VOID datcache_getport(datcache_t *cache, UH *port); | |
57 | 58 | IMPORT VOID datcache_getborad(datcache_t *cache, UB **borad, W *len); |
58 | 59 | IMPORT VOID datcache_getthread(datcache_t *cache, UB **thread, W *len); |
59 | 60 | IMPORT W datcache_datasize(datcache_t *cache); |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * http.c |
3 | 3 | * |
4 | - * Copyright (c) 2009-2011 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -54,11 +54,12 @@ struct http_t_ { | ||
54 | 54 | http_responsecontext_t *context; |
55 | 55 | }; |
56 | 56 | |
57 | -EXPORT W http_connect(http_t *http, UB *host, W host_len) | |
57 | +EXPORT W http_connect(http_t *http, UB *host, W host_len, UH port) | |
58 | 58 | { |
59 | 59 | W sock, err; |
60 | 60 | B buf[HBUFLEN], *str; |
61 | 61 | HOSTENT ent; |
62 | + struct in_addr addr; | |
62 | 63 | struct sockaddr_in *addr_in; |
63 | 64 | |
64 | 65 | if (http->sockid > 0) { |
@@ -72,25 +73,33 @@ EXPORT W http_connect(http_t *http, UB *host, W host_len) | ||
72 | 73 | strncpy(str, host, host_len); |
73 | 74 | str[host_len] = '\0'; |
74 | 75 | |
75 | - err = so_gethostbyname(str, &ent, buf); | |
76 | - free(str); | |
77 | - if (err < 0) { | |
78 | - return err; | |
76 | + err = inet_aton(str, &addr); | |
77 | + if (err == 0) { | |
78 | + err = so_gethostbyname(str, &ent, buf); | |
79 | + if (err < 0) { | |
80 | + DP_ER("so_gethostbyname error", err); | |
81 | + free(str); | |
82 | + return err; | |
83 | + } | |
84 | + addr.s_addr = *(unsigned int *)(ent.h_addr_list[0]); | |
79 | 85 | } |
86 | + free(str); | |
80 | 87 | |
81 | 88 | addr_in = (struct sockaddr_in *)&(http->addr); |
82 | 89 | addr_in->sin_family = AF_INET; |
83 | - addr_in->sin_port = htons( 80 ); | |
84 | - addr_in->sin_addr.s_addr = *(unsigned int *)(ent.h_addr_list[0]); | |
90 | + addr_in->sin_port = htons(port); | |
91 | + addr_in->sin_addr = addr; | |
85 | 92 | |
86 | 93 | sock = so_socket(AF_INET, SOCK_STREAM, 0); |
87 | 94 | if (sock < 0) { |
95 | + DP_ER("so_socket error", sock); | |
88 | 96 | return sock; |
89 | 97 | } |
90 | 98 | http->sockid = sock; |
91 | 99 | |
92 | 100 | err = so_connect(http->sockid, &(http->addr), sizeof(SOCKADDR)); |
93 | 101 | if (err < 0) { |
102 | + DP_ER("so_connect error", err); | |
94 | 103 | so_close(http->sockid); |
95 | 104 | return err; |
96 | 105 | } |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * http.h |
3 | 3 | * |
4 | - * Copyright (c) 2009 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -34,7 +34,7 @@ typedef struct http_responsecontext_t_ http_responsecontext_t; | ||
34 | 34 | |
35 | 35 | IMPORT http_t* http_new(); |
36 | 36 | IMPORT VOID http_delete(http_t *http); |
37 | -IMPORT W http_connect(http_t *http, UB *host, W host_len); | |
37 | +IMPORT W http_connect(http_t *http, UB *host, W host_len, UH port); | |
38 | 38 | IMPORT W http_send(http_t *http, UB *bin, W len); |
39 | 39 | IMPORT W http_waitresponseheader(http_t *http); |
40 | 40 | IMPORT W http_getstatus(http_t *http); |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * retriever.c |
3 | 3 | * |
4 | - * Copyright (c) 2009-2010 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -52,6 +52,7 @@ struct datretriever_t_ { | ||
52 | 52 | W server_len; |
53 | 53 | W board_len; |
54 | 54 | W thread_len; |
55 | + UH port; | |
55 | 56 | http_t *http; |
56 | 57 | }; |
57 | 58 |
@@ -270,8 +271,9 @@ EXPORT W datretriever_request(datretriever_t *retriever) | ||
270 | 271 | UB *bin, *lm, *et; |
271 | 272 | http_responsecontext_t *ctx; |
272 | 273 | |
273 | - err = http_connect(retriever->http, retriever->server, retriever->server_len); | |
274 | + err = http_connect(retriever->http, retriever->server, retriever->server_len, retriever->port); | |
274 | 275 | if (err < 0) { |
276 | + DP_ER("http_connect error", err); | |
275 | 277 | return err; |
276 | 278 | } |
277 | 279 |
@@ -513,6 +515,8 @@ LOCAL W datretriever_new_prepareinfo(datretriever_t *retriever, datcache_t *cach | ||
513 | 515 | retriever->server[len] = '\0'; |
514 | 516 | retriever->server_len = len; |
515 | 517 | |
518 | + datcache_getport(cache, &retriever->port); | |
519 | + | |
516 | 520 | datcache_getborad(cache, &str, &len); |
517 | 521 | if (str == NULL) { |
518 | 522 | ret = 0; |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * submit.c |
3 | 3 | * |
4 | - * Copyright (c) 2009-2011 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -57,11 +57,13 @@ LOCAL W ressubmit_simplerequest(ressubmit_t *submit, UB *header, W header_len, U | ||
57 | 57 | { |
58 | 58 | UB *host, *r_body = NULL, *bin; |
59 | 59 | W err, host_len, r_len = 0, len; |
60 | + UH port; | |
60 | 61 | http_responsecontext_t *context; |
61 | 62 | |
62 | 63 | datcache_gethost(submit->cache, &host, &host_len); |
64 | + datcache_getport(submit->cache, &port); | |
63 | 65 | |
64 | - err = http_connect(submit->http, host, host_len); | |
66 | + err = http_connect(submit->http, host, host_len, port); | |
65 | 67 | if (err < 0) { |
66 | 68 | DP_ER("error http_connect", err); |
67 | 69 | return err; |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * test_cache.c |
3 | 3 | * |
4 | - * Copyright (c) 2009-2012 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -49,6 +49,7 @@ LOCAL UB test_cache_testdata_04[] = {"aaa.bbb.ccc.jp"}; | ||
49 | 49 | LOCAL UB test_cache_testdata_05[] = {"thread"}; |
50 | 50 | LOCAL UB test_cache_testdata_06[] = {"AAAAAAAAA"}; |
51 | 51 | LOCAL UB test_cache_testdata_07[] = {"XXX abcdef\r\nAAAA: valueC\r\nBBBB: valueC\r\n\r\n"}; |
52 | +LOCAL UB test_cache_testdata_08[] = {"aaa.bbb.ccc.jp:15000\nthread\nAAAAAAAAA\n"}; | |
52 | 53 | |
53 | 54 | LOCAL W test_cache_util_gen_file(LINK *lnk, VID *nvid) |
54 | 55 | { |
@@ -637,6 +638,7 @@ LOCAL UNITTEST_RESULT test_cache_7_testseq(VID vid, LINK *lnk) | ||
637 | 638 | UNITTEST_RESULT result = UNITTEST_RESULT_PASS; |
638 | 639 | UB *host, *borad, *thread; |
639 | 640 | W cmp, host_len, borad_len, thread_len; |
641 | + UH port; | |
640 | 642 | |
641 | 643 | cache = datcache_new(vid); |
642 | 644 |
@@ -651,6 +653,12 @@ LOCAL UNITTEST_RESULT test_cache_7_testseq(VID vid, LINK *lnk) | ||
651 | 653 | result = UNITTEST_RESULT_FAIL; |
652 | 654 | } |
653 | 655 | |
656 | + datcache_getport(cache, &port); | |
657 | + if (port != 80) { | |
658 | + printf("datcache_getport: data error\n"); | |
659 | + result = UNITTEST_RESULT_FAIL; | |
660 | + } | |
661 | + | |
654 | 662 | datcache_getborad(cache, &borad, &borad_len); |
655 | 663 | if (borad_len != strlen(test_cache_testdata_05)) { |
656 | 664 | printf("datcache_getboard: length error\n"); |
@@ -733,6 +741,7 @@ LOCAL UNITTEST_RESULT test_cache_8_testseq(VID vid, LINK *lnk) | ||
733 | 741 | UNITTEST_RESULT result = UNITTEST_RESULT_PASS; |
734 | 742 | UB *host, *borad, *thread; |
735 | 743 | W host_len, borad_len, thread_len; |
744 | + UH port; | |
736 | 745 | |
737 | 746 | cache = datcache_new(vid); |
738 | 747 |
@@ -746,6 +755,12 @@ LOCAL UNITTEST_RESULT test_cache_8_testseq(VID vid, LINK *lnk) | ||
746 | 755 | result = UNITTEST_RESULT_FAIL; |
747 | 756 | } |
748 | 757 | |
758 | + datcache_getport(cache, &port); | |
759 | + if (port != 80) { | |
760 | + printf("datcache_getport: data error\n"); | |
761 | + result = UNITTEST_RESULT_FAIL; | |
762 | + } | |
763 | + | |
749 | 764 | datcache_getborad(cache, &borad, &borad_len); |
750 | 765 | if (borad_len != 0) { |
751 | 766 | printf("datcache_getboard: length error\n"); |
@@ -1396,6 +1411,109 @@ LOCAL UNITTEST_RESULT test_cache_15() | ||
1396 | 1411 | return result; |
1397 | 1412 | } |
1398 | 1413 | |
1414 | +/* test_cache_16 */ | |
1415 | + | |
1416 | +LOCAL UNITTEST_RESULT test_cache_16_testseq(VID vid, LINK *lnk) | |
1417 | +{ | |
1418 | + datcache_t *cache; | |
1419 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
1420 | + UB *host, *borad, *thread; | |
1421 | + W cmp, host_len, borad_len, thread_len; | |
1422 | + UH port; | |
1423 | + | |
1424 | + cache = datcache_new(vid); | |
1425 | + | |
1426 | + datcache_gethost(cache, &host, &host_len); | |
1427 | + if (host_len != strlen(test_cache_testdata_04)) { | |
1428 | + printf("datcache_gethost: length error\n"); | |
1429 | + result = UNITTEST_RESULT_FAIL; | |
1430 | + } | |
1431 | + cmp = memcmp(host, test_cache_testdata_04, host_len); | |
1432 | + if (cmp != 0) { | |
1433 | + printf("datcache_gethost: data error\n"); | |
1434 | + result = UNITTEST_RESULT_FAIL; | |
1435 | + } | |
1436 | + | |
1437 | + datcache_getport(cache, &port); | |
1438 | + if (port != 15000) { | |
1439 | + printf("datcache_getport: data error\n"); | |
1440 | + result = UNITTEST_RESULT_FAIL; | |
1441 | + } | |
1442 | + | |
1443 | + datcache_getborad(cache, &borad, &borad_len); | |
1444 | + if (borad_len != strlen(test_cache_testdata_05)) { | |
1445 | + printf("datcache_getboard: length error\n"); | |
1446 | + result = UNITTEST_RESULT_FAIL; | |
1447 | + } | |
1448 | + cmp = memcmp(borad, test_cache_testdata_05, borad_len); | |
1449 | + if (cmp != 0) { | |
1450 | + printf("datcache_getboard: data error\n"); | |
1451 | + result = UNITTEST_RESULT_FAIL; | |
1452 | + } | |
1453 | + | |
1454 | + datcache_getthread(cache, &thread, &thread_len); | |
1455 | + if (thread_len != strlen(test_cache_testdata_06)) { | |
1456 | + printf("datcache_getthread: length error\n"); | |
1457 | + result = UNITTEST_RESULT_FAIL; | |
1458 | + } | |
1459 | + cmp = memcmp(thread, test_cache_testdata_06, thread_len); | |
1460 | + if (cmp != 0) { | |
1461 | + printf("datcache_getthread: data error\n"); | |
1462 | + result = UNITTEST_RESULT_FAIL; | |
1463 | + } | |
1464 | + | |
1465 | + datcache_delete(cache); | |
1466 | + | |
1467 | + return result; | |
1468 | +} | |
1469 | + | |
1470 | +LOCAL UNITTEST_RESULT test_cache_16() | |
1471 | +{ | |
1472 | + LINK test_lnk; | |
1473 | + W fd, err; | |
1474 | + VID vid; | |
1475 | + UNITTEST_RESULT result; | |
1476 | + | |
1477 | + fd = test_cache_util_gen_file(&test_lnk, &vid); | |
1478 | + if (fd < 0) { | |
1479 | + return UNITTEST_RESULT_FAIL; | |
1480 | + } | |
1481 | + err = ins_rec(fd, test_cache_testdata_01, strlen(test_cache_testdata_01), DATCACHE_RECORDTYPE_MAIN, 0, 0); | |
1482 | + if (err < 0) { | |
1483 | + cls_fil(fd); | |
1484 | + del_fil(NULL, &test_lnk, 0); | |
1485 | + return UNITTEST_RESULT_FAIL; | |
1486 | + } | |
1487 | + err = ins_rec(fd, test_cache_testdata_02, strlen(test_cache_testdata_02), DATCACHE_RECORDTYPE_INFO, DATCACHE_RECORDSUBTYPE_HEADER, 0); | |
1488 | + if (err < 0) { | |
1489 | + cls_fil(fd); | |
1490 | + del_fil(NULL, &test_lnk, 0); | |
1491 | + return UNITTEST_RESULT_FAIL; | |
1492 | + } | |
1493 | + err = ins_rec(fd, test_cache_testdata_08, strlen(test_cache_testdata_08), DATCACHE_RECORDTYPE_INFO, DATCACHE_RECORDSUBTYPE_RETRIEVE, 0); | |
1494 | + if (err < 0) { | |
1495 | + cls_fil(fd); | |
1496 | + del_fil(NULL, &test_lnk, 0); | |
1497 | + return UNITTEST_RESULT_FAIL; | |
1498 | + } | |
1499 | + cls_fil(fd); | |
1500 | + | |
1501 | + result = test_cache_16_testseq(vid, &test_lnk); | |
1502 | + | |
1503 | + err = odel_vob(vid, 0); | |
1504 | + if (err < 0) { | |
1505 | + printf("error odel_vob:%d\n", err >> 16); | |
1506 | + result = UNITTEST_RESULT_FAIL; | |
1507 | + } | |
1508 | + err = del_fil(NULL, &test_lnk, 0); | |
1509 | + if (err < 0) { | |
1510 | + printf("error del_fil:%d\n", err >> 16); | |
1511 | + result = UNITTEST_RESULT_FAIL; | |
1512 | + } | |
1513 | + | |
1514 | + return result; | |
1515 | +} | |
1516 | + | |
1399 | 1517 | /* append and write file test */ |
1400 | 1518 | |
1401 | 1519 | LOCAL W test_cache_writecheck_testseq_appenddata(datcache_t *cache, Bool clear) |
@@ -3616,6 +3734,7 @@ IMPORT VOID test_cache_main(unittest_driver_t *driver) | ||
3616 | 3734 | UNITTEST_DRIVER_REGIST(driver, test_cache_13); |
3617 | 3735 | UNITTEST_DRIVER_REGIST(driver, test_cache_14); |
3618 | 3736 | UNITTEST_DRIVER_REGIST(driver, test_cache_15); |
3737 | + UNITTEST_DRIVER_REGIST(driver, test_cache_16); | |
3619 | 3738 | UNITTEST_DRIVER_REGIST(driver, test_cache_residinfo_1); |
3620 | 3739 | UNITTEST_DRIVER_REGIST(driver, test_cache_residinfo_2); |
3621 | 3740 | UNITTEST_DRIVER_REGIST(driver, test_cache_residinfo_3); |