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