Commit a200cd24 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Move the sockaddr emitting function to vcc_utils.c (ne: vcc_string.c)

where the rest of the compiler can get at it.
parent ffc4166d
......@@ -22,7 +22,7 @@ libvcl_la_SOURCES = \
vcc_fixed_token.c \
vcc_obj.c \
vcc_storage.c \
vcc_string.c \
vcc_utils.c \
vcc_symb.c \
vcc_token.c \
vcc_var.c \
......
......@@ -61,7 +61,6 @@
#include "vcc_compile.h"
#include "vss.h"
#include "vsa.h"
struct host {
VTAILQ_ENTRY(host) list;
......@@ -69,42 +68,6 @@ struct host {
char *vgcname;
};
/*
* The IPv6 crew royally screwed up the entire idea behind
* struct sockaddr, and combined with various other incomptency
* in the OS business, that means that there is no sane or even
* remotely portable way to initialize a sockaddr at compile time.
*
* In our case it is slightly more tricky than that, because we don't
* even want to #include the struct sockaddr* definitions.
*
* Instead we make sure the sockaddr is sane (for our values of sane)
* and dump it in binary, using a 64 bit integertype, hoping that this
* will ensure good enough alignment.
*/
static int
emit_sockaddr(struct vcc *tl, const void *sa, unsigned sal)
{
unsigned n = (sal + 7) / 8, len;
uint64_t b[n];
assert(VSA_Sane(sa));
AN(sa);
AN(sal);
assert(sal < 256);
assert(sizeof(unsigned long long) == 8);
Fh(tl, 0, "\nstatic const unsigned long long");
Fh(tl, 0, " sockaddr_%u[%d] = {\n", tl->unique, n);
memcpy(b, sa, sal);
for (len = 0; len <n; len++) {
Fh(tl, 0, "%s 0x%016jx",
len ? ",\n" : "",
(uintmax_t)b[len]);
}
Fh(tl, 0, "\n};\n");
return (tl->unique++);
}
/*--------------------------------------------------------------------
* Struct sockaddr is not really designed to be a compile time
......@@ -124,9 +87,10 @@ Emit_Sockaddr(struct vcc *tl, const struct token *t_host, const char *port)
{
struct foo_proto protos[3], *pp;
struct addrinfo *res, *res0, *res1, hint;
int error, retval, x;
int error, retval;
char hbuf[NI_MAXHOST];
char *hop, *pop;
const char *sa;
AN(t_host->dec);
......@@ -204,8 +168,8 @@ Emit_Sockaddr(struct vcc *tl, const struct token *t_host, const char *port)
pp->l = res->ai_addrlen;
memcpy(&pp->sa, res->ai_addr, pp->l);
x = emit_sockaddr(tl, res->ai_addr, res->ai_addrlen);
Fb(tl, 0, "\t.%s_sockaddr = sockaddr_%u,\n", pp->name, x);
sa = vcc_sockaddr(tl, res->ai_addr, res->ai_addrlen);
Fb(tl, 0, "\t.%s_sockaddr = %s,\n", pp->name, sa);
error = getnameinfo(res->ai_addr,
res->ai_addrlen, hbuf, sizeof hbuf,
NULL, 0, NI_NUMERICHOST);
......
......@@ -286,7 +286,8 @@ void vcc_Parse(struct vcc *tl);
sym_wildcard_t vcc_Stv_Wildcard;
/* vcc_string.c */
char *vcc_regexp(struct vcc *tl);
const char *vcc_regexp(struct vcc *tl);
const char *vcc_sockaddr(struct vcc *tl, const void *sa, unsigned sal);
/* vcc_symb.c */
struct symbol *VCC_AddSymbolStr(struct vcc *tl, const char *name, enum symkind);
......
......@@ -441,7 +441,7 @@ vcc_Eval_Regsub(struct vcc *tl, struct expr **e, const struct symbol *sym)
{
struct expr *e2;
int all = sym->eval_priv == NULL ? 0 : 1;
char *p;
const char *p;
char buf[128];
vcc_delete_expr(*e);
......@@ -963,7 +963,7 @@ vcc_expr_cmp(struct vcc *tl, struct expr **e, enum var_type fmt)
struct expr *e2;
const struct cmps *cp;
char buf[256];
char *re;
const char *re;
const char *not;
struct token *tk;
......
......@@ -31,15 +31,17 @@
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include "vcc_compile.h"
#include "vre.h"
#include "vrt.h"
#include "vsa.h"
/*--------------------------------------------------------------------*/
char *
const char *
vcc_regexp(struct vcc *tl)
{
char buf[BUFSIZ], *p;
......@@ -70,3 +72,45 @@ vcc_regexp(struct vcc *tl)
Ff(tl, 0, "\tVRT_re_fini(%s);\n", buf);
return (p);
}
/*
* The IPv6 crew royally screwed up the entire idea behind
* struct sockaddr, and combined with various other incomptency
* in the OS business, that means that there is no sane or even
* remotely portable way to initialize a sockaddr at compile time.
*
* In our case it is slightly more tricky than that, because we don't
* even want to #include the struct sockaddr* definitions.
*
* Instead we make sure the sockaddr is sane (for our values of sane)
* and dump it in binary, using a 64 bit integertype, hoping that this
* will ensure good enough alignment.
*/
const char *
vcc_sockaddr(struct vcc *tl, const void *sa, unsigned sal)
{
unsigned n = (sal + 7) / 8, len;
uint64_t b[n];
char *p;
assert(VSA_Sane(sa));
AN(sa);
AN(sal);
assert(sal < 256);
assert(sizeof(unsigned long long) == 8);
p = TlAlloc(tl, 20);
sprintf(p, "sockaddr_%u", tl->unique++);
Fh(tl, 0, "\nstatic const unsigned long long");
Fh(tl, 0, " %s[%d] = {\n", p, n);
memcpy(b, sa, sal);
for (len = 0; len <n; len++) {
Fh(tl, 0, "%s 0x%016jx",
len ? ",\n" : "",
(uintmax_t)b[len]);
}
Fh(tl, 0, "\n};\n");
return (p);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment