2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 Daniel Borkmann.
4 * Subject to the GPL, version 2.
21 #include "curvetun_mgmt_servers.h"
28 char port
[6]; /* 5 + \0 */
29 unsigned char publickey
[crypto_box_pub_key_size
];
30 struct curve25519_proto proto_inf
;
31 unsigned char auth_token
[crypto_auth_hmacsha512256_KEYBYTES
];
32 struct server_store
*next
;
35 static struct server_store
*store
= NULL
;
36 static struct server_store
*selected
= NULL
;
37 static struct rwlock store_lock
;
39 static struct server_store
*server_store_alloc(void)
41 return xzmalloc(sizeof(struct server_store
));
44 static void server_store_free(struct server_store
*ss
)
48 memset(ss
, 0, sizeof(struct server_store
));
61 static int parse_line(char *line
, char *homedir
)
64 enum parse_states s
= PARSE_ALIAS
;
65 struct server_store
*elem
;
66 unsigned char pkey
[crypto_box_pub_key_size
];
68 elem
= server_store_alloc();
71 str
= strtok(line
, ";");
72 for (; str
!= NULL
;) {
75 strlcpy(elem
->alias
, str
, sizeof(elem
->alias
));
79 strlcpy(elem
->host
, str
, sizeof(elem
->host
));
83 strlcpy(elem
->port
, str
, sizeof(elem
->port
));
87 if (!strncmp("udp", str
, strlen("udp")))
89 else if (!strncmp("tcp", str
, strlen("tcp")))
92 syslog(LOG_ERR
, "Incorrect carrier type !(udp|tcp) in server spec.\n");
98 if (!curve25519_pubkey_hexparse_32(pkey
, sizeof(pkey
),
101 memcpy(elem
->publickey
, pkey
, sizeof(elem
->publickey
));
102 memcpy(elem
->auth_token
, pkey
, sizeof(elem
->auth_token
));
103 curve25519_proto_init(&elem
->proto_inf
, elem
->publickey
, sizeof(elem
->publickey
));
112 str
= strtok(NULL
, ";");
116 return s
== PARSE_DONE
? 0 : -EIO
;
119 void parse_userfile_and_generate_serv_store_or_die(char *homedir
)
122 char path
[PATH_MAX
], buff
[1024];
125 memset(path
, 0, sizeof(path
));
126 slprintf(path
, sizeof(path
), "%s/%s", homedir
, FILE_SERVERS
);
128 rwlock_init(&store_lock
);
129 rwlock_wr_lock(&store_lock
);
131 fp
= fopen(path
, "r");
133 panic("Cannot open server file!\n");
135 memset(buff
, 0, sizeof(buff
));
136 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
137 buff
[sizeof(buff
) - 1] = 0;
138 /* A comment. Skip this line */
139 if (buff
[0] == '#' || buff
[0] == '\n') {
140 memset(buff
, 0, sizeof(buff
));
145 ret
= parse_line(buff
, homedir
);
147 panic("Cannot parse line %d from clients!\n", line
);
149 memset(buff
, 0, sizeof(buff
));
155 panic("No registered servers found!\n");
157 rwlock_unlock(&store_lock
);
160 void dump_serv_store(void)
163 struct server_store
*elem
;
165 rwlock_rd_lock(&store_lock
);
168 printf("[%s] -> %s:%s via %s -> ", elem
->alias
,
169 elem
->host
, elem
->port
,
170 elem
->udp
? "udp" : "tcp");
171 for (i
= 0; i
< sizeof(elem
->publickey
); ++i
)
172 if (i
== (sizeof(elem
->publickey
) - 1))
173 printf("%02x\n", (unsigned char)
176 printf("%02x:", (unsigned char)
180 rwlock_unlock(&store_lock
);
183 void destroy_serv_store(void)
185 struct server_store
*elem
, *nelem
= NULL
;
187 rwlock_wr_lock(&store_lock
);
193 server_store_free(elem
);
196 rwlock_unlock(&store_lock
);
197 rwlock_destroy(&store_lock
);
200 void get_serv_store_entry_by_alias(char *alias
, size_t len
,
201 char **host
, char **port
, int *udp
)
203 struct server_store
*elem
;
205 rwlock_rd_lock(&store_lock
);
208 while (elem
&& elem
->next
)
211 (*host
) = elem
->host
;
212 (*port
) = elem
->port
;
216 rwlock_unlock(&store_lock
);
221 if (!strncmp(elem
->alias
, alias
,
222 min(len
, strlen(elem
->alias
) + 1)))
227 (*host
) = elem
->host
;
228 (*port
) = elem
->port
;
232 rwlock_unlock(&store_lock
);
236 rwlock_unlock(&store_lock
);
245 struct curve25519_proto
*get_serv_store_entry_proto_inf(void)
247 struct curve25519_proto
*ret
= NULL
;
249 rwlock_rd_lock(&store_lock
);
251 ret
= &selected
->proto_inf
;
252 rwlock_unlock(&store_lock
);
257 unsigned char *get_serv_store_entry_auth_token(void)
259 unsigned char *ret
= NULL
;
261 rwlock_rd_lock(&store_lock
);
263 ret
= selected
->auth_token
;
264 rwlock_unlock(&store_lock
);