Last active
July 12, 2018 15:40
-
-
Save BrianLeishman/6d6374b91008f50ace11352b3cc89b67 to your computer and use it in GitHub Desktop.
MySQL UDF for an 8-byte time & environment based ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2018, Brian Leishman | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are met: | |
* | |
* * Redistributions of source code must retain the above copyright notice, this | |
* list of conditions and the following disclaimer. | |
* * Redistributions in binary form must reproduce the above copyright notice, | |
* this list of conditions and the following disclaimer in the documentation | |
* and/or other materials provided with the distribution. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
* POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/* | |
* USAGE INSTRUCTIONS: | |
* | |
* make sure libmysqlclient-dev and libsodium is installed: | |
* apt install libmysqlclient-dev libsodium13 libsodium-dev | |
* | |
* Replace "/usr/lib/mysql/plugin" with your MySQL plugins directory (can be found by running "select @@plugin_dir;") | |
* gcc -D ENVIRONMENT=0 -I/usr/include/mysql -o bid2.so -shared bid2.c -lsodium -fPIC && cp bid2.so /usr/lib/mysql/plugin/bid2.so | |
* | |
* Then, on the server: | |
* create function`bid2`returns string soname'bid2.so'; | |
* | |
* And use/test like: | |
* select `bid2`(); | |
* | |
* Also, make sure to store in a "BINARY(8)" column type | |
* | |
* Yeet! | |
* | |
*/ | |
#ifdef STANDARD | |
/* STANDARD is defined, don't use any mysql functions */ | |
#include <string.h> | |
#ifdef __WIN__ | |
typedef unsigned __int64 ulonglong; /* Microsofts 64 bit types */ | |
typedef __int64 longlong; | |
#else | |
typedef unsigned long long ulonglong; | |
typedef long long longlong; | |
#endif /*__WIN__*/ | |
#else | |
#include <my_global.h> | |
#include <my_sys.h> | |
#if defined(MYSQL_SERVER) | |
#include <m_string.h> | |
#else | |
/* when compiled as standalone */ | |
#include <string.h> | |
#endif | |
#endif | |
#include <mysql.h> | |
#include <ctype.h> | |
#include <stdint.h> | |
#include <time.h> | |
#ifndef ENVIRONMENT | |
#define ENVIRONMENT 0 | |
#endif | |
#include <sodium.h> | |
#ifdef HAVE_DLOPEN | |
my_bool bid2_init(UDF_INIT *initid, UDF_ARGS *args, char *message); | |
void bid2_deinit(UDF_INIT *initid); | |
char *bid2(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused))); | |
my_bool bid2_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { | |
if (sodium_init() == -1) { | |
strcpy(message, "`bid2`() requires libsodium"); | |
return 1; | |
} | |
return 0; | |
} | |
char *bid2(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused))) { | |
struct timespec ts; | |
unsigned char *bytes = malloc(sizeof (char) * (8)); | |
unsigned long buf; | |
timespec_get(&ts, TIME_UTC); | |
uint64_t Timestamp = ts.tv_sec * 1000000000 + ts.tv_nsec; | |
randombytes_buf(&buf, 1); | |
bytes[0] = (Timestamp >> 56) & 0b11111111; | |
bytes[1] = (Timestamp >> 48) & 0b11111111; | |
bytes[2] = (Timestamp >> 40) & 0b11111111; | |
bytes[3] = (Timestamp >> 32) & 0b11111111; | |
bytes[4] = (Timestamp >> 24) & 0b11111111; | |
bytes[5] = (Timestamp >> 16) & 0b11111111; | |
bytes[6] = (Timestamp >> 8) & 0b11111111; | |
bytes[7] = (ENVIRONMENT & 0b00001111) | (buf & 0b11110000); | |
*length = 8; | |
return bytes; | |
} | |
#endif /* HAVE_DLOPEN */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment