HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
usocutils.c
Go to the documentation of this file.
1 #include <string.h>
2 
3 #include <hipc/usoc.h>
4 #include <hipc/usocutils.h>
5 
13 enum HIPC_errno
14 hipcUsocWriteMsgd(struct HIPC_usoc *const so_p,
15  hipc_msgd const *const md_p)
16 {
17  enum HIPC_errno retval;
18 
19  retval = hipcUsocWrite(so_p, hipcMsgdHdrPtr(md_p), HIPC_MSGHDR_SIZE);
20  if (HIPC_SUCCESS != retval) {
21  return retval;
22  }
23  retval =
24  hipcUsocWrite(so_p, hipcMsgdBdyPtr(md_p), hipcMsgdBdySize(md_p));
25  if (HIPC_SUCCESS != retval) {
26  return retval;
27  }
28 
29  return HIPC_SUCCESS;
30 }
31 
39 static enum HIPC_errno
40 hipcUsocSendSysmsg(struct HIPC_usoc *const so_p,
41  const struct HIPC_scfg *const cfg_p)
42 {
43  enum HIPC_errno retval;
44  unsigned int i;
45 
46  for (i = 0; i < (cfg_p)->n_sysmsg; ++i) {
47  retval = hipcUsocWrite(so_p,
48  (cfg_p)->sysmsg_p[i].ptr,
49  (cfg_p)->sysmsg_p[i].size);
50  if (HIPC_SUCCESS != retval) {
51  return retval;
52  }
53  }
54 
55  return HIPC_SUCCESS;
56 }
57 
70 enum HIPC_errno
71 hipcUsocAccept(struct HIPC_usoc *const so_p,
72  const struct HIPC_scfg * *const cfg_pp,
73  const struct HIPC_scfg *const *const cfg_ary,
74  const unsigned int n_cfg,
75  unsigned char *const buf, const size_t buf_size)
76 {
77  enum HIPC_errno retval;
78  unsigned char body_size;
79  unsigned int i;
80 
81  *cfg_pp = NULL;
82 
83  if (NULL == cfg_ary) {
84  return HIPC_ERR_S_INV_NULL;
85  }
86 
87  /* receive cfgid */
88  if (HIPC_MSGHDR_SIZE > buf_size) {
89  return HIPC_ERR_S_BUF_SHTG;
90  }
91  retval = hipcUsocRead(so_p, buf, HIPC_MSGHDR_SIZE);
92  if (HIPC_SUCCESS != retval) {
93  return retval;
94  }
95  body_size = hipcMsghdrBdysize(buf);
96  if ((HIPC_MSGHDR_SIZE + (size_t) body_size) > buf_size) {
97  return HIPC_ERR_S_BUF_SHTG;
98  }
99  retval = hipcUsocRead(so_p, buf + HIPC_MSGHDR_SIZE, body_size);
100  if (HIPC_SUCCESS != retval) {
101  return retval;
102  }
103 
104  /* find cfgid */
105  for (i = 0; i < n_cfg; ++i) {
106  if (NULL == cfg_ary[i]) {
107  return HIPC_ERR_S_INV_NULL;
108  }
109  if (0 != memcmp(cfg_ary[i]->cfgid_header, buf, HIPC_MSGHDR_SIZE)) {
110  continue;
111  }
112  if (0 ==
113  memcmp(cfg_ary[i]->cfgid, buf + HIPC_MSGHDR_SIZE, body_size)) {
114  *cfg_pp = cfg_ary[i];
115  break;
116  }
117  }
118  if (NULL == *cfg_pp) {
119  return HIPC_ERR_S_INV_CFG;
120  }
121 
122  hipcScfgInit(*cfg_pp);
123  retval = hipcUsocSendSysmsg(so_p, *cfg_pp);
124  if (HIPC_SUCCESS != retval) {
125  return retval;
126  }
127 
128  return hipcScfgCheckStrusize(*cfg_pp);
129 }
130 
140 enum HIPC_errno
141 hipcUsocAcceptSimple(struct HIPC_usoc *const so_p,
142  const struct HIPC_scfg *const cfg_p)
143 {
144  enum HIPC_errno retval;
145  unsigned char body_size;
146  unsigned char c;
147  unsigned char i;
148 
149  if (NULL == cfg_p) {
150  return HIPC_ERR_S_INV_NULL;
151  }
152 
153  for (i = 0; i < HIPC_MSGHDR_SIZE; ++i) {
154  retval = hipcUsocRead(so_p, &c, 1);
155  if (HIPC_SUCCESS != retval) {
156  return retval;
157  }
158  if (cfg_p->cfgid_header[i] != c) {
159  return HIPC_ERR_S_INV_CFG;
160  }
161  }
162 
163  body_size = hipcMsghdrBdysize(cfg_p->cfgid_header);
164  for (i = 0; i < body_size; ++i) {
165  retval = hipcUsocRead(so_p, &c, 1);
166  if (HIPC_SUCCESS != retval) {
167  return retval;
168  }
169  if (cfg_p->cfgid[i] != c) {
170  return HIPC_ERR_S_INV_CFG;
171  }
172  }
173 
174  hipcScfgInit(cfg_p);
175  retval = hipcUsocSendSysmsg(so_p, cfg_p);
176  if (HIPC_SUCCESS != retval) {
177  return retval;
178  }
179 
180  return hipcScfgCheckStrusize(cfg_p);
181 }
enum HIPC_errno hipcUsocRead(struct HIPC_usoc *so_p, void *buf, const size_t size)
Reads exactly size bytes from so_p.
Definition: posix_usoc.c:24
unsigned char cfgid_header[HIPC_MSGHDR_SIZE]
Definition: scfg.h:10
void hipcScfgInit(const struct HIPC_scfg *const cfg_p)
Initializes a server-side configuration.
Definition: scfg.c:8
enum HIPC_errno hipcUsocWrite(struct HIPC_usoc *so_p, const void *buf, const size_t size)
Writes exactly size bytes to so_p.
Definition: posix_usoc.c:36
HIPC_errno
Definition: hipc.h:4
enum HIPC_errno hipcUsocWriteMsgd(struct HIPC_usoc *const so_p, hipc_msgd const *const md_p)
Writes md_p to so_p.
Definition: usocutils.c:14
unsigned char hipcMsgdBdySize(hipc_msgd const *const md_p)
Returns body size of a HIPC message data.
Definition: msgd.c:9
enum HIPC_errno hipcUsocAcceptSimple(struct HIPC_usoc *const so_p, const struct HIPC_scfg *const cfg_p)
Accepts a HIPC session of cfg_p.
Definition: usocutils.c:141
enum HIPC_errno hipcUsocAccept(struct HIPC_usoc *const so_p, const struct HIPC_scfg **const cfg_pp, const struct HIPC_scfg *const *const cfg_ary, const unsigned int n_cfg, unsigned char *const buf, const size_t buf_size)
Accepts a HIPC session if cfg_ary includes a configuration of coming HELLO message.
Definition: usocutils.c:71
const unsigned char * cfgid
Definition: scfg.h:11
Definition: usoc.h:4
unsigned char hipcMsghdrBdysize(unsigned char const *const msghdr)
Returns message size determined by a message header.
Definition: hipc.c:12
Definition: scfg.h:9
#define hipcMsgdBdyPtr(md_p)
Returns a pointer to a body of a HIPC message data.
Definition: msgd.h:52
#define hipcMsgdHdrPtr(md_p)
Returns a pointer to a header of a HIPC message data.
Definition: msgd.h:44
enum HIPC_errno hipcScfgCheckStrusize(const struct HIPC_scfg *const cfg_p)
Checks structure sizes.
Definition: scfg.c:20