/*
* vim: ai ts=4 sts=4 sw=4 cinoptions=>4 expandtab
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <syslog.h>
#include "list.h"
#include "json.h"
char *_gjson;
char g_decode_json[2048];
int g_decode_index;
static void process_value_dp( json_value* value, int depth );
#define print_log(...) ((void)printf(__VA_ARGS__))
//#define json_printf(...) ((void)__android_log_print(ANDROID_LOG_INFO, "json", __VA_ARGS__))
#define json_printf(...)
#include <stdarg.h>
unsigned char g_debug_buff[256];
int g_debug_buff_index;
#define macdbg_prser Ser_Printf
//#define Ser_WrStr do{ LOGI("%s", buffer); }while(0)
int g_printf_switch = 0x01;
int Ser_Printf (const char *format, ...)
{
unsigned char buffer[80 + 1];
va_list vArgs;
if( g_printf_switch == 0x00 ){
return 1;
}
va_start(vArgs, format);
vsnprintf((char *)buffer, sizeof(buffer), (char const *)format, vArgs);
va_end(vArgs);
//Ser_WrStr;
strcpy( &g_debug_buff[g_debug_buff_index], buffer );
g_debug_buff_index = g_debug_buff_index + strlen(buffer);
return 0;
}
int macdbg_dmphex(const char* buff, int len)
{
int retval = 0;
int x, y, tot, lineoff;
const char* curr;
lineoff = 0;
curr = buff;
tot = 0;
printf( "\ndump addr: %p\n", buff );
for( x = 0; len > x+16; ){
macdbg_prser("0x%08x: ", lineoff);
for( y = 0; y < 16; y++ ){
macdbg_prser("%02x ", (unsigned char)*(curr + y));
}
macdbg_prser(" ");
for( y = 0; y < 16; y++ ){
char c;
c = *(curr + y);
if( c > 31 && c < 127 ){
macdbg_prser("%c", c);
}else{
macdbg_prser("%c", '.');
}
tot++;
}
curr += 16;
x += 16;
lineoff+=16;
syslog( LOG_INFO, "%s", g_debug_buff );
printf("%s\n", g_debug_buff );
memset( &g_debug_buff[0x00], 0x00, sizeof(g_debug_buff) );
g_debug_buff_index = 0x00;
}
//do last line
//Ser_Printf("tot %d.\r\n", tot );
//Ser_Printf("len %d.\r\n", len );
if( tot < len ){
curr = (buff + tot);
macdbg_prser("0x%08x: ", lineoff);
for( y = 0; y < (len - tot); y++ ){
macdbg_prser("%02x ", (unsigned char)*(curr + y));
}
//padding with spaces
//Ser_Printf("(len - tot) %d.\r\n", (len - tot) );
if( (len - tot) < 16 ){
for( y = 0; y < (32 - ((len - tot)*2)); y++ ){
macdbg_prser(" ");
}
}
for( y = 0; y < 16-(len - tot); y++ ){
macdbg_prser(" ");
}
macdbg_prser(" ");
//Ser_Printf("(len - tot) %d.\r\n", (len - tot) );
for( y = 0; y < (len - tot); y++ ){
char c;
c = *(curr + y);
if( c >31 && c < 127 ){
macdbg_prser("%c", c);
}else{
macdbg_prser("%c", '.');
}
}
}
syslog( LOG_INFO, "%s", g_debug_buff );
printf("%s\n", g_debug_buff );
memset( &g_debug_buff[0x00], 0x00, sizeof(g_debug_buff) );
g_debug_buff_index = 0x00;
return retval;
}
int g_syslog_enable = 1;
static void get_depth_shift(int depth, char *_depth)
{
int j = 0;
for (j=0; j < depth; j++) {
//json_printf(" ");
_depth[j] = ' ';
}
_depth[j] = 0;
//json_printf("%s", _depth);
}
static void process_value(json_value* value, int depth);
static void process_object(json_value* value, int depth)
{
int length, x;
if (value == NULL) {
return;
}
length = value->u.object.length;
for (x = 0; x < length; x++) {
char _depth[256] = {0};
//print_depth_shift(depth);
get_depth_shift(depth, _depth);
json_printf("%sobject[%d].name = %s", _depth, x, value->u.object.values[x].name);
process_value(value->u.object.values[x].value, depth+1);
}
}
void add_decode_buff( char *in_str )
{
int buff_len;
buff_len = strlen(in_str);
if( (buff_len+g_decode_index) >= sizeof(g_decode_json) ){
return;
}
memcpy( &g_decode_json[g_decode_index], in_str, buff_len );
g_decode_index = g_decode_index + buff_len;
//printf("g_decode_json = %s\n", g_decode_json);
//printf("g_decode_jsonxcv.\n\n");
}
void init_decode_buff( void )
{
memset( &g_decode_json[0], 0x00, sizeof(g_decode_json) );
g_decode_index = 0x00;
}
void end_of_decode_buff(void)
{
int buff_len;
buff_len = strlen( g_decode_json );
if( buff_len == 1 ){
g_decode_json[1] = '}';
return;
}
if( g_decode_json[buff_len-1] == ',' ){
g_decode_json[buff_len-1] = '}';
}
}
static void process_object_dp(json_value* value, int depth)
{
int length, x;
char tmp_buff[256];
int buff_len;
if( value == NULL ){
return;
}
length = value->u.object.length;
print_log( "object length = %d\n\n", length );
//遍历对象的所有成员 如果成员还是对象 会递归调用
for( x=0; x<length; x++ ){
char _depth[256] = {0};
//print_depth_shift(depth);
get_depth_shift(depth, _depth);
json_printf("%sobject[%d].name = %s\n", _depth, x, value->u.object.values[x].name);
print_log("%sobject[%d].name = %s\n", _depth, x, value->u.object.values[x].name);
memset( tmp_buff, 0x00, sizeof(tmp_buff) );
sprintf( tmp_buff, "\"%s\":", value->u.object.values[x].name );
//printf("tmp_buff = %s\n", tmp_buff);
add_decode_buff( tmp_buff );
//把键值输出
process_value_dp(value->u.object.values[x].value, depth+4);
}
}
static void process_array(json_value* value, int depth)
{
int length, x;
if (value == NULL) {
return;
}
length = value->u.array.length;
json_printf("array");
for (x = 0; x < length; x++) {
process_value(value->u.array.values[x], depth);
}
}
static void process_value(json_value* value, int depth)
{
int j;
char _depth[256] = {0};
if (value == NULL) {
return;
}
if (value->type != json_object) {
//char _depth[256];
get_depth_shift(depth, _depth);
//print_depth_shift(depth);
}
switch (value->type) {
case json_none:
json_printf("%s%s", _depth, "none");
break;
case json_object:
process_object(value, depth+1);
break;
case json_array:
process_array(value, depth+1);
break;
case json_integer:
json_printf("%sint: %d", _depth, value->u.integer);
//printf("int: %10 \n", value->u.integer);
//printf("int: %d \n", value->u.integer);
break;
case json_double:
json_printf("%sdouble: %f", _depth, value->u.dbl);
break;
case json_string:
json_printf("%sstring: %s", _depth, value->u.string.ptr);
break;
case json_boolean:
json_printf("%sbool: %d", _depth, value->u.boolean);
break;
}
}
static void process_value_dp( json_value* value, int depth )
{
int j;
char _depth[256] = {0};
char tmp_buff[2560] = {0x00};
int buff_len;
if( value == NULL ){
return;
}
if( value->type != json_object ){
//char _depth[256];
get_depth_shift(depth, _depth);
//print_depth_shift(depth);
}
//print_log( "value->type = %d\n", value->type );
switch( value->type ) {
case json_none:
json_printf("%s%s", _depth, "none");
break;
case json_object:
process_object_dp(value, depth+1);
break;
case json_array:
process_array(value, depth+1);
break;
case json_integer:
json_printf("%sint: %d", _depth, value->u.integer);
print_log("%sint: %d", _depth, value->u.integer);
memset( tmp_buff, 0x00, sizeof(tmp_buff) );
sprintf( tmp_buff, "%d,", value->u.integer );
add_decode_buff(tmp_buff);
//printf("int: %10 \n", value->u.integer);
//printf("int: %d \n", value->u.integer);
break;
case json_double:
json_printf("%sdouble: %f", _depth, value->u.dbl);
break;
case json_string:
json_printf("%sstring: %s\n", _depth, value->u.string.ptr);
print_log("%sstring: %s\n", _depth, value->u.string.ptr);
memset( tmp_buff, 0x00, sizeof(tmp_buff) );
sprintf( tmp_buff, "\"%s\",", value->u.string.ptr );
add_decode_buff(tmp_buff);
//把相应的value输出
break;
case json_boolean:
json_printf("%sbool: %d", _depth, value->u.boolean);
break;
}
}
static void getStringValue(json_value* value, int depth, char *_value)
{
int j;
char _depth[256] = { 0 };
if (value == NULL) {
return;
}
if (value->type != json_object) {
//char _depth[256];
get_depth_shift(depth, _depth);
//print_depth_shift(depth);
}
switch (value->type) {
case json_none:
json_printf("%s%s", _depth, "none");
break;
case json_object:
process_object(value, depth + 1);
break;
case json_array:
process_array(value, depth + 1);
break;
case json_integer:
//json_printf("%sint: %d", _depth, value->u.integer);
sprintf(_value, "%d", value->u.integer);
break;
case json_double:
//json_printf("%sdouble: %f", _depth, value->u.dbl);
sprintf(_value, "%f", value->u.dbl);
break;
case json_string:
//json_printf("%sstring: %s", _depth, value->u.string.ptr);
sprintf(_value, "%s", value->u.string.ptr);
break;
case json_boolean:
//json_printf("%sbool: %d", _depth, value->u.boolean);
sprintf(_value, "%d", value->u.boolean);
break;
}
}
//处理 获取 键对应的值 即key --> value
static void getStringValue_dp( json_value* value, int depth, char *_value )
{
int j;
char _depth[256] = { 0 };
if (value == NULL) {
return;
}
if (value->type != json_object) {
//char _depth[256];
get_depth_shift(depth, _depth);
//print_depth_shift(depth);
}
//print_log( "\ngetStringValue_dp value->type = %d\n", value->type );
switch (value->type) {
case json_none:
json_printf("%s%s", _depth, "none");
break;
case json_object:
//如果值是对象的话 处理对象
process_object_dp(value, depth + 1);
break;
case json_array:
process_array(value, depth + 1);
break;
case json_integer:
//json_printf("%sint: %d", _depth, value->u.integer);
sprintf(_value, "%d", value->u.integer);
printf( "get integer %s\n", _value );
printf( "get &value->u.integer = %p\n", &value->u.integer );
//printf( "get value = %p\n", value );
//printf( "get &value->parent = %p\n", &value->parent );
//printf( "get &value->type = %p\n", &value->type );
//printf( "get value->parent = %p\n", value->parent);
//printf( "get &value->parent = %p\n", &value->parent);
break;
case json_double:
//json_printf("%sdouble: %f", _depth, value->u.dbl);
sprintf(_value, "%f", value->u.dbl);
break;
case json_string:
//json_printf("%sstring: %s", _depth, value->u.string.ptr);
sprintf(_value, "%s", value->u.string.ptr);
break;
case json_boolean:
//json_printf("%sbool: %d", _depth, value->u.boolean);
sprintf(_value, "%d", value->u.boolean);
break;
}
}
static void getObjectValue(json_value* value, int depth, char *_name, char *_value)
{
int length, x;
if (value == NULL) {
return;
}
length = value->u.object.length;
//print_log("_name = %s", _name);
//print_log("_value = %s", _value);
//print_log("depth = %d", depth);
//print_log("length = %d", length);
for (x = 0; x < length; x++) {
char _depth[256] = { 0 };
//print_depth_shift(depth);
get_depth_shift(depth, _depth);
//json_printf("%sobject[%d].name = %s", _depth, x, value->u.object.values[x].name);
//process_value(value->u.object.values[x].value, depth + 1);
if (strcmp(value->u.object.values[x].name, _name) == 0)
{
//strcpy(_value, value->u.object.values[x].value);
getStringValue(value->u.object.values[x].value, depth + 1, _value);
return;
}
}
}
void debug_dump_json_value( json_value* value )
{
printf( "\ndump value:\n" );
printf("value = %p\n", value);
printf("value->parent = %p\n", value->parent);
printf("value->type = %d\n", value->type);
switch( value->type )
{
case json_none:
break;
case json_object:
break;
case json_array:
break;
case json_integer:
printf("value->u.integer = %d\n", value->u.integer);
break;
case json_double:
printf("value->u.dbl = %f\n", value->u.dbl);
break;
case json_string:
printf("value->u.string.ptr = %s\n", value->u.string.ptr );
break;
case json_boolean:
printf( "value->u.boolean = %d\n", value->u.boolean );
break;
default:
printf( "\ndump dfault xxx:\n" );
break;
}
printf("value->u.object.length = %d\n", value->u.object.length);
printf("value->u.object.values = %p\n", value->u.object.values);
printf("value->u.object.values& = %p\n", &value->u.object.values);
if( value->u.object.values ){
printf("value->u.object.values->name = %p\n", value->u.object.values->name);
printf("value->u.object.values->name = %s\n", value->u.object.values->name);
printf("value->u.object.values->name & = %p\n", &(value->u.object.values->name));
printf("root->u.object.name_length = %d\n", value->u.object.values->name_length);
printf("root->u.object.value = %p\n", value->u.object.values->value);
}
}
static void getObjectValue_dp(json_value* value, int depth, char *_name, char *_value)
{
int length, x;
if( value == NULL ){
return;
}
length = value->u.object.length;
//在这一级有几个对象(平级 同一级别)
//print_log("\n\n_name = %s\n", _name);
//print_log("_value = %s\n", _value);
//print_log("depth = %d\n", depth);
//print_log("length = %d\n", length);
//debug_dump_json_value(value);
//macdbg_dmphex( (const char*) value, 0x100 );
//debug_dump_json_value(value->u.object.values[0].value);
//macdbg_dmphex((const char*)value->u.object.values[0].value,40);
for( x=0; x<length; x++ ){
//遍历本级的所有对象
char _depth[256] = { 0x00 };
//print_depth_shift(depth);
get_depth_shift(depth, _depth);
//json_printf("%sobject[%d].name = %s", _depth, x, value->u.object.values[x].name);
//process_value(value->u.object.values[x].value, depth + 1);
//print_log("%sobject[%d].name = %s\n", _depth, x, value->u.object.values[x].name);
if( strcmp(value->u.object.values[x].name, _name) == 0 ){
//找到了要匹配的键值 --> key
//strcpy(_value, value->u.object.values[x].value);
getStringValue_dp(value->u.object.values[x].value, depth + 1, _value);
//获取相应的值-->value
return;
//找到即可返回了
}
}
}
int getJsonValue(char *_json_str, char *_name, char *_value)
{
//printf("%s\n", _json_str);
//print_buf("--------------------------------\n\n\n\n", NULL, 0);
//print_buf("--------------------------------decode JSON\n\n", NULL, 0);
json_char *json = (json_char*)_json_str;
json_value *value = json_parse(json, strlen(_json_str));
if( value == NULL ){
print_log("Unable to parse data");
return 1;
}
getObjectValue(value, 0, _name, _value);
//print_log("_json_str = %s", _json_str);
//print_log("_name = %s", _name);
//print_log("_value = %s", _value);
json_value_free(value);
return 0;
}
int getJsonValue_dp( char *_json_str, char *_name, char *_value )
{
//printf("%s\n", _json_str);
//print_buf("--------------------------------\n\n\n\n", NULL, 0);
//print_buf("--------------------------------decode JSON\n\n", NULL, 0);
json_char *json = (json_char*)_json_str;
json_value *value = json_parse(json, strlen(_json_str));
//解析json到value
if( value == NULL ){
print_log("Unable to parse data\n");
return 1;
}
getObjectValue_dp(value, 0, _name, _value);
//获取键值为_name的对象
//print_log("_json_str = %s\n", _json_str);
//print_log("_name = %s\n", _name);
//print_log("_value = %s\n", _value);
json_value_free(value);
return 0;
}
int main_json( int argc, char **argv )
{
int i;
unsigned long int *tableA;
int arg[8];
char _value[1024] = {0};
int ret;
//_gjson = "{\"status\":123,\"remark\":\"request success!\",\"data\":\
//{\"id\":251},\"type\":\"\",\"filePath\":\"\"}";
//printf("json = %s\n", _gjson );
//_gjson = "{\"status\": 9123,\"remark\":\"computed finish!\",\
//\"data\":{\"data\":\"[\\\"1122334455\\\"]\",\"result\":\"5566778899\",\"psn\":\"10003047\"},\"type\":\"\",\"filePath\":\"\"}";
_gjson = "{\"stat\":123}";
printf("json = %s\n", _gjson );
init_decode_buff();
add_decode_buff( "{" );
ret = getJsonValue_dp(_gjson, "stat", _value);
if( ret == 0x00 ){
end_of_decode_buff();
//srv_len = strlen(g_decode_json);
}
//printf("data 2= %s\n", _value);
//printf("\nparse result = %s.\n\n", g_decode_json);
}
int mainxxx( int argc, char **argv )
{
int i;
printf("json = %s\n", _gjson );
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip_icmp.h> //struct icmp
#include <netinet/in.h> //sockaddr_in
#include <netinet/ip.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
//校验和计算
unsigned short calc_cksum(char *buff,int len)
{
int blen = len;
unsigned short *mid = (unsigned short*)buff;
unsigned short te = 0;
unsigned int sum = 0;
unsigned short return_sum;
//printf("len = %d\n", len);
//macdbg_dmphex(buff,len);
while(blen > 1)
{
sum += *mid++;
blen -= 2;
}
//数据长度为奇数比如65 上面的while是按16计算的 最后就会剩下一字节不能计算
if(blen == 1)
{
//将多出的一字节放入short类型的高位 低8位置0 加入到sum中
te = *(unsigned char*)mid;
te = (te << 8) & 0xff;
sum += te;
}
sum = (sum >> 16) + (sum&0xffff);
sum += sum >>16;
//printf("sum = %x\n", sum);
return_sum = (unsigned short)(~sum);
printf("return_sum = %x\n", return_sum );
return_sum = return_sum -1+1;
return return_sum;
}
static void icmp_packet(char *buff,int len,int id,int seq)
{
struct timeval *tval = NULL;
struct icmp *icmp = (struct icmp*)buff;
int i;
icmp->icmp_type = 8; //ECHO REQUEST
icmp->icmp_code = 0;
icmp->icmp_cksum = 0; //first set zero
icmp->icmp_id = 0x1234 & 0xffff;
icmp->icmp_seq = 0x5678;
icmp->icmp_data[0] = 0x55;
for( i=0x00; i<54; i++ ){
icmp->icmp_data[i+1] = i+1;
}
icmp->icmp_data[55] = 0xaa;
//tval = (struct timeval *)icmp->icmp_data;
//gettimeofday(tval,NULL);//获得传输时间作为数据
//计算校验和
icmp->icmp_cksum = calc_cksum(buff,len);
icmp->icmp_cksum = icmp->icmp_cksum;
return;
}
#if __BYTE_ORDER == __LITTLE_ENDIAN
// kkkk
#elif __BYTE_ORDER == __BIG_ENDIAN
adfdasfdsf
#endif
void parse_packet(char *buff,int len)
{
struct timeval *val;
struct timeval nv;
struct icmp *icmp;
struct iphdr *iphead = (struct iphdr *)buff;
struct in_addr addr;
addr.s_addr = iphead->saddr;
printf("comefrom ip=%s \n",inet_ntoa(addr));
macdbg_dmphex(buff,len);
printf( "iphead->ihl = %x\n", iphead->ihl );
printf( "iphead->version = %x\n", iphead->version );
printf( "iphead->tos = %x\n", iphead->tos );
printf( "iphead->tot_len = %x\n", iphead->tot_len );
//跳过ip头
icmp = (struct icmp *)(buff+sizeof(struct iphdr));
//看传输回的包校验和是否正确
printf( "lenx = %d\n", len );
printf( "lenx1 = %d\n", sizeof(struct iphdr) );
if(calc_cksum((char *)icmp,len-sizeof(struct iphdr)) != 0x00 ){
printf("receiver error\n");
return;
}
printf( "sizeof(struct iphdr) = %d\n", sizeof(struct iphdr) );
gettimeofday(&nv,NULL);
val = (struct timeval *)icmp->icmp_data;
printf("type=%d seq=%d id=%d pid=%d usec=%d \n",icmp->icmp_type,icmp->icmp_seq,icmp->icmp_id,(getpid()&0xffff),
(nv.tv_usec - val->tv_usec)/1000);
}
/* Untitled2 (2021/4/16 15:32:45)
StartOffset: 00000000, EndOffset: 0000003F, Length: 00000040 */
unsigned char rawData1[64] = {
0x08, 0x00, 0x00, 0x00, 0x18, 0x31, 0x01, 0x00, 0x34, 0x3D, 0x79, 0x60,
0x00, 0x00, 0x00, 0x00, 0x45, 0x42, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
/* Untitled3 (2021/4/16 15:40:57)
StartOffset: 00000000, EndOffset: 00000013, Length: 00000014 */
unsigned char rawData2[20] = {
0x45, 0x00, 0x00, 0x54, 0x79, 0x7B, 0x40, 0x00, 0x40, 0x01, 0x3F, 0x09,
0xC0, 0xA8, 0x00, 0x6B, 0xC0, 0xA8, 0x00, 0x69
};
/* Untitled5 (2021/4/17 10:00:26)
StartOffset: 00000000, EndOffset: 00000053, Length: 00000054 */
unsigned char rawData[84] = {
0x45, 0x00, 0x00, 0x54, 0xD7, 0x7A, 0x40, 0x00, 0x40, 0x01, 0xE1, 0x06,
0xC0, 0xA8, 0x00, 0x6B, 0xC0, 0xA8, 0x00, 0x6C, 0x08, 0x00, 0xFF, 0x10,
0x34, 0x12, 0x78, 0x56, 0x55, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B,
0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0xAA
};
#ifndef do_csum
static inline unsigned short from32to16(unsigned int x)
{
/* add up 16-bit and 16-bit for 16+c bit */
x = (x & 0xffff) + (x >> 16);
/* add up carry.. */
x = (x & 0xffff) + (x >> 16);
return x;
}
static unsigned int do_csum(const unsigned char *buff, int len)
{
int odd;
unsigned int result = 0;
//printk("in do_csum\n");
//dump_stack();
if (len <= 0)
goto out;
odd = 1 & (unsigned long) buff;
if (odd) {
#ifdef __LITTLE_ENDIAN
//dddd
result += (*buff << 8);
#else
result = *buff;
#endif
len--;
buff++;
}
printf("buff = %x\n", buff);
printf("len = %x\n", len);
if (len >= 2) {
if (2 & (unsigned long) buff) {
printf("len1 = %x\n", len);
result += *(unsigned short *) buff;
len -= 2;
buff += 2;
}
if (len >= 4) {
const unsigned char *end = buff + ((unsigned)len & ~3);
unsigned int carry = 0;
do {
unsigned int w = *(unsigned int *) buff;
buff += 4;
result += carry;
result += w;
printf("resultx = %x\n", result);
carry = (w > result);
} while (buff < end);
printf("result = %x\n", result);
result += carry;
printf("result = %x\n", result);
result = (result & 0xffff) + (result >> 16);
printf("result = %x\n", result);
}
if (len & 2) {
result += *(unsigned short *) buff;
buff += 2;
}
}
printf("result = %x\n", result);
if (len & 1)
#ifdef __LITTLE_ENDIAN
result += *buff;
#else
xxx
result += (*buff << 8);
#endif
printf("result = %x\n", result);
result = from32to16(result);
printf("result = %x\n", result);
if (odd)
result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
out:
return result;
}
#endif
int main(int argc,char *argv[])
{
int skfd;
struct sockaddr_in addr={0};
struct sockaddr_in saddr={0};
char buff[68]={0};
char recvbuff[512]={0};
int ret;
int addrlen = 0;
int count = 1;
int i = 1;
unsigned int csum=0x00;
unsigned int sum = 0x00;
unsigned int result = 0x00;
//printf("rawData = %x\n", *(unsigned int *)rawData);
//csum = do_csum(rawData, 0x54);
//printf("csum = %x\n", csum);
//result = csum;
/* add in old sum, and carry.. */
///result += sum;
//if (sum > result)
//result += 1;
//printf("result = %x\n", result);
//calc_cksum(rawData, 20);
//return 0;
skfd = socket(PF_INET,SOCK_RAW,IPPROTO_ICMP);
if(skfd < 0)
{
printf("socket error\n");
return -1;
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("192.168.0.108");
//每一秒发送一次 共发送count次
while(count > 0)
{
//序列号seq 从1 开始传输 buff的大小为64
memset(buff,0,sizeof(buff));
icmp_packet(buff,64,getpid(),i);
i++;
count --;
//将数据发送出去
ret = sendto(skfd,buff,64,0,(struct sockaddr *)&addr,sizeof(addr));
if(ret <= 0)
{
printf("send error\n");
goto out;
}
else
printf("send success ret=%d\n",ret);
macdbg_dmphex(buff,ret);
//接收echo replay
memset(recvbuff,0,sizeof(recvbuff));
memset(&saddr,0,sizeof(saddr));
addrlen = sizeof(saddr);
ret = recvfrom(skfd,recvbuff,sizeof(recvbuff),0,(struct sockaddr *)&saddr,&addrlen);
if(ret <= 0)
{
printf("recv error\n");
goto out;
}
printf("\n\n\n recv success ret=%d\n",ret);
parse_packet(recvbuff,ret);
//sleep(1);
}
out:
close(skfd);
return 0;
}