Skip to content

Instantly share code, notes, and snippets.

@masami256
Created May 26, 2013 03:39
Show Gist options
  • Select an option

  • Save masami256/5651642 to your computer and use it in GitHub Desktop.

Select an option

Save masami256/5651642 to your computer and use it in GitHub Desktop.
TCP FASTOPENテストコード
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#define SERVER_ADDR "127.0.0.1"
#define SERVER_PORT 1111
// Basically MSG_XXX are, such as MSG_PEEK, defined in /usr/include/bits/socket.h
// but glibc-headers-2.16-31.fc18.x86_64 does not define it.
#define MSG_FASTOPNE_VALUE 0x20000000
static int tfo_sendto(int sock);
static int tfo_sendmsg(int sock);
static int notfo_sendmsg(int sock);
typedef int (*send_data_func_t)(int sock);
static void
error_exit(void)
{
fprintf(stderr, "[-]Error: %s\n", strerror(errno));
exit(-1);
}
#define set_sockaddr_in(sin) \
do { \
sin.sin_family = AF_INET; \
sin.sin_addr.s_addr = htonl(INADDR_ANY); \
sin.sin_port = htons(SERVER_PORT); \
} while (0)
static int
tfo_sendto(int sock)
{
char buf[16] = "sendo";
struct sockaddr_in sin = { 0 };
socklen_t slen = sizeof(sin);
printf("[-]Start %s\n", __FUNCTION__);
set_sockaddr_in(sin);
return sendto(sock, buf, strlen(buf), MSG_FASTOPNE_VALUE,
(struct sockaddr *) &sin, slen);
}
#define set_msghdr(msg, iov, buf) \
do { \
iov[0].iov_base = buf; \
iov[0].iov_len = strlen(buf); \
msg.msg_iov = iov; \
msg.msg_iovlen = (sizeof(iov) / sizeof(iov[0])); \
} while (0)
static int
notfo_sendmsg(int sock)
{
struct iovec iov[1];
struct msghdr msg = { 0 };
struct sockaddr_in sin = { 0 };
socklen_t slen = sizeof(sin);
char buf[16] = "notfo sendmsg";
printf("[-]Start %s\n", __FUNCTION__);
set_msghdr(msg, iov, buf);
set_sockaddr_in(sin);
connect(sock, (struct sockaddr *) &sin, slen);
return sendmsg(sock, &msg, 0);
}
static int
tfo_sendmsg(int sock)
{
struct iovec iov[1];
struct msghdr msg = { 0 };
char buf[16] = "tfo sendmsg";
printf("[-]Start %s\n", __FUNCTION__);
set_msghdr(msg, iov, buf);
return sendmsg(sock, &msg, MSG_FASTOPNE_VALUE);
}
static void
tfo_test_start(send_data_func_t func)
{
int sock = 0;
int ret = 0;
char buf[32] = { 0 };
printf("[-]Start %s\n", __FUNCTION__);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
error_exit();
ret = func(sock);
if (ret < 0)
error_exit();
ret = recv(sock, buf, sizeof(buf), MSG_PEEK);
if (ret == -1)
error_exit();
printf("[-]Receve data is \n");
printf(" %s", buf);
}
int
main(int argc, char **argv)
{
send_data_func_t func = &tfo_sendto;
if (argc == 2) {
if (!strcmp("sendmsg", argv[1]))
func = &tfo_sendmsg;
else if (!strcmp("notfo_sendmsg", argv[1]))
func = &notfo_sendmsg;
}
tfo_test_start(func);
return 0;
}
#!/usr/bin/env ruby
require "socket"
def start_server(port = 1111)
sock = Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0)
# Defined in include/linux/tcp.h
# #define TCP_FASTOPEN 23 /* Enable FastOpen on listeners */
sock.setsockopt(Socket::SOL_TCP, 23, 5)
sock.bind(Addrinfo.tcp("127.0.0.1", port))
sock.listen(5)
while true
s = sock.accept
req = s[0].recv(64, Socket::MSG_PEEK)
s[0].printf "fuction is %s\n", req
s[0].close
end
sock.close
end
if __FILE__ == $0 then
start_server()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment