Skip to content

Commit

Permalink
cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
john-tornblom committed Jul 11, 2024
1 parent 7445c91 commit bb71119
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 33 deletions.
28 changes: 16 additions & 12 deletions cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ along with this program; see the file COPYING. If not, see
#include <sys/mount.h>

#include "cmd.h"
#include "log.h"


#define IOVEC_ENTRY(x) {x ? x : 0, \
Expand Down Expand Up @@ -418,8 +419,6 @@ ftp_cmd_NOOP(ftp_env_t *env, const char* arg) {
}




/**
* Establish a data connection with client.
**/
Expand Down Expand Up @@ -491,6 +490,7 @@ ftp_cmd_RETR(ftp_env_t *env, const char* arg) {
char pathbuf[PATH_MAX];
uint8_t buf[PAGE_SIZE];
struct stat st;
int err = 0;
int len;
int fd;

Expand All @@ -512,19 +512,22 @@ ftp_cmd_RETR(ftp_env_t *env, const char* arg) {
}

if(ftp_active_printf(env, "150 Opening data transfer\r\n")) {
return ftp_perror(env);
close(fd);
return -1;
}

if(ftp_data_open(env)) {
return ftp_perror(env);
err = ftp_perror(env);
close(fd);
return err;
}

while((len=read(fd, buf, sizeof(buf))) != 0) {
if(len < 0 || len != write(env->data_fd, buf, len)) {
int ret = ftp_perror(env);
err = ftp_perror(env);
ftp_data_close(env);
close(fd);
return ret;
return err;
}
}

Expand Down Expand Up @@ -631,6 +634,7 @@ int
ftp_cmd_STOR(ftp_env_t *env, const char* arg) {
uint8_t readbuf[0x4000];
char pathbuf[PATH_MAX];
int err = 0;
size_t len;
int fd;

Expand All @@ -649,17 +653,17 @@ ftp_cmd_STOR(ftp_env_t *env, const char* arg) {
}

if(ftp_data_open(env)) {
int ret = ftp_perror(env);
err = ftp_perror(env);
close(fd);
return ret;
return err;
}

while((len=ftp_data_read(env, readbuf, sizeof(readbuf)))) {
if(write(fd, readbuf, len) != len) {
int ret = ftp_perror(env);
err = ftp_perror(env);
ftp_data_close(env);
close(fd);
return ret;
return err;
}
}

Expand Down Expand Up @@ -712,9 +716,9 @@ ftp_cmd_USER(ftp_env_t *env, const char* arg) {
**/
int
ftp_cmd_KILL(ftp_env_t *env, const char* arg) {
puts("[ftpsrv.elf] Server killed");
FTP_LOG_PUTS("Server killed");
exit(EXIT_SUCCESS);
return 0;
return -1;
}


Expand Down
39 changes: 39 additions & 0 deletions log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Copyright (C) 2024 John Törnblom
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */

#pragma once

#include <stdio.h>
#include <ps5/klog.h>


/**
* Log to stdout and klog
**/
#define FTP_LOG_PUTS(s) { \
puts(s); \
klog_puts(s); \
}

#define FTP_LOG_PRINTF(s, ...) { \
printf(s, __VA_ARGS__); \
klog_printf(s, __VA_ARGS__); \
}

#define FTP_LOG_PERROR(s) { \
printf("%s:%d:%s: %s\n", __FILE__, __LINE__, s, strerror(errno)); \
klog_printf("%s:%d:%s: %s\n", __FILE__, __LINE__, s, strerror(errno)); \
}
46 changes: 25 additions & 21 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ along with this program; see the file COPYING. If not, see
#include <sys/sysctl.h>
#include <sys/syscall.h>

#include <ps5/klog.h>

#include "cmd.h"
#include "log.h"


/**
Expand Down Expand Up @@ -105,7 +104,7 @@ ftp_readline(int fd) {
char c;

if(!buffer) {
klog_perror("malloc");
FTP_LOG_PERROR("malloc");
return NULL;
}

Expand Down Expand Up @@ -137,7 +136,7 @@ ftp_readline(int fd) {
buffer_backup = buffer;
buffer = realloc(buffer, bufsize);
if(!buffer) {
klog_perror("realloc");
FTP_LOG_PERROR("realloc");
free(buffer_backup);
return NULL;
}
Expand Down Expand Up @@ -176,9 +175,15 @@ ftp_execute(ftp_env_t *env, char *line) {
**/
static int
ftp_greet(ftp_env_t *env) {
const char *msg = "220 Service is ready\r\n";
size_t len = strlen(msg);
char msg[0x100];
size_t len;

snprintf(msg, sizeof(msg),
"220-Welcome to ftpsrv.elf running on pid %d, compiled at %s %s\r\n",
getpid(), __DATE__, __TIME__);
strncat(msg, "220 Service is ready\r\n", sizeof(msg)-1);

len = strlen(msg);
if(write(env->active_fd, msg, len) != len) {
return -1;
}
Expand Down Expand Up @@ -255,7 +260,7 @@ ftp_serve(uint16_t port) {
int srvfd;

if(getifaddrs(&ifaddr) == -1) {
klog_perror("getifaddrs");
FTP_LOG_PERROR("getifaddrs");
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -284,7 +289,7 @@ ftp_serve(uint16_t port) {
continue;
}

klog_printf("Serving FTP on %s:%d (%s)\n", ip, port, ifa->ifa_name);
FTP_LOG_PRINTF("Serving FTP on %s:%d (%s)\n", ip, port, ifa->ifa_name);
ifaddr_wait = 0;
}

Expand All @@ -295,12 +300,12 @@ ftp_serve(uint16_t port) {
}

if((srvfd=socket(AF_INET, SOCK_STREAM, 0)) < 0) {
klog_perror("socket");
FTP_LOG_PERROR("socket");
return -1;
}

if(setsockopt(srvfd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0) {
klog_perror("setsockopt");
FTP_LOG_PERROR("setsockopt");
return -1;
}

Expand All @@ -310,20 +315,20 @@ ftp_serve(uint16_t port) {
server_addr.sin_port = htons(port);

if(bind(srvfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) != 0) {
klog_perror("bind");
FTP_LOG_PERROR("bind");
return -1;
}

if(listen(srvfd, 5) != 0) {
klog_perror("listen");
FTP_LOG_PERROR("listen");
return -1;
}

addr_len = sizeof(client_addr);

while(1) {
if((connfd=accept(srvfd, (struct sockaddr*)&client_addr, &addr_len)) < 0) {
klog_perror("accept");
FTP_LOG_PERROR("accept");
break;
}

Expand All @@ -346,17 +351,17 @@ find_pid(const char* name) {
uint8_t *buf;

if(sysctl(mib, 4, 0, &buf_size, 0, 0)) {
klog_perror("sysctl");
FTP_LOG_PERROR("sysctl");
return -1;
}

if(!(buf=malloc(buf_size))) {
klog_perror("malloc");
FTP_LOG_PERROR("malloc");
return -1;
}

if(sysctl(mib, 4, buf, &buf_size, 0, 0)) {
klog_perror("sysctl");
FTP_LOG_PERROR("sysctl");
free(buf);
return -1;
}
Expand Down Expand Up @@ -388,13 +393,12 @@ main() {

syscall(SYS_thr_set_name, -1, "ftpsrv.elf");

printf("Socket server was compiled at %s %s\n", __DATE__, __TIME__);
klog_printf("Socket server was compiled at %s %s\n", __DATE__, __TIME__);
FTP_LOG_PRINTF("FTP server was compiled at %s %s\n", __DATE__, __TIME__);

while((pid=find_pid("ftpsrv.elf")) > 0) {
if(kill(pid, SIGKILL)) {
klog_perror("kill");
_exit(-1);
FTP_LOG_PERROR("kill");
exit(EXIT_FAILURE);
}
sleep(1);
}
Expand All @@ -404,6 +408,6 @@ main() {
sleep(3);
}

return 0;
return EXIT_SUCCESS;
}

0 comments on commit bb71119

Please sign in to comment.