-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdebug.c
155 lines (146 loc) · 4.46 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2012,2013 Open Cloud Demonstration Experiments Taskforce.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
// Copyright(C) 2012, Open Cloud Demonstration Experiments Taskforce.
//
// debug.c 0.1 <Dec 2012>
//
// print out functions for debug of VEIP.
//
//
#include "veip.h"
//===========================================================
//
//
// Optional functions for debug
//
//
//===========================================================
// list abuf table
//===========================================================
void a_list(FILE * fd, struct abuf * c){
int i = 0; // index
char vp_gw[INET_ADDRSTRLEN],ip[INET_ADDRSTRLEN];
do{
fprintf(fd, "%04d ", i++);
pri_eha(fd, c->eha);
inet_ntop(PF_INET, &c->ip, ip, sizeof(ip));
inet_ntop(PF_INET, &c->target.vp_gw, vp_gw, sizeof(vp_gw));
fprintf(fd, "(%s) via %s \n", ip, vp_gw);
c=c->a_next;
}while(c != NULL);
return;
}
//===========================================================
// Print ethernet hardware address in "ff:ff:ff:ff:ff:ff".
//===========================================================
void pri_eha(FILE * fd, u_char eha[]){
int i;
for(i = 0; i < ETHER_ADDR_LEN; i++){
fprintf(fd,"%02x", eha[i]);
if( i < (ETHER_ADDR_LEN -1)) fprintf(fd,":");
}
return;
}
//===========================================================
// Print ethernet header summary
//===========================================================
void pri_ehdr(FILE * fd, struct ether_header * hdr){
pri_eha(fd, hdr->ether_shost);
fprintf(fd, " > ");
pri_eha(fd, hdr->ether_dhost);
fprintf(fd, " ethertype ");
switch(ntohs(hdr->ether_type)){
case ETHERTYPE_IP:
fprintf(fd,"IPv4(0x%04x)", ntohs(hdr->ether_type));
break;
case ETHERTYPE_IPV6:
fprintf(fd,"IPv6(0x%04x)", ntohs(hdr->ether_type));
break;
case ETHERTYPE_ARP:
fprintf(fd, "ARP(0x%04x)", ntohs(hdr->ether_type));
break;
case ETHERTYPE_VLAN:
fprintf(fd, "VLAN(0x%04x)", ntohs(hdr->ether_type));
break;
default:
fprintf(fd, "0x%04x", ntohs(hdr->ether_type));
break;
}
return;
}
//===========================================================
// Print IP header summary.
//===========================================================
void pri_iphdr(FILE * fd, struct iphdr * hdr){
char src[INET_ADDRSTRLEN],dst[INET_ADDRSTRLEN];
inet_ntop(PF_INET, &hdr->saddr, src, sizeof(src));
inet_ntop(PF_INET, &hdr->daddr, dst, sizeof(dst));
fprintf(fd,
"IP (tos 0x%02x, ttl %d, id %d, frag 0x%04x, proto %d, length: %d)\n%s > %s",
hdr->tos, // type of service
hdr->ttl, // time to live
ntohs(hdr->id),// ID
ntohs(hdr->frag_off), // fragment flag and offset
hdr->protocol, // payload protocol
ntohs(hdr->tot_len), // total length (including IP header)
src, dst);
return;
}
//===========================================================
// Print packet data in Hex
//===========================================================
void pri_x(FILE * fd, char * ptr, int len){
int i;
u_char pbuf[len];
memcpy(pbuf, ptr, len);
for( i = 0; i < len; i++){
if(!(i % 16)){
if( i > 0 ) fprintf(fd, "\n");
fprintf(fd,"%04d ", i);
}
fprintf(fd,"%02x",pbuf[i]);
if(i % 2) fprintf(fd," ");
}
fprintf(fd,"\n");
return;
}
//===========================================================
// logging
//===========================================================
void logging(char f[], char m[]){
FILE * fd;
if((fd = fopen(f, "a")) < 0){
perror("fopen");
return;
}
time_t ctime;
struct tm * tm;
ctime = time(NULL);
tm = (struct tm *)localtime(&ctime);
fprintf(fd, "%d/%02d/%02d %02d:%02d:%02d ",
tm->tm_year + 1900,
tm->tm_mon +1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
fprintf(fd, "%s\n", m);
fclose(fd);
return;
}
/////////////////////////////////////////////
// END of debug.c