Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add net module, tests, and docs #686

Merged
merged 10 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/docs/standard-lib/net.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
layout: default
title: IO
nav_order: 13
parent: Standard Library
---

# IO
{: .no_toc }

## Table of contents
{: .no_toc .text-delta }

1. TOC
{:toc}

---

## Net

To make use of the Net module an import is required.

```cs
import Net;
```

### Net.parseIp4(String) -> Result\<List>

parseIp4 takes a string representation of an IP4 address and returns a Result that unwraps to list containing an entry for each octet.

```cs
Net.parseIp4("10.0.0.2").unwrap();
// [10, 0, 0, 2]
```
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/object.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Object
nav_order: 13
nav_order: 14
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/path.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Path
nav_order: 14
nav_order: 15
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/process.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Process
nav_order: 15
nav_order: 16
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/queue.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Queue
nav_order: 16
nav_order: 17
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/random.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Random
nav_order: 17
nav_order: 18
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/socket.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Socket
nav_order: 18
nav_order: 19
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/sqlite.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Sqlite
nav_order: 19
nav_order: 20
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/stack.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Stack
nav_order: 20
nav_order: 21
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/system.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: System
nav_order: 21
nav_order: 22
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/term.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Term
nav_order: 22
nav_order: 23
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/unittest.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: UnitTest
nav_order: 23
nav_order: 24
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/uuid.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: UUID
nav_order: 24
nav_order: 25
parent: Standard Library
---

Expand Down
7 changes: 7 additions & 0 deletions examples/ip.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Net;


print(Net.ip4Len);
print(Net.ip6Len);
const res = Net.parseIp4("10.1.1.9").unwrap();
print(res);
84 changes: 84 additions & 0 deletions src/optionals/net.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#ifdef _WIN32
#include "windowsapi.h"
#include <Ws2tcpip.h>
#else
#include <arpa/inet.h>
#endif

#include <string.h>

#include "net.h"
#include "../vm/vm.h"

#define IP4_LEN 4
#define IP6_LEN 16

static Value parseIp4(DictuVM *vm, int argCount, Value *args) {
if (argCount != 1) {
runtimeError(vm, "parseIp4() takes 1 argument (%d given)", argCount);
return EMPTY_VAL;
}

if (!IS_STRING(args[0])) {
runtimeError(vm, "parseIp4() argument must be a string");
return EMPTY_VAL;
}

char *ipStr = AS_CSTRING(args[0]);
char ignore[4] = {0};

#ifdef _WIN32
if (InetPton(AF_INET, ipStr, ignore) == 0) {
return newResultError(vm, "invalid ip4 address");
}
#else
if (inet_pton(AF_INET, ipStr, ignore) == 0) {
return newResultError(vm, "invalid ip4 address");
}
#endif

unsigned char value[IP4_LEN] = {0};
size_t index = 0;

while (*ipStr) {
if (isdigit((unsigned char)*ipStr)) {
value[index] *= 10;
value[index] += *ipStr - '0';
} else {
index++;
}
ipStr++;
}

ObjList *list = newList(vm);
push(vm, OBJ_VAL(list));

for (int i = 0; i < IP4_LEN; i++) {
writeValueArray(vm, &list->values, NUMBER_VAL(value[i]));
}

pop(vm);

return newResultSuccess(vm, OBJ_VAL(list));
}

Value createNetModule(DictuVM *vm) {
ObjString *name = copyString(vm, "Net", 3);
push(vm, OBJ_VAL(name));
ObjModule *module = newModule(vm, name);
push(vm, OBJ_VAL(module));

defineNativeProperty(vm, &module->values, "ip4Len", NUMBER_VAL(IP4_LEN));
defineNativeProperty(vm, &module->values, "ip6Len", NUMBER_VAL(IP6_LEN));

/**
* Define Net methods
*/
defineNative(vm, &module->values, "parseIp4", parseIp4);

pop(vm);
pop(vm);

return OBJ_VAL(module);
}

8 changes: 8 additions & 0 deletions src/optionals/net.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef dictu_net_h
#define dictu_net_h

#include "optionals.h"

Value createNetModule(DictuVM *vm);

#endif //dictu_net_h
1 change: 1 addition & 0 deletions src/optionals/optionals.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ BuiltinModules modules[] = {
{"Path", &createPathModule, false},
{"Datetime", &createDatetimeModule, false},
{"Socket", &createSocketModule, false},
{"Net", &createNetModule, false},
{"Random", &createRandomModule, false},
{"Base64", &createBase64Module, false},
{"Hashlib", &createHashlibModule, false},
Expand Down
1 change: 1 addition & 0 deletions src/optionals/optionals.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "c.h"
#include "datetime.h"
#include "socket.h"
#include "net.h"
#include "random.h"
#include "base64.h"
#include "hashlib.h"
Expand Down
7 changes: 7 additions & 0 deletions tests/net/import.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* import.du
*
* General import file for net module tests.
*/

import "parseIp4.du";
40 changes: 40 additions & 0 deletions tests/net/parseIp4.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* parseIp.du
*
* Testing parseIp function and net module constants.
*
*/

from UnitTest import UnitTest;

import Net;

class TestParseIp4 < UnitTest {
const ip4Addr = "10.0.0.2";

testIp4Len() {
this.assertEquals(Net.ip4Len, 4);
}

testIp6Len() {
this.assertEquals(Net.ip6Len, 16);
}

testParseIp4() {
const res = Net.parseIp4(this.ip4Addr);
this.assertSuccess(res);

const parsed = res.unwrap();
this.assertEquals(parsed.len(), Net.ip4Len);
}

testParseBadIp4() {
const res = Net.parseIp4("900.8");
this.assertError(res);

const res2 = Net.parseIp4("255.256.255.255");
this.assertError(res2);
}
}

TestParseIp4().run();
1 change: 1 addition & 0 deletions tests/runTests.du
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import "json/import.du";
import "log/import.du";
import "path/import.du";
import "sockets/import.du";
import "net/import.du";
import "base64/import.du";
import "sqlite/import.du";
import "process/import.du";
Expand Down