all repos — xrxs @ 2d4f1f5ec44e7056408dc1c080de2736914ea4a2

experimental networked application/game server with 9p

added MIT license and created headers and code files for all the types
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmDinFoACgkQO3+8IhRO
Y5j1sw//dxzg/XE8dGJrU2LXtuyOSxP3PmnPSvKhJzbLypHySigmrELEcY89f86g
x+jRHjDqEk97qYKCI9vGbQum07OcQaxq451laSnUpkFgW6H90qRCQPodSG7G5Veu
YZRzWg6MGtp3+AP0no1LiAk81aNpRIbk0BKLII6vV87bGBj+GJElylkvPysPt/Y8
W0UxUrcr9mPSZH8a/dcZEauPJ6vpy2im+PWURn6JB5gYuFISEXmvGCZNIM6RjORd
HZaqYUCAi6DQFuByHp/6WLkJaM7dZtmBl28szOWE/PrBv6gx7ymVi7ypM7udN/oP
GtmmGmXa2JQMShSTQdtNK2HkQX4i4vWPl99Rtg0vi2+bTiIkRlroirbunU0VTqlw
qjwA5FH7VqzDjBiJx0mBhxqYcd6DT2XJH3QvSiSerPdYMthQvzyP8+HuGn8cs3Zv
KNy3fXzsQ/dD+bVHYwj01lt2nQy5ktXxlCXFF9KhQs8T0d9cl+EQCfomx7KpLU1O
i5b5QsfiXZ16RFI1HAX0xaaScptz1qnoVkn0Rjm8CIasfAw8ngLUaTrw1VlhL8Lj
GHhNnEVwBnL5xz5CIUjLRP2QmN0oTKa2//oo00p/xbbYN785UMuVWYHfXnF6/swr
Pym3LcDAtZu4jab2is9a4kzMaDQFITpobJ4uzVx1idlox9pcGTY=
=zF0T
-----END PGP SIGNATURE-----
commit

2d4f1f5ec44e7056408dc1c080de2736914ea4a2

parent

0cc1835a02348eb0fd89e59cdc663d90a0fb0fbf

17 files changed, 203 insertions(+), 75 deletions(-)

jump to
A LICENSE

@@ -0,0 +1,8 @@

+Copyright 2021 Derek Stevens + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +
A aux.c

@@ -0,0 +1,12 @@

+#include <u.h> +#include <libc.h> +#include "aux.h" + +Aux* create_aux(FileType t) { + Aux* self = (Aux*)malloc(sizeof(Aux)); + self->type = t; + self->data = nil; + self->count = 0; + + return self; +}
A aux.h

@@ -0,0 +1,9 @@

+typedef enum { CTL = 1, USERS, CARTS, SLOT, DATA, REALMS, UNIVERSE } FileType; + +typedef struct Aux { + FileType type; + char* data; + int count; +} Aux; + +Aux* create_aux(FileType t);
M build.shbuild.sh

@@ -1,6 +1,6 @@

#!/bin/sh -clang-format -i ./*.c +clang-format -i ./*.c ./*.h mk clean mk o.xrxs
A cart.c

          
A cart.h

@@ -0,0 +1,7 @@

+typedef struct Cart { + char name[32]; + char* cart_rom; + char* cart_txt_data; + char* cart_sprite_data; + char* cart_audio_data; +} Cart;
A command.h

@@ -0,0 +1,12 @@

+typedef enum { + LOGIN = 208176873, + LOAD = 5626172, + CREATE = 7083959236, + PROTECT = 295480618573, + ENTER = 195024746, + LEAVE = 207662601, + LOGOUT = 7702552890, + SAVE = 5962355, + RESET = 218931595, + UNLOAD = 8325026851 +} Command;
A err.h

@@ -0,0 +1,2 @@

+#define EUNAME "username is already taken" +#define EUFULL "users table is full"
A realm.h

@@ -0,0 +1,8 @@

+typedef struct Universe Universe; + +typedef struct Realm { + char name[32]; + ushort max; + uvlong password; + Universe* universe; +} Realm;
A universe.h

@@ -0,0 +1,11 @@

+typedef unsigned int unit; + +typedef struct Atom { + char name[16]; + char value[64]; +} Atom; + +typedef struct Universe { + uint count; + Atom** data; +} Universe;
A user.c

          
A user.h

@@ -0,0 +1,8 @@

+typedef struct Cart Cart; +typedef struct Realm Realm; +typedef struct UserInfo { + char name[32]; + uvlong password; + Cart* cart; + Realm* realm; +} UserInfo;
A util.c

@@ -0,0 +1,92 @@

+#include <u.h> +#include "util.h" + +uvlong hash(char* str) { + uvlong h; + uchar* p; + + h = 0; + for (p = (unsigned char*)str; *p != '\0'; p++) + h = 37 * h + *p; + return h; // or, h % ARRAY_SIZE; +} + +char clca(char c) { + return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; +} /* char to lowercase */ + +char cuca(char c) { + return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c; +} /* char to uppercase */ + +int slen(char* s) { + int i = 0; + while (s[i] && s[++i]) { + ; + } + return i; +} /* string length */ + +char* st__(char* s, char (*fn)(char)) { + int i = 0; + char c; + while ((c = s[i])) + s[i++] = fn(c); + return s; +} +char* stuc(char* s) { return st__(s, cuca); } /* string to uppercase */ + +char* stlc(char* s) { return st__(s, clca); } /* string to lowercase */ + +char* scpy(char* src, char* dst, int len) { + int i = 0; + while ((dst[i] = src[i]) && i < len - 2) + i++; + dst[i + 1] = '\0'; + return dst; +} /* string copy */ + +int scmp(char* a, char* b) { + int i = 0; + while (a[i] == b[i]) + if (!a[i++]) + return 1; + return 0; +} /* string compare */ + +char* scsw(char* s, char a, char b) { + int i = 0; + char c; + while ((c = s[i])) + s[i++] = c == a ? b : c; + return s; +} /* string char swap */ + +char* scat(char* dst, const char* src) { + char* ptr = dst + slen(dst); + while (*src) + *ptr++ = *src++; + *ptr = '\0'; + return dst; +} /* string cat */ + +int ssin(char* s, char* ss) { + int a = 0, b = 0; + while (s[a]) { + if (s[a] == ss[b]) { + if (!ss[b + 1]) + return a - b; + b++; + } else + b = 0; + a++; + } + return -1; +} /* string substring index */ + +char* ccat(char* dst, char c) { + int len = slen(dst); + dst[len] = c; + dst[len + 1] = '\0'; + return dst; +}
A util.h

@@ -0,0 +1,15 @@

+typedef unsigned long long uvlong; + +uvlong hash(char* str); +char clca(char c); +char cuca(char c); +int slen(char* str); +char* st__(char* str, char (*fn)(char c)); +char* stuc(char* str); +char* stlc(char* str); +char* scpy(char* src, char* dst, int len); +int scmp(char* a, char* b); +char* scsw(char* str, char a, char b); +char* scat(char* dst, const char* src); +int ssin(char* str, char* substr); +char* ccat(char* dst, char c);
M xrxs.cxrxs.c

@@ -6,84 +6,21 @@ #include <9p.h>

#include <stdio.h> #include <dirent.h> #include <libString.h> -#include <mp.h> -#include <libsec.h> - -/* clang-format off */ - -char clca(char c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; } /* char to lowercase */ -char cuca(char c) { return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c; } /* char to uppercase */ -int slen(char *s) { int i = 0; while(s[i] && s[++i]) { ; } return i; } /* string length */ -char *st__(char *s, char (*fn)(char)) { int i = 0; char c; while((c = s[i])) s[i++] = fn(c); return s; } -char *stuc(char *s) { return st__(s, cuca); } /* string to uppercase */ -char *stlc(char *s) { return st__(s, clca); } /* string to lowercase */ -char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */ -int scmp(char *a, char *b) { int i = 0; while(a[i] == b[i]) if(!a[i++]) return 1; return 0; } /* string compare */ -char *scsw(char *s, char a, char b) { int i = 0; char c; while((c = s[i])) s[i++] = c == a ? b : c; return s; } /* string char swap */ -char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */ -int ssin(char *s, char *ss) { int a = 0, b = 0; while(s[a]) { if(s[a] == ss[b]) { if(!ss[b + 1]) return a - b; b++; } else b = 0; a++; } return -1; } /* string substring index */ -char *ccat(char *dst, char c) { int len = slen(dst); dst[len] = c; dst[len + 1] = '\0'; return dst; } +#include "err.h" +#include "command.h" +#include "util.h" +#include "aux.h" +#include "cart.h" +#include "universe.h" +#include "realm.h" +#include "user.h" -/* clang-format on */ int chatty9p = 1; -static char Ebad[] = "something bad happened"; -static char Enomem[] = "no memory"; -static char Euname[] = "username is already taken"; - static Tree* tree; -typedef enum { - LOGIN = 208176873, - LOAD = 5626172, - CREATE = 7083959236, - PROTECT = 295480618573, - ENTER = 195024746, - LEAVE = 207662601, - LOGOUT = 7702552890, - SAVE = 5962355, - RESET = 218931595, - UNLOAD = 8325026851 -} Command; - -typedef enum { CTL = 1, USERS, CARTS, SLOT, DATA, REALMS, UNIVERSE } FileType; - -typedef struct Aux Aux; -struct Aux { - FileType type; - char* data; - int count; -}; - -Aux* create_aux(FileType t) { - Aux* self = (Aux*)malloc(sizeof(Aux)); - self->type = t; - self->data = nil; - self->count = 0; - - return self; -} - -typedef struct UserInfo UserInfo; -struct UserInfo { - char name[32]; - uvlong password; - char realm[32]; - char cart[32]; -}; - static UserInfo users_table[64]; -uvlong hash(char* str) { - uvlong h; - uchar* p; - - h = 0; - for (p = (unsigned char*)str; *p != '\0'; p++) - h = 37 * h + *p; - return h; // or, h % ARRAY_SIZE; -} - void xrxs_attach(Req* r) { /* As it is, once the user detaches, they will stay in the table * until the server restarts. We have to figure out some way

@@ -92,6 +29,7 @@ * or the attach will never complete.

*/ int i = 0; int l = 0; + int vacancy = 0; char* usr; char* username = r->ifcall.uname; usr = username;

@@ -102,15 +40,20 @@ }

for (i = 0; i < 64; i++) { usr = users_table[i].name; if (scmp(usr, username)) { - respond(r, Euname); + respond(r, EUNAME); return; } if (*usr == 0) { scpy(username, usr, l + 1); + vacancy = 1; break; } } - respond(r, nil); + if (vacancy) { + respond(r, nil); + } else { + respond(r, EUFULL); + } } void login(char* uname, char* password) {

@@ -129,7 +72,8 @@ for (i = 0; i < 64; i++) {

if (scmp(uname, users_table[i].name)) { *(users_table[i].name) = 0; users_table[i].password = 0; - *(users_table[i].realm) = 0; + /* free cart */ + /* free realm */ return 1; } }