static struct packet * sender6_make_packet(struct sender6 *s)/*{{{*/ { libnet_t *ln = s->ln; uint8_t *ipbuf = libnet_getpbuf(ln, s->iptag); size_t iplen = libnet_getpbuf_size(ln, s->iptag); uint8_t *icbuf = libnet_getpbuf(ln, s->icmptag); size_t iclen = libnet_getpbuf_size(ln, s->icmptag); uint8_t *buf = malloc(iplen + iclen); if(!buf) logea(__FILE__, __LINE__, NULL); memcpy(buf, ipbuf, iplen); memcpy(buf+iplen, icbuf, iclen); struct packet *pkt = packet_create_ip(buf, iplen + iclen); free(buf); return pkt; }/*}}}*/
/*- -- net:pbuf(ptag) */ static int lnet_pbuf(lua_State* L) { libnet_t* ud = checkudata(L); int ptag = luaL_checkint(L, 2); /* checkpblock */ const char* pbuf = (const char*)libnet_getpbuf(ud, ptag); size_t pbufsz = libnet_getpbuf_size(ud, ptag); if(!pbuf) return net_error(L, ud); lua_pushlstring(L, pbuf, pbufsz); return 1; }
int main( int argc, char* argv[] ) { int socket; unsigned char data[1280]; int size; libnet_t *libnet_context; char libnet_errmsg_buf[LIBNET_ERRBUF_SIZE]; //socket = sock_afpacket( argv[1] ); socket = sock_udp( argv[2], strtoul(argv[3], NULL, 10 ) ); if ( socket == -1 ) { fprintf(stderr, "socket error\n"); return 1; } // libnetのコンテキスト作成 libnet_context = libnet_init(LIBNET_RAW6_ADV, NULL, libnet_errmsg_buf ); //libnet_context = libnet_init(LIBNET_RAW6_ADV, argv[1], libnet_errmsg_buf ); if ( NULL == libnet_context ) { fprintf(stderr, "%s\n", libnet_errmsg_buf ); close(socket); return 1; } for(;;) { libnet_ptag_t ptag_udp, ptag_ip6; uint8_t *pbuf; uint32_t payload_len; //afpacketからのデータ受信 //size = recv_packet( socket, sizeof(data), data ); //print_ipv6_header( data ); // UDPデータ受信 size = recv( socket, data, sizeof(data), MSG_TRUNC ); printf("data recieve. "); dump_packet( size, data ); putc('\n', stdout); // UDPヘッダ ptag_udp = libnet_build_udp( (uint16_t)(random()%(65535-49152)+49152), // source port(49152 - 65535) 53, // destination port sizeof(header_udp) + size, // length of UDP pakcet 0, // checksum(autofill) data, // payload size, // payload length libnet_context, 0 ); pbuf = libnet_getpbuf( libnet_context, ptag_udp ); payload_len = libnet_getpbuf_size( libnet_context, ptag_udp ); // IPv6ヘッダ ptag_ip6 = libnet_build_ipv6( 0, // Traffic Class 0, // Flow Label //sizeof(header_ipv6) + sizeof(header_udp) + size, // total length of the IP packet sizeof(header_udp) + size, // total length of the IP packet IPPROTO_UDP, // Next header(UDP) 10, // Hop Limit libnet_name2addr6( libnet_context, SRC_IP, LIBNET_DONT_RESOLVE ), // src ip libnet_name2addr6( libnet_context, DST_IP, LIBNET_DONT_RESOLVE ), // dst ip pbuf, // payload payload_len, // Payload Length libnet_context, 0 ); // 送信データ取得 pbuf = libnet_getpbuf( libnet_context, ptag_ip6 ); payload_len = libnet_getpbuf_size( libnet_context, ptag_ip6 ); // チェックサム計算←IPv6は未対応の模様 //libnet_do_checksum( libnet_context, pbuf, IPPROTO_UDP, payload_len ); printf("data sending. "); dump_packet( payload_len, pbuf ); // データ送信 //libnet_write_raw_ipv6( libnet_context, data, size ); libnet_write_raw_ipv6( libnet_context, pbuf, payload_len ); // 作成データ解放 libnet_clear_packet( libnet_context ); } return 0; }