OpenSDN source code
c/sandesh.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 /*
7  * sandesh.h
8  *
9  * Sandesh C Library
10  */
11 
12 #ifndef __SANDESHC_H__
13 #define __SANDESHC_H__
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /* OS specific defines */
20 #ifdef __KERNEL__
21 #if defined(__linux__)
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 
25 #include <linux/types.h>
26 #include <linux/in.h>
27 #include <linux/in6.h>
28 
29 #define OS_LOG_ERR KERN_ERR
30 #define OS_LOG_DEBUG KERN_DEBUG
31 
32 #define os_malloc(size) kmalloc(size, GFP_KERNEL)
33 #define os_zalloc(size) kzalloc(size, GFP_KERNEL)
34 #define os_realloc(ptr, size) krealloc(ptr, size, GFP_KERNEL)
35 #define os_free(ptr) kfree(ptr)
36 #define os_log(level, format, arg...) printk(level format, ##arg)
37 #endif
38 
39 extern int vrouter_dbg;
40 #else /* __KERNEL__ */
41 
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <arpa/inet.h>
46 
47 #include <syslog.h>
48 #include <sys/errno.h>
49 
50 #define OS_LOG_ERR LOG_ERR
51 #define OS_LOG_DEBUG LOG_DEBUG
52 
53 #define os_malloc(size) malloc(size)
54 #define os_zalloc(size) calloc(1, size)
55 #define os_realloc(ptr, size) realloc(ptr, size)
56 #define os_free(ptr) free(ptr)
57 #define os_log(level, format, arg...) syslog(level, format, ##arg)
58 
59 
60 #endif /* __KERNEL__ */
61 
62 typedef unsigned char ct_uuid_t[16];
63 
64 typedef struct ipaddr_s {
65  uint8_t iptype; // AF_INET or AF_INET6
66  union {
67  struct in_addr ipv4;
68  struct in6_addr ipv6;
69  };
71 
72 static inline uint64_t os_get_value64(const uint8_t *data) {
73  uint64_t value = 0;
74  int i = 0;
75  for (; i < 8; ++i) {
76  if (i) value <<= 8;
77  value += *data++;
78  }
79  return value;
80 }
81 
82 static inline void os_put_value64(uint8_t *data, uint64_t value) {
83  int i = 0, offset;
84  for (; i < 8; i++) {
85  offset = (8 - (i + 1)) * 8;
86  *data++ = ((value >> offset) & 0xff);
87  }
88 }
89 
90 #include "thrift.h"
98 
99 typedef struct sandesh_info_s {
100  const char *name;
101  u_int32_t size;
102  int32_t (*read) (void *, ThriftProtocol *, int *);
103  int32_t (*read_binary_from_buffer) (void *, uint8_t *, const size_t, int *);
104  int32_t (*write) (void *, ThriftProtocol *, int *);
105  int32_t (*write_binary_to_buffer) (void *, uint8_t *, const size_t, int *);
106  void (*process) (void *);
107  void (*free) (void *);
109 
110 typedef sandesh_info_t * (*sandesh_find_info_fn) (const char *name);
111 
112 sandesh_info_t * sandesh_find_info(sandesh_info_t *infos, const char *name);
113 int32_t sandesh_decode(u_int8_t *buf, u_int32_t buf_len,
114  sandesh_find_info_fn sinfo_find_fn, int *error);
115 int32_t sandesh_encode(void *sandesh, const char *sname,
116  sandesh_find_info_fn sinfo_find_fn, u_int8_t *buf,
117  u_int32_t buf_len, int *error);
118 int32_t sandesh_get_encoded_length(void *sandesh, const char *sname,
119  sandesh_find_info_fn sinfo_find_fn,
120  int *error);
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif /* __SANDESHC_H__ */
sandesh_info_t * sandesh_find_info(sandesh_info_t *infos, const char *name)
int32_t sandesh_decode(u_int8_t *buf, u_int32_t buf_len, sandesh_find_info_fn sinfo_find_fn, int *error)
int32_t sandesh_get_encoded_length(void *sandesh, const char *sname, sandesh_find_info_fn sinfo_find_fn, int *error)
sandesh_info_t *(* sandesh_find_info_fn)(const char *name)
Definition: c/sandesh.h:110
static uint64_t os_get_value64(const uint8_t *data)
Definition: c/sandesh.h:72
int32_t sandesh_encode(void *sandesh, const char *sname, sandesh_find_info_fn sinfo_find_fn, u_int8_t *buf, u_int32_t buf_len, int *error)
struct sandesh_info_s sandesh_info_t
static void os_put_value64(uint8_t *data, uint64_t value)
Definition: c/sandesh.h:82
unsigned char ct_uuid_t[16]
Definition: c/sandesh.h:62
struct ipaddr_s ipaddr_t
uint8_t iptype
Definition: c/sandesh.h:65
struct in6_addr ipv6
Definition: c/sandesh.h:68
struct in_addr ipv4
Definition: c/sandesh.h:67
const char * name
Definition: c/sandesh.h:100
u_int32_t size
Definition: c/sandesh.h:101
int32_t(* write_binary_to_buffer)(void *, uint8_t *, const size_t, int *)
Definition: c/sandesh.h:105
int32_t(* read)(void *, ThriftProtocol *, int *)
Definition: c/sandesh.h:102
int32_t(* read_binary_from_buffer)(void *, uint8_t *, const size_t, int *)
Definition: c/sandesh.h:103
void(* free)(void *)
Definition: c/sandesh.h:107
int32_t(* write)(void *, ThriftProtocol *, int *)
Definition: c/sandesh.h:104
Implementation of a Thrift FAKE transport.
Implementation of a file based thrift transport.
Implementation of a Thrift memory buffer transport.
Abstract class for Thrift protocol implementations.