OS X doesn't offer the SO_PROTOCOL socket option which allows the caller to "...retrieve the socket type as an integer." (http://linux.die.net/man/7/socket)
In other words the following program builds and works under linux but won't compile under OS X:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char **argv)
{
int c, s, type, len;
len = sizeof(type);
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s < 0)
{
fprintf(stderr, "socket kaboom: %s\n", strerror(errno));
return 1;
}
if (getsockopt(s, SOL_SOCKET, SO_PROTOCOL, &type, &len) < 0)
{
fprintf(stderr, "getsosockopt kaboom: %s\n", strerror(errno));
return 1;
}
printf("socket type: %d\n", type);
return 0;
}
How to accomplish this under OS X?
The standard SO_TYPE socket option, which returns values like SOCK_STREAM (corresponding to TCP) and SOCK_DGRAM (corresponding to UDP), should suffice. With SCTP, SOCK_STREAM might correspond to TCP or SCTP and SO_PROTOCOL is useful to distinguish them, but MacOS X does not support SCTP.
Unix domain sockets do not use protocol numbers; therefore, SO_TYPE is the right choice there as well.
Related
The title says it all. On Linux, how to know a domain of a not connected, nor binded socket.
Here is the code for reference.
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
int domain;
socklen_t s_len;
if(sd < 0){
perror("socket DID NOT create");
return 1;
}
if(getsockopt(sd, SOL_SOCKET, SO_DOMAIN, &domain, &s_len) < 0){
perror("getsockopt failed");
return 2;
}
const char *so_domain = NULL;
if(domain == AF_UNIX)
so_domain = "AF_UNIX";
printf("sock domain is: %d : %s\n", domain, so_domain);
return 0;
}
The code returns zero for the domain.
You are passing an uninitialised value of s_len to getsockopt(). From the manpage:
For getsockopt(), optlen is a value-result argument, initially containing the size
of the buffer pointed to by optval, and modified on return to indicate
the actual size of the value returned.
Changing it to:
socklen_t s_len = sizeof domain;
fixes it. Output:
sock domain is: 1 : AF_UNIX
My network consists of the GL-MT300N-V2 router running this code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <time.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int sock;
if( (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
perror("socket : ");
return -1;
}
int broadcast = 1;
if( setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) != 0 )
{
perror("setsockopt BROADCAST: ");
close(sock);
return -1;
}
char netif[] = "eth0";
if( setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, netif, sizeof(netif) )
{
perror("setsockopt BIND : ");
close(sock);
return -1;
};
char *ip = "255.255.255.255";
char * msg = "OOO"; //Actual code has 250 'O's to be visible in the dump
struct sockaddr_in si;
si.sin_family = AF_INET;
si.sin_port = htons( 4444 );
inet_aton( ip, &si.sin_addr.s_addr );
/* send data */
for(int i = 0; i < 10000; i ++) //Used to spam for tcpdump
{
size_t nBytes = sendto(sock, msg, strlen(msg), 0,
(struct sockaddr*) &si, sizeof(si));
if(i % 1000 == 0)
{
printf("Sent msg: %s, %d bytes with socket %d to %s\n", msg, nBytes, sock, ip);
}
}
return 0;
}
The code successfully continually and rapidly sends packets. Said packets are easily visible in TCPdump. However, when I connect the router to a Windows 10 PC or laptop using ethernet, the computers are unable to detect the packets with Wireshark.
The sheer amount of packets is likely not the problem, as other code I have run sends similar amounts of packets, all of which are received.
Windows firewall has also been disabled, so the packet isn't being dropped there either.
It could be an interface optimization--drop it in hardware, so to speak (assuming port 0x4444 is not LISTENING--check netstat). For obvious reasons, you do not want to send all bcasts up the stack.
I've been a Unix programmer for a decent amount of time, and I know pretty well the system's socket API, I use it mostly for networking.
The thing is that I'm currently trying to create a cross-platform software, and so I began to learn how to compile my source code into windows executable files.
I've created a startup() function which does the simplest thing: connect to a server (of which IP is given), and return -1 on failure. On Linux my code runs smooth, but on Windows(7) I get the same error on some machines - "Connect()" function failed, errno: "Result too large" which (by what I understood) means it could no find any listening server on that IP, but on others it runs well.
Here is the code (win version):
#define _WIN32_WINNT 0x0501
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <windows.h>
#include <winsock.h>
#define ADDR "127.127.127.127"
#define PORT 8752
int startup(struct sockaddr_in sin)
{
int sockfd, soaddr;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
fprintf(stderr, "socket:%s\n", strerror(errno));
return(-1);
}
soaddr = sizeof(sin);
if(connect(sockfd, (struct sockaddr*)&sin, soaddr) == -1)
{
fprintf(stderr, "connect:%s\n", strerror(errno));
return(-1);
}
return sockfd;
}
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(ADDR);
sockfd = startup(sin);
// Code continues
return(0);
}
note:ADDR has been changed for privacy reasons, but there is a working server on that machine
So, why exactly am I getting this error ?
WinSock does not use errno, it uses WSAGetLastError() instead.
WinSock does not use file descriptors for sockets, it uses actual kernel objects. On Windows, you need to use the SOCKET handle type, not int, for your socket variables (or at least type-cast SOCKET values to (u)intptr_t and then type-cast back to SOCKET when calling WinSock functions).
You can use the Win32 API FormatMessage() function to get a human readable string for a WinSock error code.
#define _WIN32_WINNT 0x0501
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <windows.h>
#include <winsock.h>
#define ADDR "127.127.127.127"
#define PORT 8752
__declspec(thread) char errmsg[256];
char* sockerr(int errcode)
{
DWORD len = FormatMessageA(FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, 0, errmsg, 255, NULL);
if (len != 0)
errmsg[len] = 0;
else
sprintf(errmsg, "error %d", errcode);
return errmsg;
}
int startup()
{
WSADATA wsaData;
int ret = WSAStartup(MAKEWORD(1,1), &wsaData);
if (ret != 0) {
fprintf(stderr, "WSAStartup:%s\n", sockerr(ret));
return(ret);
}
return(0);
}
void cleanup()
{
WSACleanup();
}
SOCKET connectToServer(struct sockaddr_in sin)
{
SOCKET sockfd;
int soaddr;
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == INVALID_SOCKET)
{
fprintf(stderr, "socket:%s\n", sockerr(WSAGetLastError()));
return(INVALID_SOCKET);
}
soaddr = sizeof(sin);
if (connect(sockfd, (struct sockaddr*)&sin, soaddr) == -1)
{
fprintf(stderr, "connect:%s\n", sockerr(WSAGetLastError()));
closesocket(sockfd);
return(INVALID_SOCKET);
}
return(sockfd);
}
int main(int argc, char *argv[])
{
SOCKET sockfd;
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(ADDR);
if (startup() != 0) {
exit(1);
}
sockfd = connectToServer(sin);
if (sockfd != INVALID_SOCKET)
{
// ...
closesocket(sockfd);
}
cleanup();
return(0);
}
You will have to take these differences into consideration when writing cross-platform socket code.
According to your code you are running client socket in windows machine.You need to load runtime libraries for windows as
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
You can refer to Windows Client Socket Source. Hope this works for you.
Good evening to all,
For kicks and giggles I'm trying my hand at *NIX sockets and TCP/IP. Now, to get off the ground I'm simply trying to create a socket on two endpoints and a basic text chat program back and forth. Now, before I'm even up and running I'm hit with a bind 'Invalid Argument':
user#user-VirtualBox:~/sockets$ ./socket
sock=3
s_->sin_family = 2
s_->sin_port = 3879
s_->sin_addr.s_addr = 0
sockfd = 3
s_->sin_family = 2
s_->sin_port = 3879
s_->sin_addr.s_addr = 0
Socket bind error: Invalid argument
sizeof(s_) = 8
Code below. so, INADDR_ANY should be 255.255.225.255 = 0, from what I understand; AF_INET is 2; and sin_port, well, I've looked at the binary backward and forward and am not sure I understand how 9000 is represented in host order at 3879 from 9000, but assume it's a non-issue. Additionally, since 1 is stdout and 2 is stderr, I assume that anything above this is dynamically allocated and so 3 should be fine for the socket file descriptor.
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
void setSocket(struct sockaddr_in* s_){
s_->sin_family=AF_INET;
s_->sin_port=htons(9999);
s_->sin_addr.s_addr=INADDR_ANY;
memset(&(s_->sin_zero), '\0', 8);
printf("s_->sin_family = %i\n", s_->sin_family);
printf("s_->sin_port = %i\n", s_->sin_port);
printf("s_->sin_addr.s_addr= %i\n", s_->sin_addr.s_addr);
}
void createSocket(int *sock){
if ((*sock = socket(AF_INET, SOCK_STREAM, 0)) == -1){
fprintf(stderr, "Socket creation error: %s\n", strerror(errno));
exit(1);
}
printf("sock = %i\n", *sock);
fflush(stdout);
}
void bindSocket(int sock, struct sockaddr_in* s_){
printf("s_->sin_family = %i\n",s_->sin_family);
printf("s_->sin_port = %i\n",s_->sin_port);
printf("s_->sin_addr.s_addr = %i\n",s_->sin_addr.s_addr);
if((bind(sock, (struct sockaddr*)s_, (socklen_t)sizeof(s_))) == -1){
fprintf(stderr, "Socket bind error: %s\n", strerror(errno));
}
printf("sizeof(s_) = %lu\n", sizeof(s_));
}
int main(int argc, char* argv[]){
int sockfd;
struct sockaddr_in socket_;
createSocket(&sockfd);
setSocket(&socket_);
printf("sockfd = %i\n", sockfd);
fflush(stdout);
bindSocket(sockfd, &socket_);
exit(0);
}
I believe the problem is the
sizeof()
inside your bind()... 's_' is a pointer, so its sizeof is (probably) 4...
You need to dereference it:
if((bind(sock, (struct sockaddr*)s_, (socklen_t)sizeof(*s_))) == -1){
fprintf(stderr, "Socket bind error: %s\n", strerror(errno));
}
I have a C function to check a host and its port, when I use FQDN host name, the function return error like: connect() failed: connect time out, but if I use IP address instead, it seems ok, how to fix this?
Thanks.
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
int is_network_up(char *chkhost, unsigned short chkport) {
int sock;
struct sockaddr_in chksock;
struct hostent *host = NULL;
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
syslog(LOG_ERR, "socket() creation error: %s", strerror(errno));
return 0;
}
memset(&chksock, 0, sizeof(chksock));
chksock.sin_family = AF_INET;
chksock.sin_port = htons(chkport);
/* get the server address */
if (inet_pton(AF_INET, chkhost, &(chksock.sin_addr.s_addr)) <= 0) {
if ((host = gethostbyname(chkhost)) == NULL) {
syslog(LOG_ERR, "%s", hstrerror(h_errno));
return 0;
}
memcpy(&(chksock.sin_addr.s_addr), &(host->h_addr_list[0]),
sizeof(struct in_addr));
}
/* try to connect */
if (connect(sock, (struct sockaddr *) &chksock, sizeof(chksock)) < 0) {
syslog(LOG_ERR, "connect() failed: %s", strerror(errno));
return 0;
}
close(sock);
return 1;
}
inet_pton() is the wrong task for that. It only accepts numerical addresses.
In former times, people used to use gethostbyname() for name resolution.
But as we have 2012 meanwhile, this method is outdated for several years now, as it is still restricted to AF_INET.
With the program below, you should achieve about the same and stay future compatible.
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
int is_network_up(char *chkhost, unsigned short chkport) {
int sock = -1;
struct addrinfo * res, *rp;
int ret = 0;
char sport[10];
snprintf(sport, sizeof sport, "%d", chkport);
struct addrinfo hints = { .ai_socktype=SOCK_STREAM };
if (getaddrinfo(chkhost, sport, &hints, &res)) {
perror("gai");
return 0;
}
for (rp = res; rp && !ret; rp = rp->ai_next) {
sock = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sock == -1) continue;
if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) {
char node[200], service[100];
getnameinfo(res->ai_addr, res->ai_addrlen, node, sizeof node, service, sizeof
service, NI_NUMERICHOST);
printf("Success on %s, %s\n", node, service);
ret = 1; /* Success */
}
close(sock);
}
freeaddrinfo(res);
return ret;
}
int main(int argc, char** argv) {
if (argc > 1) {
printf("%s: %d\n", argv[1], is_network_up(argv[1], 22));
}
}
Make sure name resolution is working. See if you can ping the machine by name from the exact same environment in which your code runs.
If ping works, try telnet <machinename> <portnumber> -- If both of those work it is likely a problem with your code (which I did not look at in depth, too sleepy:).
Make sure you're converting anything returned by the OS as an ip address from network order to host order. IIRC, gethostbyname returns binary ip addresses in network order.
ntohl can be used on chksock.sin_addr.s_addr after the memcpy to achieve this.