Как правильно получить значение из С в golang?
Пытаюсь сделать конвертор из flow-tools на golang для сбора статистики, но CGO даётся очень тяжело, установил flow-tools-dev для вызова функций. В данный момент имею такой код (ниже) , он только для теcтирования вызова функций, мне необходимо лишь вытащить адреса порты и количество байт, нашёл в сети код на СИ для конвертирования, подскажите как правильно конвертировать из *char s в uint32 как это делается ниже в цикле while() только для Go.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include "ftlib.h"
/* $Id: $ */
#pragma pack(1)
typedef struct
{
unsigned long fromAddr;
unsigned long toAddr;
short int fromIface;
short int toIface;
short int fromPort;
short int toPort;
unsigned long amount;
} bitel;
/* parameters
* dstfile router_ip [dstfile router] sb srcfile [srcfile..]
*/
int
main (int argc, char *argv[])
{
int i = 0;
int fd = 0;
int str_count = 0;
FILE *fp;
FILE *out;
struct ftio ftio;
struct ftprof ftp;
struct fts3rec_offsets fo;
struct ftver ftv;
char *rec;
u_int32 last_time;
u_int32 tm;
bitel b_rec;
if (argc < 3)
{
fprintf (stderr, "Usage: %s out_file in_file1 in_file2 in_file3 ... \n",
argv[0]);
return 1;
}
if ((out = fopen (argv[1], "wb")) == NULL)
{
perror ("Cannot create file %m");
return 2;
}
for (i = 2; i < argc; i++)
{
if ((fp = fopen (argv[i], "rb")) == NULL)
{
perror ("Cannot open file %m");
return 1;
}
fd = fileno (fp);
fterr_setid (argv[0]);
ftprof_start (&ftp);
if (ftio_init (&ftio, fd, FT_IO_FLAG_READ) < 0)
{
perror ("Error in initialization ftio structure:%m");
return 0;
}
ftio_get_ver (&ftio, &ftv);
fts3rec_compute_offsets (&fo, &ftv);
last_time = 0;
while ( (rec = ftio_read (&ftio)) )
{
str_count++;
tm = *((u_int32 *) (rec + fo.unix_secs));
if (last_time != tm)
{
b_rec.fromAddr = 0;
b_rec.toAddr = 0;
b_rec.fromIface = htons (0xff77);
b_rec.toIface = htons (0xff77);
b_rec.fromPort = htons ((u_int16) ((tm >> 16) & 0xFFFF));
b_rec.toPort = htons ((u_int16) (tm & 0xFFFF));
b_rec.amount = 0;
fwrite (&b_rec, sizeof (bitel), 1, out);
last_time = tm;
}
b_rec.fromAddr = htonl (*((u_int32 *) (rec + fo.srcaddr)));
b_rec.toAddr = htonl (*((u_int32 *) (rec + fo.dstaddr)));
b_rec.fromIface = htons (*((u_int16 *) (rec + fo.input)));
b_rec.toIface = htons (*((u_int16 *) (rec + fo.output)));
b_rec.fromPort = htons (*((u_int16 *) (rec + fo.srcport)));
b_rec.toPort = htons (*((u_int16 *) (rec + fo.dstport)));
b_rec.amount = htonl (*((u_int32 *) (rec + fo.dOctets)));
fwrite (&b_rec, sizeof (bitel), 1, out);
}
ftio_close (&ftio);
fclose (fp);
}
fclose (out);
fprintf (stderr, "Total lines: %d\n", str_count);
return 0;
}
package main
/*
#cgo CFLAGS: -I/usr/include
#cgo LDFLAGS: -L/usr/lib -lft -lz
#include "ftlib.h"
void ftio_get_ver(struct ftio *ftio, struct ftver *ver);
int ftio_init(struct ftio *ftio, int fd, int flag);
int fts3rec_compute_offsets(struct fts3rec_offsets *o, struct ftver *ftv);
void *ftio_read(struct ftio *ftio);
*/
import "C"
import (
"fmt"
"log"
"os"
"unsafe"
)
const FT_IO_FLAG_READ = 0x4 /* stream is open for reading */
func main() {
path := "/var/log/netflows/2019/2019-01/2019-01-30/ft-v05.2019-01-30.000500+0400"
ftio := C.struct_ftio{}
ftv := C.struct_ftver{}
fo := C.struct_fts3rec_offsets{}
f, err := os.OpenFile(path, os.O_RDONLY, 0755)
if err != nil {
log.Fatal(err)
}
C.ftio_init(&ftio,C.int(f.Fd()),FT_IO_FLAG_READ)
C.fts3rec_compute_offsets(&fo,&ftv)
for i := 0;i < 5; i++{
rec := C.ftio_read(&ftio)
fmt.Printf("%+v\n",(*C.uint32_t)(*(*unsafe.Pointer)(rec)))
}
}