coardump
2005-03-15 10:32:56 UTC
Hi all, I was wondering if someone could look over the attached code
and tell me what I have done wrong, no doubt several things.
-bash-2.05b$ ./d 192.168.1.2 22 aaaaa
Connected!! Reading from the socket....!
Snarfed:
Data aaaaa
Size 6
Sending buffer....
Sent [5] bytes to [192.168.1.2]
Data sent -> [aaaaa]
Snarfed:
Data aaaaa
Size 6
-bash-2.05b$
As you can see, when I send 5 bytes, the code will recv 6 bytes, I
guess it's a NULL byte but I can't think of a way to remove it other
than hard coding the buffer limit, which sadly has to be dynamic as the
data being recv()'d is unknown in size. Can csomeone suggest how I can
a) sort this code to recv() correctly, by that I mean if I sent 5 'a's
it should show 5 'a's AND total '5' in the Sent area.
b) any other issues that glare at you?
Once I fix this issue I will malloc my storage areas too.
Appreciated,
coardump.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_RECV_BUF 1000
#define ERR 1
int banner(int argc, char **argv);
int socksnarf(int bytes, int sockfd, char *buf);
int socksend(int bytes, int sockfd, char *data, struct sockaddr_in
addr);
int main(int argc, char **argv);
int
banner(int argc, char **argv)
{
fprintf(stderr,
"\n+------------------------------------------");
fprintf(stderr, "\nSends data bulks to a remote TCP socket");
fprintf(stderr, "\nUsage: %s <ip> <port> <data>", argv[0]);
fprintf(stderr,
"\n+------------------------------------------");
fprintf(stderr, "\nExample:\n\t %s 192.168.1.1 23 `perl -e
'print \"A\" x 5000'`\n\n", argv[0]);
return(1);
}
int
socksnarf(int bytes, int sockfd, char *buf)
{
if((bytes = recv(sockfd, buf, MAX_RECV_BUF - 1, 0)) < 0) {
perror("recv");
_exit(ERR);
}
buf[bytes] = '\0';
fprintf(stderr, "Snarfed:\nData %s \nSize %d\n", buf, bytes);
return(0);
}
int
socksend(int bytes, int sockfd, char *data, struct sockaddr_in addr)
{
fprintf(stderr, "\nSending buffer....");
if((bytes = send(sockfd, data, strlen(data), 0)) == NULL) {
perror("send");
_exit(ERR);
}
printf("\nSent [%d] bytes to [%s]\n", strlen(data),
inet_ntoa(addr.sin_addr));
printf("Data sent -> [%s]\n\n", data);
return(0);
}
int
main(int argc, char **argv)
{
char *data = NULL;
char buf[1024];
int bytes, port, sockfd;
struct sockaddr_in addr;
struct hostent *host;
if(argc < 4) {
banner(argc, argv);
}
port = atoi(argv[2]);
if(!port) {
fprintf(stderr, "um, port seems to be NULL :/!\n");
_exit(ERR);
}
data = argv[3]; /* Did malloc data but removed it incase it
caused my issue, seems not */
if(!data) {
fprintf(stderr, "um, data pointer is NULL :/!\n");
_exit(ERR);
}
if((host = gethostbyname(argv[1])) == NULL) {
perror("gethostbyname");
exit(ERR);
}
bzero((char *)&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr((char*)argv[1]);
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
_exit(ERR);
}
if(connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("\nconnect");
_exit(ERR);
}else{
printf("\n\nConnected!! Reading from the
socket....!\n");
}
socksnarf(bytes, sockfd, buf);
if(!socksnarf) {
perror("socksnarf");
_exit(ERR);
}
sleep(2);
socksend(bytes, sockfd, data, addr);
if(!socksnarf) {
perror("socksnarf");
_exit(ERR);
}
sleep(2);
socksnarf(bytes, sockfd, buf);
if(!socksnarf) {
perror("socksnarf");
_exit(ERR);
}
close(sockfd);
if(!sockfd) {
perror("close");
_exit(ERR);
}
return(0);
}
and tell me what I have done wrong, no doubt several things.
-bash-2.05b$ ./d 192.168.1.2 22 aaaaa
Connected!! Reading from the socket....!
Snarfed:
Data aaaaa
Size 6
Sending buffer....
Sent [5] bytes to [192.168.1.2]
Data sent -> [aaaaa]
Snarfed:
Data aaaaa
Size 6
-bash-2.05b$
As you can see, when I send 5 bytes, the code will recv 6 bytes, I
guess it's a NULL byte but I can't think of a way to remove it other
than hard coding the buffer limit, which sadly has to be dynamic as the
data being recv()'d is unknown in size. Can csomeone suggest how I can
a) sort this code to recv() correctly, by that I mean if I sent 5 'a's
it should show 5 'a's AND total '5' in the Sent area.
b) any other issues that glare at you?
Once I fix this issue I will malloc my storage areas too.
Appreciated,
coardump.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_RECV_BUF 1000
#define ERR 1
int banner(int argc, char **argv);
int socksnarf(int bytes, int sockfd, char *buf);
int socksend(int bytes, int sockfd, char *data, struct sockaddr_in
addr);
int main(int argc, char **argv);
int
banner(int argc, char **argv)
{
fprintf(stderr,
"\n+------------------------------------------");
fprintf(stderr, "\nSends data bulks to a remote TCP socket");
fprintf(stderr, "\nUsage: %s <ip> <port> <data>", argv[0]);
fprintf(stderr,
"\n+------------------------------------------");
fprintf(stderr, "\nExample:\n\t %s 192.168.1.1 23 `perl -e
'print \"A\" x 5000'`\n\n", argv[0]);
return(1);
}
int
socksnarf(int bytes, int sockfd, char *buf)
{
if((bytes = recv(sockfd, buf, MAX_RECV_BUF - 1, 0)) < 0) {
perror("recv");
_exit(ERR);
}
buf[bytes] = '\0';
fprintf(stderr, "Snarfed:\nData %s \nSize %d\n", buf, bytes);
return(0);
}
int
socksend(int bytes, int sockfd, char *data, struct sockaddr_in addr)
{
fprintf(stderr, "\nSending buffer....");
if((bytes = send(sockfd, data, strlen(data), 0)) == NULL) {
perror("send");
_exit(ERR);
}
printf("\nSent [%d] bytes to [%s]\n", strlen(data),
inet_ntoa(addr.sin_addr));
printf("Data sent -> [%s]\n\n", data);
return(0);
}
int
main(int argc, char **argv)
{
char *data = NULL;
char buf[1024];
int bytes, port, sockfd;
struct sockaddr_in addr;
struct hostent *host;
if(argc < 4) {
banner(argc, argv);
}
port = atoi(argv[2]);
if(!port) {
fprintf(stderr, "um, port seems to be NULL :/!\n");
_exit(ERR);
}
data = argv[3]; /* Did malloc data but removed it incase it
caused my issue, seems not */
if(!data) {
fprintf(stderr, "um, data pointer is NULL :/!\n");
_exit(ERR);
}
if((host = gethostbyname(argv[1])) == NULL) {
perror("gethostbyname");
exit(ERR);
}
bzero((char *)&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr((char*)argv[1]);
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
_exit(ERR);
}
if(connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("\nconnect");
_exit(ERR);
}else{
printf("\n\nConnected!! Reading from the
socket....!\n");
}
socksnarf(bytes, sockfd, buf);
if(!socksnarf) {
perror("socksnarf");
_exit(ERR);
}
sleep(2);
socksend(bytes, sockfd, data, addr);
if(!socksnarf) {
perror("socksnarf");
_exit(ERR);
}
sleep(2);
socksnarf(bytes, sockfd, buf);
if(!socksnarf) {
perror("socksnarf");
_exit(ERR);
}
close(sockfd);
if(!sockfd) {
perror("close");
_exit(ERR);
}
return(0);
}