1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdint.h>
6 #include <string.h>
7 #include <sys/types.h>
8 
9 #include <mos_api.h>
10 #include <mtcp_api.h>
11 #include <mtcp_epoll.h>
12 #include <assert.h>
13 #include <ctype.h>
14 
15 #include "applib.h"
16 /*----------------------------------------------------------------------------*/
17 char *
strevent(uint64_t ev)18 strevent(uint64_t ev)
19 {
20 	switch (ev) {
21 #define EV_CASE(e) case e: return #e
22 		/* mtcp-defined epoll events */
23 		EV_CASE(MOS_EPOLLNONE);
24 		EV_CASE(MOS_EPOLLIN);
25 		EV_CASE(MOS_EPOLLPRI);
26 		EV_CASE(MOS_EPOLLOUT);
27 		EV_CASE(MOS_EPOLLRDNORM);
28 		EV_CASE(MOS_EPOLLRDBAND);
29 		EV_CASE(MOS_EPOLLWRNORM);
30 		EV_CASE(MOS_EPOLLWRBAND);
31 		EV_CASE(MOS_EPOLLMSG);
32 		EV_CASE(MOS_EPOLLERR);
33 		EV_CASE(MOS_EPOLLHUP);
34 		EV_CASE(MOS_EPOLLRDHUP);
35 
36 		/* mtcp-defined epoll events */
37 		EV_CASE(MOS_EPOLLONESHOT);
38 		EV_CASE(MOS_EPOLLET);
39 
40 		default:
41 			return "Unknown";
42 
43 #undef EV_CASE
44 	}
45 }
46 /*----------------------------------------------------------------------------*/
47 char *
strcbevent(uint64_t ev)48 strcbevent(uint64_t ev)
49 {
50 	switch (ev) {
51 #define EV_CASE(e) case e: return #e
52 
53 		/* mos-defined tcp build-in events */
54 		EV_CASE(MOS_ON_PKT_IN);
55 		EV_CASE(MOS_ON_PKT_OUT);
56 		EV_CASE(MOS_ON_CONN_SETUP);
57 		EV_CASE(MOS_ON_CONN_NEW_DATA);
58 		EV_CASE(MOS_ON_TCP_STATE_CHANGE);
59 		EV_CASE(MOS_ON_CONN_END);
60 		EV_CASE(MOS_ON_TIMEOUT);
61 		EV_CASE(MOS_ON_ERROR);
62 		EV_CASE(MOS_ON_ORPHAN);
63 
64 		default:
65 			return "Unknown";
66 
67 #undef EV_CASE
68 	}
69 }
70 
71 /*----------------------------------------------------------------------------*/
72 #define SKIP_SPACES(x) while (*x && isspace((int)*x)) x++;
73 /*----------------------------------------------------------------------------*/
74 int
LoadConfig(char * file_name,struct conf_var * vlist,int size)75 LoadConfig(char *file_name, struct conf_var *vlist, int size)
76 {
77 	assert(file_name != NULL);
78 
79 	FILE *fp = fopen(file_name, "r");
80 	char line_buf[MAX_LINE_LEN] = {0};
81 	int i;
82 	char *p;
83 
84 	if (fp == NULL) {
85 		fprintf(stderr, "%s not found\n", file_name);
86 		return -1;
87 	}
88 
89 	while ((p = fgets(line_buf, MAX_LINE_LEN, fp)) != NULL) {
90 		char *temp;
91 
92 		if (p[MAX_LINE_LEN - 1]) {
93 			fprintf(stderr, "%s has a line longer than %d\n",
94 					file_name, MAX_LINE_LEN);
95 			return -1;
96 		}
97 
98 		SKIP_SPACES(p);
99 		if (*p == '\0' || *p == '#')
100 			continue;
101 
102 		/* comments */
103 		if ((temp = strchr(p, '#')))
104 			*temp = '\0';
105 
106 		/* remove spaces at the end of the line */
107 		while (isspace(p[strlen(p) - 1]))
108 			p[strlen(p) - 1] = '\0';
109 
110 		for (i = 0; i < size; i++) {
111 			int len = strlen(vlist[i].name);
112 			if (strncmp(vlist[i].name, p, len))
113 				continue;
114 
115 			if (!isspace(p[len]))
116 				continue;
117 
118 			if (!(p = strchr(p, '=')))
119 				break;
120 
121 			p++;
122 			SKIP_SPACES(p);
123 
124 			if ((len = strlen(p)) > CONF_VALUE_LEN)
125 				break;
126 
127 			strncpy(vlist[i].value, p, len);
128 			vlist[i].value[len] = '\0';
129 		}
130 	}
131 
132 	fclose(fp);
133 	return 0;
134 }
135 /*----------------------------------------------------------------------------*/
136