Hi all,
I've spend a couple of hours beating my head against the wall trying to read a csv back into a struct in my program. For some reason, I cannot get the values, which I wrote to the file read back into my struct correctly.
Ideas?
Struct:
Code:
typedef struct conn_pair {
u_char ether_shost[ETHER_ADDR_LEN]; /**source host address */
u_char ether_dhost[ETHER_ADDR_LEN]; /**destination host address */
struct in_addr ip_src; /**source address */
struct in_addr ip_dst; /**dest address */
u_short th_sport; /**source port */
u_short th_dport; /**destination port */
u_char ip_p; /**protocol */
enum conn_state state; /**connection state */
} CONNECTION;
Offending code:
Code:
if (f == NULL) return -1;
u_char proto = 0;
char ether_shost;
u_char ether_dhost;
char srcIP;
char dstIP;
u_short srcPort = 0;
u_short dstPort = 0;
enum conn_state state = na;
struct ether_addr *tempeth = NULL;
char *rec;
char *token;
char *flds[8];
int i;
rec = malloc(2048);
while (fgets(rec, 2048, f) != NULL) {
CONNECTION temp;
rec[strlen(rec) - 1] = '\0';
i = 0;
token = strtok(rec, ",");
while (token != NULL) {
flds[i] = token;
i++;
token = strtok(NULL, ",");
}
memcpy(&temp.ip_p,flds[0],sizeof(u_char));
memcpy(&temp.ether_shost,(char *)ether_aton((char *)flds[1]),sizeof(u_char));
inet_pton(AF_INET, flds[2], &temp.ip_src);
temp.th_sport = ntohs(flds[3]);
memcpy(temp.ether_dhost,ether_aton((char *)flds[4]),sizeof(u_char[ETHER_ADDR_LEN]));
inet_pton(AF_INET,flds[5],&temp.ip_dst);
temp.th_dport = ntohs(flds[6]);
memcpy(&temp.state,flds[7],sizeof(int));
printf("\n0x%-2x %15s:%-5d -> ", \
temp.ip_p, \
inet_ntoa(temp.ip_src), \
ntohs(temp.th_sport));
printf("%-15s:%-5d %u\n", \
inet_ntoa(temp.ip_dst), \
ntohs(temp.th_dport), \
temp.state \
);
}
Input - don't worry about the MACs.
11,0:15:6d:c4:27:4b,142.232.191.37,68,0:15:6d:c4:2 7:4b,142.232.191.37,67,5
6,0:15:6d:c4:27:4b,74.125.53.125,35330,0:15:6d:c4: 27:4b,74.125.53.125,5222,3
6,1c:c1:de:af:b0:ba,192.168.1.182,443,1c:c1:de:af: b0:ba,192.168.1.182,36577,3
Output:
0x31 142.232.191.37:52131 -> 142.232.191.37 :52166 8245
0x36 74.125.53.125:52129 -> 74.125.53.125 :52166 8243
0x36 192.168.1.182:52130 -> 192.168.1.182 :52166 8243
As you can see, the IP addresses are working correctly, but for the life of me I cannot get the fields: ip_p, th_sport, th_dport and state are not working correctly.
I'm guessing this is for a number of reasons - anyone know?