feat(server): added general networking functions needed for server
This commit is contained in:
parent
a7e00c443c
commit
6f39b4d502
4 changed files with 67 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -66,3 +66,5 @@ Module.symvers
|
|||
Mkfile.old
|
||||
dkms.conf
|
||||
|
||||
build
|
||||
.cache
|
||||
|
|
@ -53,7 +53,7 @@ struct qnet_server {
|
|||
unsigned short port;
|
||||
|
||||
int socket;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### Enums
|
||||
|
|
|
|||
35
include/qnet/network.h
Normal file
35
include/qnet/network.h
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
|
||||
*
|
||||
* This software is licensed under the Apache License, Version 2.0 (the "License");
|
||||
* You may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License in the file LICENSE or at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
#ifndef QNET_NETWORK
|
||||
#define QNET_NETWORK
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define QNET_INVALID_SOCKET -1
|
||||
|
||||
typedef int qnet_socket_type;
|
||||
#elif defined(_WIN32)
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
#define QNET_INVALID_SOCKET INVALID_SOCKET
|
||||
|
||||
typedef SOCKET qnet_socket_type;
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
QNET_NETWORK_ERROR_OK = 0,
|
||||
QNET_NETWORK_ERROR_INVALID_IP = -1
|
||||
} QNetNetworkError;
|
||||
|
||||
QNetNetworkError qnet_ip4_address(const char *ip, unsigned short port, struct sockaddr_in *address);
|
||||
QNetNetworkError qnet_ip6_address(const char *ip, unsigned short port, struct sockaddr_in6 *address);
|
||||
|
||||
#endif /* QNET_NETWORK */
|
||||
29
src/network.c
Normal file
29
src/network.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
|
||||
*
|
||||
* This software is licensed under the Apache License, Version 2.0 (the "License");
|
||||
* You may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License in the file LICENSE or at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
#include "qnet/network.h"
|
||||
|
||||
QNetNetworkError qnet_ip4_address(const char *ip, unsigned short port, struct sockaddr_in *address) {
|
||||
address->sin_family = AF_INET;
|
||||
address->sin_port = htons(port);
|
||||
if (inet_pton(address->sin_family, ip, &address->sin_addr.s_addr) != 0) {
|
||||
return QNET_NETWORK_ERROR_INVALID_IP;
|
||||
}
|
||||
|
||||
return QNET_NETWORK_ERROR_OK;
|
||||
}
|
||||
|
||||
QNetNetworkError qnet_ip6_address(const char *ip, unsigned short port, struct sockaddr_in6 *address) {
|
||||
address->sin6_family = AF_INET6;
|
||||
address->sin6_port = htons(port);
|
||||
if (inet_pton(address->sin6_family, ip, &address->sin6_addr.s6_addr) != 0) {
|
||||
return QNET_NETWORK_ERROR_INVALID_IP;
|
||||
}
|
||||
|
||||
return QNET_NETWORK_ERROR_OK;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue