Commit 1cfeb83e authored by Mike Hibler's avatar Mike Hibler

first cut at MYSQL support, not used by default (yet)

parent 24ed4e12
......@@ -3,15 +3,20 @@ INSTALL_BINDIR = /usr/testbed/bin
INSTALL = /usr/bin/install -c
CFLAGS = -g -DSOLARIS -DHAVE_SOCKADDR_SA_LEN -DUSE_RECVMSG
INCS = -I//n/moab/z/stoller/oskit-debug/install/include/oskit/boot
CFLAGS += -DUSE_NULL_DB
INCS = -I/n/moab/z/mike/flux/install.debug/include
all: proxydhcp bootinfo
proxydhcp: proxydhcp.c
cc $(CFLAGS) -o proxydhcp proxydhcp.c $(LFLAGS)
bootinfo: bootinfo.c
cc $(CFLAGS) $(INCS) -o bootinfo bootinfo.c $(LFLAGS)
bootinfo: bootinfo.c bootinfo_null.c bootinfo_mysql.c
cc $(CFLAGS) $(INCS) -I/usr/local/include -o bootinfo $? \
$(LFLAGS) -L/usr/local/lib/mysql -lmysqlclient
testmysql: bootinfo_mysql.c
cc $(CFLAGS) -DTEST $(INCS) -o testmysql bootinfo_mysql.c $(LFLAGS)
install: all
-mkdir $(INSTALL_BINDIR)
......@@ -23,5 +28,5 @@ install: all
$(INSTALL) -m 755 bootinfo.restart $(INSTALL_BINDIR)/pxe
clean:
rm proxydhcp bootinfo
rm -f proxydhcp bootinfo testmysql
......@@ -2,7 +2,8 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include "bootwhat.h"
#include <oskit/boot/bootwhat.h>
/*
* For now, hardwired.
......@@ -11,11 +12,18 @@
main()
{
int sock, length, data, i, mlen;
int sock, length, data, i, mlen, err;
struct sockaddr_in name, client;
boot_info_t boot_info;
boot_what_t *boot_whatp = (boot_what_t *) &boot_info.data;
/* Initialize data base */
err = open_bootinfo_db();
if (err) {
fprintf(stderr, "Could not open database\n");
exit(1);
}
/* Create socket from which to read. */
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
......@@ -55,15 +63,11 @@ main()
continue;
}
boot_info.opcode = BIOPCODE_BOOTWHAT_REPLY;
boot_info.status = BISTAT_SUCCESS;
#if 0
boot_whatp->type = BIBOOTWHAT_TYPE_MB;
boot_whatp->what.mb.tftp_ip.s_addr = 0;
strcpy(boot_whatp->what.mb.filename, NETBOOT);
#else
boot_whatp->type = BIBOOTWHAT_TYPE_SYSID;
boot_whatp->what.sysid = 165; /* BSD */
#endif
err = query_bootinfo_db(client.sin_addr, boot_whatp);
if (err)
boot_info.status = BISTAT_FAIL;
else
boot_info.status = BISTAT_SUCCESS;
client.sin_family = AF_INET;
client.sin_port = htons(BOOTWHAT_SRCPORT);
if (sendto(sock, (char *)&boot_info, sizeof(boot_info), 0,
......@@ -71,5 +75,7 @@ main()
perror("sendto");
}
close(sock);
close_bootinfo_db();
exit(0);
}
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <oskit/boot/bootwhat.h>
#include <mysql/mysql.h>
#ifdef USE_MYSQL_DB
static char dbname[] = "tbdb";
static char dbquery[] =
"select n.boot_default from nodes as n, interfaces as i "
"where i.IP = \"%s\" and n.node_id = i.node_id";
static MYSQL db;
int
open_bootinfo_db(void)
{
return 0;
}
int
query_bootinfo_db(struct in_addr ipaddr, boot_what_t *info)
{
char querybuf[1024];
int n, nrows, ncols, part;
MYSQL db;
MYSQL_RES *res;
MYSQL_ROW row;
n = snprintf(querybuf, sizeof querybuf, dbquery, inet_ntoa(ipaddr));
if (n > sizeof querybuf) {
fprintf(stderr, "query too long for buffer\n");
return 1;
}
mysql_init(&db);
if (mysql_real_connect(&db, 0, 0, 0, dbname, 0, 0, 0) == 0) {
fprintf(stderr, "%s: connect failed: %s\n",
dbname, mysql_error(&db));
return 1;
}
if (mysql_real_query(&db, querybuf, n) != 0) {
fprintf(stderr, "%s: query failed: %s\n",
dbname, mysql_error(&db));
mysql_close(&db);
return 1;
}
res = mysql_store_result(&db);
if (res == 0) {
fprintf(stderr, "%s: store_result failed: %s\n",
dbname, mysql_error(&db));
mysql_close(&db);
return 1;
}
mysql_close(&db);
nrows = (int)mysql_num_rows(res);
switch (nrows) {
case 0:
fprintf(stderr, "%s: no entry for host %s\n",
dbname, inet_ntoa(ipaddr));
mysql_free_result(res);
return 1;
case 1:
break;
default:
fprintf(stderr, "%s: %d entries for IP %s, using first\n",
dbname, nrows, inet_ntoa(ipaddr));
break;
}
ncols = (int)mysql_num_fields(res);
switch (ncols) {
case 1:
break;
default:
fprintf(stderr, "%s: %d fields in query for IP %s!\n",
dbname, ncols, inet_ntoa(ipaddr));
mysql_free_result(res);
return 1;
}
row = mysql_fetch_row(res);
if (row[0] == 0 || row[0] == '\0') {
fprintf(stderr, "%s: null query result for IP %s!\n",
dbname, inet_ntoa(ipaddr));
mysql_free_result(res);
return 1;
}
/*
* Just 45000 lines of code later, we have the 32 bits of info
* we wanted!
*/
part = atoi(row[0]);
mysql_free_result(res);
info->type = BIBOOTWHAT_TYPE_PART;
info->what.partition = part;
return 0;
}
int
close_bootinfo_db(void)
{
return 0;
}
#ifdef TEST
main(int argc, char **argv)
{
struct in_addr ipaddr;
boot_info_t boot_info;
boot_what_t *boot_whatp = (boot_what_t *)&boot_info.data;
while (--argc > 0) {
if (inet_aton(*++argv, &ipaddr))
if (query_bootinfo_db(ipaddr, boot_whatp) == 0)
printf("returned partition %d\n",
boot_whatp->what.partition);
else
printf("failed\n");
else
printf("bogus IP address `%s'\n", *argv);
}
exit(0);
}
#endif
#endif
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <oskit/boot/bootwhat.h>
#ifdef USE_NULL_DB
int
open_bootinfo_db(void)
{
return 0;
}
int
query_bootinfo_db(struct in_addr ipaddr, boot_what_t *info)
{
#if 0
info->type = BIBOOTWHAT_TYPE_MB;
info->what.mb.tftp_ip.s_addr = 0;
strcpy(info->what.mb.filename, NETBOOT);
#else
info->type = BIBOOTWHAT_TYPE_SYSID;
info->what.sysid = 165; /* BSD */
#endif
return 0;
}
int
close_bootinfo_db(void)
{
return 0;
}
#endif
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