nmsg  0.9.0
output_pres.c
1 /*
2  * Copyright (c) 2008-2012 by Farsight Security, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* Import. */
18 
19 #include "private.h"
20 
21 /* Internal functions. */
22 
24 _output_pres_write(nmsg_output_t output, nmsg_message_t msg) {
25  Nmsg__NmsgPayload *np;
26  const char *vname = NULL;
27  const char *mname = NULL;
28  char *pres_data;
29  char when[32];
30  nmsg_msgmod_t mod;
31  nmsg_res res;
32  struct tm *tm;
33  time_t t;
34 
35  np = msg->np;
36 
37  /* lock output */
38  pthread_mutex_lock(&output->pres->lock);
39 
40  t = np->time_sec;
41  tm = gmtime(&t);
42  strftime(when, sizeof(when), "%Y-%m-%d %T", tm);
43  mod = nmsg_msgmod_lookup(np->vid, np->msgtype);
44  if (mod != NULL) {
45  res = nmsg_message_to_pres(msg, &pres_data, output->pres->endline);
46  if (res != nmsg_res_success)
47  goto out;
48  } else {
49  nmsg_asprintf(&pres_data, "<UNKNOWN NMSG %u:%u>%s",
50  np->vid, np->msgtype,
51  output->pres->endline);
52  }
53  vname = nmsg_msgmod_vid_to_vname(np->vid);
54  mname = nmsg_msgmod_msgtype_to_mname(np->vid, np->msgtype);
55  fprintf(output->pres->fp, "[%zu] [%s.%09u] [%d:%d %s %s] "
56  "[%08x] [%s] [%s] %s%s",
57  np->has_payload ? np->payload.len : 0,
58  when, np->time_nsec,
59  np->vid, np->msgtype,
60  vname ? vname : "(unknown)",
61  mname ? mname : "(unknown)",
62  np->has_source ? np->source : 0,
63 
64  np->has_operator_ ?
66  : "",
67 
68  np->has_group ?
70  : "",
71 
72  output->pres->endline, pres_data);
73  fputs("\n", output->pres->fp);
74  if (output->pres->flush)
75  fflush(output->pres->fp);
76 
77  free(pres_data);
78 out:
79  /* unlock output */
80  pthread_mutex_unlock(&output->pres->lock);
81 
82  return (nmsg_res_success);
83 }
nmsg_res
nmsg result code
Definition: res.h:25
success
Definition: res.h:26
nmsg_res nmsg_message_to_pres(nmsg_message_t msg, char **pres, const char *endline)
Convert a message object to presentation format.
const char * nmsg_msgmod_msgtype_to_mname(unsigned vid, unsigned msgtype)
Convert a vendor ID / message type ID tuple to the human-readable form of the message type...
operator ID -> operator name
Definition: alias.h:32
const char * nmsg_msgmod_vid_to_vname(unsigned vid)
Convert a numeric vendor ID to its human-readable name.
nmsg_msgmod_t nmsg_msgmod_lookup(unsigned vid, unsigned msgtype)
Determine which nmsg_msgmod is responsible for a given vid/msgtype tuple, if any. ...
const char * nmsg_alias_by_key(nmsg_alias_e ae, unsigned key)
Look up an alias by key.
Definition: alias.c:47
group ID -> group name
Definition: alias.h:33