Compare commits
35 Commits
2a007ba56c
...
v0.1.1
Author | SHA1 | Date | |
---|---|---|---|
6b4d131f4f | |||
51b4029a79 | |||
a1f804ac38 | |||
eb32ec1027 | |||
2625239dba | |||
44de668546 | |||
20cb665b33 | |||
9a6fc3475a | |||
d15a771e89 | |||
f8cb03bf88 | |||
b65cef96c3 | |||
b511c14cc3 | |||
122cd5ed04 | |||
3b8bcb4c8a | |||
e7e7a247d8 | |||
eb2a0a9122 | |||
46e3e9da85 | |||
5bb51fb90c | |||
5da734e06d | |||
925ef4df4b | |||
79cd6dab8d | |||
28ee686295 | |||
5e0bbbec4f | |||
05f3ebc178 | |||
a34f2309a8 | |||
66b227fb63 | |||
373c3c3385 | |||
624ea575cc | |||
ffae02b62b | |||
81b976bcc7 | |||
0b67cb317e | |||
098f7fe01b | |||
76fc61a19d | |||
94d1d99af2 | |||
7feac0b5f7 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
ccat
|
||||
*.zip
|
||||
|
||||
|
73
README.md
Normal file
73
README.md
Normal file
@@ -0,0 +1,73 @@
|
||||
## ccat
|
||||
|
||||
ccat is a file printing tool (like 'cat') which uses Regular Expressions to enable syntax highlighting.
|
||||
|
||||
---
|
||||
|
||||
### Features
|
||||
- 11 colors are defined out-of-the-box: RED, BLUE, GREEN, MAGENTA, CYAN, BLACK, WHITE, YELLOW, GRAY, ORANGE and DARKBLUE.
|
||||
- Support for defining custom colors via the `ccat.colors` file.
|
||||
- Regex-color mappings are stored in configuration files.
|
||||
- Uses the file extension to determine which configuration file to use.
|
||||
- Highly extensible - to add a config file for an specific file type, name the file `<extension>.conf`.
|
||||
- Support for printing line numbers with the `-n` flag.
|
||||
- Statically linked Go binary - no runtime dependencies, config files are distributed along with the binary.
|
||||
- Linux and MacOS supported.
|
||||
|
||||
---
|
||||
|
||||
### Installing
|
||||
|
||||
Download the appropriate zip-file from the 'Releases' section. Place the executable in your PATH.
|
||||
|
||||
NOTE: The releases are not available on the GitHub repo (which is a mirror of https://gitea.twomorecents.org/Rockingcool/ccat). Obtain the [releases](https://gitea.twomorecents.org/Rockingcool/ccat/releases) from there instead.
|
||||
|
||||
---
|
||||
|
||||
### Building from source
|
||||
|
||||
If you have the `go` command installed, run `make` after cloning the repository.
|
||||
|
||||
---
|
||||
|
||||
### Supported Languages
|
||||
|
||||
The following languages have config files included by default:
|
||||
|
||||
- C
|
||||
- Go
|
||||
|
||||
---
|
||||
|
||||
### Getting Started
|
||||
The config files are embedded within the binary. They will automatically be installed to the correct location (`~/.config/ccat` on UNIX) when the program is first run.
|
||||
|
||||
As written above, if provided a file with extension `.example`, the program will look for the config file named `example.conf`. If such a file doesn't exist, the file is printed out without any highlighting.
|
||||
|
||||
For example, if you want to create syntax highlighting for Java, create a file named `java.conf` in your config directory. In this file, include regular-expressions for each of the langauges's keywords, and provide a corresponding color. Use the provided `c.conf` and `go.conf` files as a starting point.
|
||||
|
||||
---
|
||||
|
||||
### Config Files
|
||||
|
||||
The config files are written in YAML. Each line has the following syntax:
|
||||
|
||||
`"<regex>": COLOR`
|
||||
|
||||
Note that the regex must be enclosed in double quotes, and the color must be capitalized.
|
||||
|
||||
---
|
||||
|
||||
### Custom Colors
|
||||
|
||||
To define a color of your own, create a file named `ccat.colors` in the config directory (mentioned above). The syntax of this file is the following:
|
||||
|
||||
`COLOR: <red> <green> <blue>`
|
||||
|
||||
Note that the color name must be capitalized (and shouldn't contain spaces). The RGB values must each be from 0 to 255.
|
||||
|
||||
---
|
||||
|
||||
### TODO:
|
||||
- Windows support.
|
||||
- Allow users to provide a config file in the command-line, overriding the extension-based config file.
|
101
color.go
101
color.go
@@ -2,8 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
colorData "github.com/fatih/color"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// A color represents a possible color, which text can be printed out in.
|
||||
@@ -14,6 +18,14 @@ type color struct {
|
||||
colorObj *colorData.Color
|
||||
}
|
||||
|
||||
// A RGB represents a Red, Blue, Green trio of values. Each value is represented as
|
||||
// an int.
|
||||
type RGB struct {
|
||||
red int
|
||||
blue int
|
||||
green int
|
||||
}
|
||||
|
||||
// The following is a list of all possible colors, stored in a map.
|
||||
var possibleColors map[string]color = map[string]color{
|
||||
"BLACK": {"BLACK", colorData.New(colorData.FgBlack)},
|
||||
@@ -27,6 +39,7 @@ var possibleColors map[string]color = map[string]color{
|
||||
"GRAY": {"GRAY", colorData.New(colorData.FgWhite, colorData.Faint)},
|
||||
// Last three numbers are RGB. Reference https://en.wikipedia.org/wiki/ANSI_escape_code for what the first two numbers mean.
|
||||
"ORANGE": {"ORANGE", colorData.New(38, 2, 255, 153, 28)},
|
||||
"DARKBLUE": {"DARKBLUE", colorData.New(38, 2, 0, 112, 255)},
|
||||
"NONE": {"NONE", colorData.New()},
|
||||
}
|
||||
|
||||
@@ -52,9 +65,95 @@ func newColor(colorString string) (color, error) {
|
||||
// newColorMust is similar to newColor, but prints an error and exits if the given color isn't valid.
|
||||
func newColorMust(colorString string) color {
|
||||
if clr, err := newColor(colorString); err != nil {
|
||||
printAndExit(err.Error())
|
||||
printErrAndExit(err.Error())
|
||||
panic(err) // NEVER REACHED
|
||||
} else {
|
||||
return clr
|
||||
}
|
||||
}
|
||||
|
||||
// isValidColorName returns true if the given string only contains uppercase alphabetic
|
||||
// characters.
|
||||
func isValidColorName(colorName string) bool {
|
||||
for _, ch := range colorName {
|
||||
if ch > 'Z' || ch < 'A' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// stringToRGB takes a string representing an RGB trio. It constructs and RGB type and
|
||||
// returns it. Any errors encountered are returned. If an error is returned, it is safe to
|
||||
// assume that the string doesn't represent an RGB trio.
|
||||
func stringToRGB(rgbString string) (*RGB, error) {
|
||||
values := strings.Split(rgbString, " ")
|
||||
// There must be three space-separated strings.
|
||||
if len(values) != 3 {
|
||||
// TODO: Instead of ignoring these errors and returning a generic error (as I do in the
|
||||
// callee), wrap the error returned from this function, inside the error returned by the callee.
|
||||
return nil, fmt.Errorf("Error parsing RGB trio.")
|
||||
}
|
||||
// If any of the strings doesn't represent an integer (or is out of bounds), return an error.
|
||||
// WARNING: LAZY CODE INCOMING
|
||||
var toReturn RGB
|
||||
var err error
|
||||
toReturn.red, err = strconv.Atoi(values[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error parsing RED integer: Invalid value.")
|
||||
}
|
||||
if toReturn.red < 0 || toReturn.red > 255 {
|
||||
return nil, fmt.Errorf("Error parsing RED integer: Out-of-bounds.")
|
||||
}
|
||||
toReturn.blue, err = strconv.Atoi(values[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error parsing BLUE integer: Invalid value.")
|
||||
}
|
||||
if toReturn.blue < 0 || toReturn.blue > 255 {
|
||||
return nil, fmt.Errorf("Error parsing BLUE integer: Out-of-bounds.")
|
||||
}
|
||||
toReturn.green, err = strconv.Atoi(values[2])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error parsing GREEN integer: Invalid value.")
|
||||
}
|
||||
if toReturn.green < 0 || toReturn.green > 255 {
|
||||
return nil, fmt.Errorf("Error parsing GREEN integer: Out-of-bounds.")
|
||||
}
|
||||
return &toReturn, nil
|
||||
}
|
||||
|
||||
// loadColorsFromFile loads the colors defined in the given config file, and adds them to
|
||||
// the possibleColors map. This allows the user to define custom colors at run-time.
|
||||
// The colors config file has the following syntax:
|
||||
// COLOR: <RED> <GREEN> <BLUE>
|
||||
//
|
||||
// Note that the color must be capitalized (and not contain spaces), and the R, G and B
|
||||
// values must be from 0 to 255.
|
||||
func loadColorsFromFile(filepath string) error {
|
||||
data, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Read color config file into a MapSlice
|
||||
tempMapSlice := yaml.MapSlice{}
|
||||
if err := yaml.Unmarshal(data, &tempMapSlice); err != nil {
|
||||
return fmt.Errorf("Unable to read color config file: %s", filepath)
|
||||
}
|
||||
|
||||
for _, item := range tempMapSlice {
|
||||
if !(isValidColorName(item.Key.(string))) {
|
||||
return fmt.Errorf("Invalid color name: %s", item.Key.(string))
|
||||
}
|
||||
var rgb *RGB
|
||||
if rgb, err = stringToRGB(item.Value.(string)); err != nil {
|
||||
return fmt.Errorf("Invalid RGB trio: %s", item.Value.(string))
|
||||
}
|
||||
// If we haven't returned an error yet, the color must be valid.
|
||||
// Add it to the map. colorData.New() expects values of type colorData.Attribute,
|
||||
// so we must cast our RGB values accordingly.
|
||||
possibleColors[item.Key.(string)] = color{item.Key.(string), colorData.New(38, 2, colorData.Attribute(rgb.red), colorData.Attribute(rgb.blue), colorData.Attribute(rgb.green))}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
55
config.go
55
config.go
@@ -1,14 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"ccat/stack"
|
||||
"embed"
|
||||
"errors"
|
||||
"gitea.twomorecents.org/Rockingcool/ccat/stack"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
//go:embed config
|
||||
var storedConfigs embed.FS // Embed the folder containing config files
|
||||
|
||||
// runningOnWindows: At the moment this function isn't used. When Window support is added,
|
||||
// it will be used to determine if the program is being run on Windows.
|
||||
func runningOnWindows() bool {
|
||||
return runtime.GOOS == "windows"
|
||||
}
|
||||
|
||||
// generateDefaultConfigs is used to generate a folder of default config files
|
||||
// for common languages. These default config files are embedded into the program, and will
|
||||
// be outputted into the given directory.
|
||||
//
|
||||
// If there is an error encountered, the error is returned.
|
||||
func generateDefaultConfigs(configOutputPath string) error {
|
||||
err := os.MkdirAll(configOutputPath, 0755)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
return errors.New("Directory already exists.")
|
||||
} else {
|
||||
return errors.New("Unable to create directory.")
|
||||
}
|
||||
}
|
||||
|
||||
// Copy each folder from the embedded filesystem, into the destination path
|
||||
err = fs.WalkDir(storedConfigs, "config", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() { // Skip directories
|
||||
return nil
|
||||
}
|
||||
relPath, _ := filepath.Rel("config", path)
|
||||
dstPath := filepath.Join(configOutputPath, relPath) // Destination path
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(dstPath, data, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// loadConfig takes in the filename of a config file. It reads the file,
|
||||
// and returns a stack of RegColors, with the item at the bottom being the one that
|
||||
// was read first. This ensures that, _when accessing the RegColors in the stack, the last
|
||||
|
30
config/go.conf
Normal file
30
config/go.conf
Normal file
@@ -0,0 +1,30 @@
|
||||
# Priority decreases going downward ie. If two regexes match the same piece of
|
||||
# text, the one defined earlier will take precedence over the one defined later.
|
||||
# Comments
|
||||
'//.*': GRAY
|
||||
'/\*[^*]*\*+(?:[^/*][^*]*\*+)*/': GRAY
|
||||
# Numbers and special values
|
||||
'\b\-?[0-9]*\b': MAGENTA
|
||||
'\b(true|false)\b': MAGENTA
|
||||
'\b(nil)\b': MAGENTA
|
||||
# Strings in double quotes and backticks
|
||||
'"(.*?)"': BLUE
|
||||
'`(.*?)`': BLUE
|
||||
# Bytes / Runes
|
||||
"'(.)'": BLUE
|
||||
"'\\\\(.)'": BLUE # The escape backslash needs to be escaped as well
|
||||
# Assignments and comparisons
|
||||
'(?:\s|\b)(=|==|!=|<=|>=)(\s|\b)' : CYAN
|
||||
'(&&)|(\|\|)': CYAN
|
||||
# Keywords
|
||||
'\b(if|else|for|range|go|func|return|break|continue)\b': CYAN
|
||||
'\b(import|var|const|type|struct)\b': CYAN
|
||||
# Built-in Functions
|
||||
'\b(panic|len)\b': DARKBLUE
|
||||
# Functions from packages (package name and function name separated by dot)
|
||||
'\b(\w*\.\w*)\b': DARKBLUE
|
||||
# Data Types
|
||||
'\b(bool|byte|rune|string|interface|map|chan)\b': YELLOW
|
||||
'\b(u?int)(8|16|32|64)?\b': YELLOW
|
||||
'\b(float)(32|64)\b': YELLOW
|
||||
'\b(complex)(64|128)\b': YELLOW
|
17
create_release_builds.sh
Executable file
17
create_release_builds.sh
Executable file
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
POSSIBLE_GOOS=( "linux" "darwin" )
|
||||
POSSIBLE_GOARCH=( "amd64" "arm64" )
|
||||
|
||||
for OS in "${POSSIBLE_GOOS[@]}"; do
|
||||
for ARCH in "${POSSIBLE_GOARCH[@]}"; do
|
||||
FOLDER_NAME="ccat-$OS-$ARCH"
|
||||
mkdir "${FOLDER_NAME}"
|
||||
GOOS=$OS GOARCH=$ARCH go build -o "${FOLDER_NAME}/"
|
||||
zip -r "${FOLDER_NAME}" "${FOLDER_NAME}"
|
||||
rm -r "${FOLDER_NAME}"
|
||||
done
|
||||
done
|
||||
|
@@ -5,7 +5,7 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func printAndExit(errorStr string) {
|
||||
func printErrAndExit(errorStr string) {
|
||||
fmt.Printf("ERROR: %s\n", errorStr)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
||||
module ccat
|
||||
module gitea.twomorecents.org/Rockingcool/ccat
|
||||
|
||||
go 1.22.5
|
||||
|
||||
|
98
main.go
98
main.go
@@ -1,9 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
@@ -15,7 +19,7 @@ func fileExists(filename string) bool {
|
||||
} else if errors.Is(err, os.ErrNotExist) {
|
||||
return false
|
||||
} else {
|
||||
printAndExit(err.Error())
|
||||
printErrAndExit(err.Error())
|
||||
return false // NEVER REACHED
|
||||
}
|
||||
}
|
||||
@@ -24,7 +28,7 @@ func fileExists(filename string) bool {
|
||||
// the file doesn't exist.
|
||||
func mustExist(filename string) {
|
||||
if fileExists(filename) != true {
|
||||
printAndExit(os.ErrNotExist.Error())
|
||||
printErrAndExit(os.ErrNotExist.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,12 +36,12 @@ func mustExist(filename string) {
|
||||
// It returns two values: the first is true if the config file exists.
|
||||
// If it does, the second value is the config filename.
|
||||
// If it doesn't, the second value is blank and can be ignored.
|
||||
func getConfig(extension string) (bool, string) {
|
||||
func getConfig(configPath, extension string) (bool, string) {
|
||||
if extension == "" {
|
||||
return false, ""
|
||||
}
|
||||
// Assuming the file has an extension
|
||||
fileName := "config/" + extension[1:] + ".conf"
|
||||
fileName := filepath.Join(configPath, extension[1:]+".conf")
|
||||
if exists := fileExists(fileName); exists == false {
|
||||
return false, ""
|
||||
} else {
|
||||
@@ -52,41 +56,89 @@ func printFile(fileName string) {
|
||||
mustExist(fileName)
|
||||
data, err := os.ReadFile(fileName)
|
||||
if err != nil {
|
||||
printAndExit(err.Error())
|
||||
printErrAndExit(err.Error())
|
||||
}
|
||||
fmt.Print(string(data))
|
||||
return
|
||||
}
|
||||
|
||||
// computeLineNumDigits computes the number of digits in the number of lines
|
||||
// in the given byte array.
|
||||
func computeLineNumDigits(data []byte) int {
|
||||
numLines := bytes.Count(data, []byte{'\n'}) + 1
|
||||
return int(math.Round(math.Log10(float64(numLines))))
|
||||
}
|
||||
|
||||
func main() {
|
||||
disableColorFlag := flag.Bool("d", false, "Disable color")
|
||||
lineNumberFlag := flag.Bool("n", false, "Print line numbers")
|
||||
// Used only if lineNumberFlag is true
|
||||
var lineNumDigits int
|
||||
var lineNum int
|
||||
flag.Parse()
|
||||
|
||||
// Check if config exists. If it doesn't, generate the config files.
|
||||
currentUser, err := user.Current() // Get current user, to determine config path
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
configPath := filepath.Join("/home/" + currentUser.Username + "/.config/ccat/")
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
generateDefaultConfigs(configPath)
|
||||
}
|
||||
|
||||
// Check if user has provided a file name
|
||||
if len(os.Args) != 2 {
|
||||
printAndExit("Invalid number of arguments")
|
||||
if len(flag.Args()) < 1 {
|
||||
printErrAndExit("No File specified")
|
||||
}
|
||||
fileName := os.Args[1]
|
||||
fileName := flag.Args()[0]
|
||||
|
||||
// Check if file exists.
|
||||
mustExist(fileName)
|
||||
|
||||
extension := filepath.Ext(fileName)
|
||||
configExists, configFilename := getConfig(extension)
|
||||
// If the given file has no corresponding config, print it out
|
||||
// and exit.
|
||||
configExists, configFilename := getConfig(configPath, extension)
|
||||
// If the given file has no corresponding config, print the file out and exit.
|
||||
if configExists == false {
|
||||
printFile(fileName)
|
||||
return
|
||||
}
|
||||
|
||||
// To save computing time, determine here if the file is empty. If it is, exit
|
||||
// the program.
|
||||
finfo, err := os.Stat(fileName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if finfo.Size() == 0 {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Assuming the file is not empty...
|
||||
// If a ccat.colors file exists in the config directory, load all the colors in it
|
||||
if fileExists(filepath.Join(configPath, "ccat.colors")) {
|
||||
err := loadColorsFromFile(filepath.Join(configPath, "ccat.colors"))
|
||||
if err != nil {
|
||||
printErrAndExit(err.Error())
|
||||
}
|
||||
}
|
||||
// If the given file has a config, load the config into a stack of regColors.
|
||||
regColorStack, err := loadConfig(configFilename)
|
||||
if err != nil {
|
||||
printAndExit(err.Error())
|
||||
printErrAndExit(err.Error())
|
||||
}
|
||||
|
||||
// Load the input file into a colorunit slice (units) and a byte slice (data)
|
||||
units, data, err := loadInputFile(fileName)
|
||||
if err != nil {
|
||||
printAndExit(err.Error())
|
||||
printErrAndExit(err.Error())
|
||||
}
|
||||
|
||||
// If the '-n' flag is set, compute the number of digits in the number of lines
|
||||
// in the file, to determine the padding for the line numbers.
|
||||
if *lineNumberFlag {
|
||||
lineNumDigits = computeLineNumDigits(data)
|
||||
}
|
||||
// For each regular expression in the stack, apply it to the byte slice. Find
|
||||
// the first and last index of all matches of the regex. Then apply the corresponding color
|
||||
// to every character within these indices.
|
||||
@@ -94,7 +146,9 @@ func main() {
|
||||
// The infinite for loop exists, because I couldn't figure out a way to pop an element from
|
||||
// the stack inside the 'for' statement. The loop exits when the 'pop' call returns 'false',
|
||||
// indicating that the stack is empty.
|
||||
for {
|
||||
//
|
||||
// The loop is also only run if the 'disable color' flag is not set.
|
||||
for *disableColorFlag == false {
|
||||
regclr, ok := regColorStack.Pop()
|
||||
// regColorStack.Pop() returns false when there are no more elements to pop
|
||||
if ok != true {
|
||||
@@ -115,7 +169,21 @@ func main() {
|
||||
}
|
||||
|
||||
// After all possible regexes have been matched, print out the contents of 'units'.
|
||||
for _, unit := range units {
|
||||
|
||||
// If the line number flag is set, initialize the lineNum variable and print the first line number
|
||||
// with the appropriate padding.
|
||||
if *lineNumberFlag {
|
||||
lineNum = 1
|
||||
fmt.Printf(" %*d ", lineNumDigits, lineNum)
|
||||
lineNum++
|
||||
}
|
||||
for idx, unit := range units {
|
||||
unit.print()
|
||||
// If the flag is set and we encounter a newline (and the newline isn't a trailing newline),
|
||||
// then print the next line number.
|
||||
if *lineNumberFlag && unit.ch == '\n' && idx != len(units)-1 {
|
||||
fmt.Printf(" %*d ", lineNumDigits, lineNum)
|
||||
lineNum++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
173
test.c
173
test.c
@@ -1,173 +0,0 @@
|
||||
//go:build exclude
|
||||
#include "easysock.h"
|
||||
|
||||
|
||||
int create_socket(int network, char transport) {
|
||||
int domain;
|
||||
int type;
|
||||
|
||||
if (network == 4) {
|
||||
domain = AF_INET;
|
||||
} else if (network == 6) {
|
||||
domain = AF_INET6;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (transport == 'T') {
|
||||
type = SOCK_STREAM;
|
||||
} else if (transport == 'U') {
|
||||
type = SOCK_DGRAM;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int newSock = socket(domain,type,0);
|
||||
return newSock;
|
||||
}
|
||||
|
||||
|
||||
int create_addr(int network, char* address, int port,struct sockaddr* dest) {
|
||||
if (network == 4) {
|
||||
struct sockaddr_in listen_address;
|
||||
|
||||
listen_address.sin_family = AF_INET;
|
||||
listen_address.sin_port = htons(port);
|
||||
inet_pton(AF_INET,address,&listen_address.sin_addr);
|
||||
memcpy(dest,&listen_address,sizeof(listen_address));
|
||||
return 0;
|
||||
|
||||
} else if (network == 6) {
|
||||
struct sockaddr_in6 listen_ipv6;
|
||||
listen_ipv6.sin6_family = AF_INET6;
|
||||
listen_ipv6.sin6_port = htons(port);
|
||||
inet_pton(AF_INET6,address,&listen_ipv6.sin6_addr);
|
||||
memcpy(dest,&listen_ipv6,sizeof(listen_ipv6));
|
||||
return 0;
|
||||
|
||||
} else {
|
||||
return -202;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) {
|
||||
int socket = create_socket(network,transport);
|
||||
if (socket < 0) {
|
||||
return (-1 * errno);
|
||||
}
|
||||
create_addr(network,address,port,addr_struct);
|
||||
int addrlen;
|
||||
if (network == 4) {
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
} else if (network == 6) {
|
||||
addrlen = sizeof(struct sockaddr_in6);
|
||||
} else {
|
||||
return -202;
|
||||
}
|
||||
|
||||
/* The value of addrlen should be the size of the 'sockaddr'.
|
||||
This should be set to the size of 'sockaddr_in' for IPv4, and 'sockaddr_in6' for IPv6.
|
||||
See https://stackoverflow.com/questions/73707162/socket-bind-failed-with-invalid-argument-error-for-program-running-on-macos */
|
||||
|
||||
int i = bind (socket,addr_struct,(socklen_t)addrlen);
|
||||
if (i < 0) {
|
||||
return (-1 * errno);
|
||||
}
|
||||
return socket;
|
||||
}
|
||||
|
||||
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct) {
|
||||
|
||||
struct addrinfo hints; /* Used to tell getaddrinfo what kind of address we want */
|
||||
struct addrinfo* results; /* Used by getaddrinfo to store the addresses */
|
||||
|
||||
|
||||
if (check_ip_ver(address) < 0) { /* If the address is a domain name */
|
||||
int err_code;
|
||||
char* port_str = malloc(10 * sizeof(char));
|
||||
|
||||
sprintf(port_str,"%d",port); /* getaddrinfo expects a string for its port */
|
||||
|
||||
|
||||
memset(&hints,'\0',sizeof(hints));
|
||||
hints.ai_socktype = char_to_socktype(transport);
|
||||
|
||||
err_code = getaddrinfo(address,port_str,&hints,&results);
|
||||
if (err_code != 0) {
|
||||
return (-1 * err_code);
|
||||
}
|
||||
remote_addr_struct = results->ai_addr;
|
||||
network = inet_to_int(results->ai_family);
|
||||
} else {
|
||||
create_addr(network,address,port,remote_addr_struct);
|
||||
}
|
||||
|
||||
int socket = create_socket(network,transport);
|
||||
if (socket < 0) {
|
||||
return (-1 * errno);
|
||||
}
|
||||
|
||||
int addrlen;
|
||||
if (network == 4) {
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
} else if (network == 6) {
|
||||
addrlen = sizeof(struct sockaddr_in6);
|
||||
} else {
|
||||
return (-202);
|
||||
}
|
||||
|
||||
/* The value of addrlen should be the size of the 'sockaddr'.
|
||||
This should be set to the size of 'sockaddr_in' for IPv4, and 'sockaddr_in6' for IPv6.
|
||||
See https://stackoverflow.com/questions/73707162/socket-bind-failed-with-invalid-argument-error-for-program-running-on-macos */
|
||||
|
||||
int i = connect(socket,remote_addr_struct,(socklen_t)addrlen);
|
||||
if (i < 0) {
|
||||
return (-1 * errno);
|
||||
}
|
||||
return socket;
|
||||
}
|
||||
|
||||
|
||||
int check_ip_ver(char* address) {
|
||||
char buffer[16]; /* 16 chars - 128 bits - is enough to hold an ipv6 address */
|
||||
if (inet_pton(AF_INET,address,buffer) == 1) {
|
||||
return 4;
|
||||
} else if (inet_pton(AF_INET6,address,buffer) == 1) {
|
||||
return 6;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int int_to_inet(int network) {
|
||||
if (network == 4) {
|
||||
return AF_INET;
|
||||
} else if (network == 6) {
|
||||
return AF_INET6;
|
||||
} else {
|
||||
return -202;
|
||||
}
|
||||
}
|
||||
|
||||
int inet_to_int(int af_type) {
|
||||
if (af_type == AF_INET) {
|
||||
return 4;
|
||||
} else if (af_type == AF_INET6) {
|
||||
return 6;
|
||||
} else {
|
||||
return -207;
|
||||
}
|
||||
}
|
||||
|
||||
int char_to_socktype(char transport) {
|
||||
if (transport == 'T') {
|
||||
return SOCK_STREAM;
|
||||
} else if (transport == 'U') {
|
||||
return SOCK_DGRAM;
|
||||
} else {
|
||||
return -250;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user